[go: up one dir, main page]

Skip to content

Commit

Permalink
completed new interpreter and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack You committed Jun 26, 2018
1 parent d597bd9 commit 9c1a58f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 86 deletions.
7 changes: 7 additions & 0 deletions .lein-repl-history
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ f 3
(interpreter/interpret [[:push 1] :print-int])
(interpreter/interpret [[:push 1] :print-int :end])
(interpreter/interpret (parser/parse (slurp "resources/count.c")))
(interpreter/interpret [[:push 1] :print-int :end])
(parser/parse (slurp "resources/count.c"))
(interpreter/interpret (parser/parse (slurp "resources/count.c")))
(parser/parse (slurp "resources/count.c"))
(interpreter/interpret (parser/parse (slurp "resources/count.c")))
(interpreter/interpret (parser/parse (slurp "resources/helloworld.ws")))
(parser/parse (slurp "resources/helloworld.ws"))
14 changes: 1 addition & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,10 @@ $ java -jar clj-whitespace-1.0-standalone.jar [options] file
## Options

```
-i, --intermediate Execute Clojure-Whitespace source generated by the compiler
-c, --compile Compile whitespace program into intermediate code
-i, --intermediate Pretty print Clojure intermediate source generated by the parser
-h, --help
```

## Test Cases

Test modules exist in `test/clj_whitespace`, they are minimal given the time constraints of the project.
However, they are fully extensible by any user (provided they know Clojure).

Tests can be executed using:

```
$ lein test
```

## Examples

There are two sample programs located in `resources`. `helloworld.ws` is purely Whitespace
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject clj-whitespace "1.0"
(defproject clj-whitespace "2.0"
:description "A Whitespace compiler in Clojure"
:url "https://github.com/jacksyou/clj-whitespace"
:license {:name "MIT Public License"
Expand Down
44 changes: 42 additions & 2 deletions src/clj_whitespace/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,47 @@
[clj-whitespace.interpreter :as interpreter]
[clojure.string :as string]
[clojure.tools.cli :refer [parse-opts]]
[instaparse.core :as insta])
[instaparse.core :as insta]
[clojure.pprint])
(:gen-class))


(def cli-options
[["-i" "--intermediate" "Generate intermediate Clojure for the Whitespace source" :default false]
["-h" "--help" :default false]])

(defn usage [options-summary]
(->> ["This program executes Whitespace source, with the option of producing an intermediate representation."
""
"Usage: clj-whitespace [options] source-file"
""
"Options:"
options-summary
""]
(string/join \newline)))

(defn error-msg [errors]
(str "The following errors occurred while parsing your command:\n\n"
(string/join \newline errors)))

(defn validate-args [args]
"We validate arguments for functionality in the main method."
(let [{:keys [options arguments errors summary]} (parse-opts args cli-options)]
(cond
(:help options) {:exit-message (usage summary) :ok? true}
errors {:exit-message (error-msg errors)}
(and (= 1 (count arguments)) (:intermediate options))
{:action :generate-intermediate :file (first arguments)}
(and (= 1 (count arguments)) (not (:intermediate options)))
{:action :execute-source :file (first arguments)}
:else ; failed custom validation => exit with usage summary
{:exit-message (usage summary)})))

(defn -main [& args]
"This function is the main class"
(let [{:keys [action file exit-message ok?]} (validate-args args)]
(if exit-message
(do (println exit-message)
(System/exit (if ok? 0 1)))
(case action
:generate-intermediate (clojure.pprint/pprint(parser/parse (slurp file)))
:execute-source (interpreter/interpret (parser/parse (slurp file)))))))
14 changes: 9 additions & 5 deletions src/clj_whitespace/interpreter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
[:mod]
(binary-op [mod])
[:store]
(let [val (consume) addr (consume)] (swap! heap conj addr val))
(let [val (consume) addr (consume)] (swap! heap conj {addr val}))
[:load]
(let [
addr (consume)
Expand All @@ -56,7 +56,9 @@
(do (print (consume)) (flush))
[:read-char]
(let [keyint (.read System/in) addr (consume)]
(swap! heap conj addr keyint))
(swap! heap conj {addr keyint}))
[[:location loc]]
(reset! state :continue)
[[:call addr]]
(do
(swap! call-stack conj (inc pc))
Expand All @@ -82,14 +84,16 @@
[:end] (reset! state :halt)
))
]
(while (= @state :continue)
(loop []
(do
(exec (prgm @pc))
(case @state
:continue
(swap! pc inc)
(do
(swap! pc inc)
(recur))
:jump
(swap! state :continue)
(recur)
:halt
(println ""))))))

Expand Down
12 changes: 0 additions & 12 deletions test/clj_whitespace/compiler_test.clj

This file was deleted.

53 changes: 0 additions & 53 deletions test/clj_whitespace/parser_test.clj

This file was deleted.

0 comments on commit 9c1a58f

Please sign in to comment.