forked from jmesyou/erasure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jack You
authored and
Jack You
committed
Apr 9, 2018
1 parent
696290b
commit 44ce002
Showing
11 changed files
with
210 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include <stdio.h> int main() {puts("Hello World")} | ||
|
||
|
||
|
||
|
@@ -27,9 +27,11 @@ | |
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,5 @@ | |
:printc | ||
[:push 10] | ||
:printc | ||
:pop | ||
:end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
(ns clj-whitespace.compiler | ||
(:require [clojure.core.match :refer [match]]) | ||
(:require [clj-whitespace.parser :as parser]) | ||
(:gen-class)) | ||
(:require [clojure.core.match :refer [match]]) | ||
(:require [clj-whitespace.parser :as parser]) | ||
(:gen-class)) | ||
|
||
(defn compile-helper [cmds prgm labels pc] | ||
(if (empty? cmds) | ||
[prgm labels] | ||
(let [[n-cmds n-prgm n-labels n-pc] | ||
(match [cmds] | ||
[([[:push a] :pop & xs] :seq)] [xs prgm labels pc] | ||
[([[:push a] [:push b] :swap & xs] :seq)] [xs (concat prgm [[:push b] [:push a]]) labels (+ pc 2)] | ||
[([([:label l] :as x) & xs] :seq)] (if (contains? labels l) | ||
(throw (Exception. "label already present in global table")) | ||
[xs prgm (conj labels {l pc}) pc]) | ||
[([x & xs] :seq)] [xs (conj prgm x) labels (inc pc)] | ||
[([:end] :seq)] [(list) (conj prgm :end) labels (-1)])] | ||
(recur n-cmds n-prgm n-labels n-pc)))) | ||
"" | ||
(if (empty? cmds) | ||
[prgm labels] | ||
(let [[n-cmds n-prgm n-labels n-pc] | ||
(match [cmds] | ||
[([[:push a] :pop & xs] :seq)] [xs prgm labels pc] | ||
[([:swap :swap & xs] :seq)] [xs prgm labels pc] | ||
[([[:push a] [:push b] :swap & xs] :seq)] [xs (concat prgm [[:push b] [:push a]]) labels (+ pc 2)] | ||
[([([:label l] :as x) & xs] :seq)] (if (contains? labels l) | ||
(throw (Exception. "label already present in global table")) | ||
[xs prgm (conj labels {l pc}) pc]) | ||
[([x & xs] :seq)] [xs (conj prgm x) labels (inc pc)] | ||
[([:end] :seq)] [(list) (conj prgm :end) labels (-1)])] | ||
(recur n-cmds n-prgm n-labels n-pc)))) | ||
|
||
(defn compile-tokens [cmds] (compile-helper cmds [] {} 0)) | ||
|
||
(defn compile-program [s & {:keys [mode] :or {mode :string}}] | ||
(case mode | ||
:commands (compile-tokens s) | ||
(let [tokens (parser/parse s :mode mode)] | ||
(compile-tokens tokens)))) | ||
(case mode | ||
:commands (compile-tokens s) | ||
(let [tokens (parser/parse s :mode mode)] | ||
(compile-tokens tokens)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,53 @@ | ||
(ns clj-whitespace.core | ||
(:require [clj-whitespace.parser :as parser]) | ||
(:require [clj-whitespace.compiler :as compiler]) | ||
(:require [clj-whitespace.programs :as programs]) | ||
(:require [clj-whitespace.runtime :as runtime]) | ||
(:require [clojure.tools.cli :refer [parse-opts]]) | ||
(:require [clj-whitespace.parser :as parser] | ||
[clj-whitespace.compiler :as compiler] | ||
[clj-whitespace.programs :as programs] | ||
[clj-whitespace.runtime :as runtime] | ||
[clojure.string :as string] | ||
[clojure.tools.cli :refer [parse-opts]]) | ||
(:gen-class)) | ||
|
||
(defn -main | ||
"I don't do a whole lot ... yet." | ||
[& args] | ||
() | ||
) | ||
(def cli-options | ||
[["-i" "--intermediate" "Execute Clojure-Whitespace source generated by the compiler" :default false] | ||
["-c" "--compile" "Compile whitespace program into intermediate code" :default false] | ||
["-h" "--help" :default false]]) | ||
|
||
(defn usage [options-summary] | ||
(->> ["This program compiles and executes Whitespace source, 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] | ||
(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)) (:compile options) (not (:intermediate options))) | ||
{:action :compile-source :file (first arguments)} | ||
(and (= 1 (count arguments)) (not (:compile options)) (:intermediate options)) | ||
{:action :execute-intermediate :file (first arguments)} | ||
(and (= 1 (count arguments)) (not (:compile options)) (not (:intermediate options))) | ||
{:action :execute-source :file (first arguments)} | ||
:else ; failed custom validation => exit with usage summary | ||
{:exit-message (usage summary)}))) | ||
|
||
(defn -main [& args] | ||
(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 | ||
:compile-source (parser/parse file :mode :file :intermediate? true) | ||
:execute-intermediate (let [[prgm labels] (compiler/compile-tokens (eval (read-string (slurp file))))] | ||
(runtime/main-routine prgm labels)) | ||
:execute-source (runtime/exec file :mode :file))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.