diff options
| author | Joel Martin <github@martintribe.org> | 2015-03-02 21:33:10 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-03-02 21:33:10 -0600 |
| commit | 835fb7d8b06e2b44792a97ac89994658bf6d00af (patch) | |
| tree | 578f67726ab9e3ce5fcbc50220e9761a66c5ddf1 /miniMAL | |
| parent | 6b72e6078a7d505ecf9d711eb4a16fc4dfac36b6 (diff) | |
| parent | 8a98ef9a3f3a6b6d05d02dc305a0c886c907e0f3 (diff) | |
| download | mal-835fb7d8b06e2b44792a97ac89994658bf6d00af.tar.gz mal-835fb7d8b06e2b44792a97ac89994658bf6d00af.zip | |
Merge branch 'master' into gh-pages
Conflicts:
.gitignore
Diffstat (limited to 'miniMAL')
| -rw-r--r-- | miniMAL/Makefile | 12 | ||||
| -rw-r--r-- | miniMAL/core.json | 154 | ||||
| -rw-r--r-- | miniMAL/env.json | 42 | ||||
| -rw-r--r-- | miniMAL/miniMAL-core.json | 111 | ||||
| l--------- | miniMAL/node_readline.js | 1 | ||||
| -rw-r--r-- | miniMAL/package.json | 8 | ||||
| -rw-r--r-- | miniMAL/printer.json | 66 | ||||
| -rw-r--r-- | miniMAL/reader.json | 126 | ||||
| -rw-r--r-- | miniMAL/step0_repl.json | 21 | ||||
| -rw-r--r-- | miniMAL/step1_read_print.json | 27 | ||||
| -rw-r--r-- | miniMAL/step2_eval.json | 60 | ||||
| -rw-r--r-- | miniMAL/step3_env.json | 74 | ||||
| -rw-r--r-- | miniMAL/step4_if_fn_do.json | 92 | ||||
| -rw-r--r-- | miniMAL/step5_tco.json | 100 | ||||
| -rw-r--r-- | miniMAL/step6_file.json | 107 | ||||
| -rw-r--r-- | miniMAL/step7_quote.json | 131 | ||||
| -rw-r--r-- | miniMAL/step8_macros.json | 157 | ||||
| -rw-r--r-- | miniMAL/step9_try.json | 168 | ||||
| -rw-r--r-- | miniMAL/stepA_mal.json | 171 | ||||
| -rw-r--r-- | miniMAL/types.json | 145 |
20 files changed, 1773 insertions, 0 deletions
diff --git a/miniMAL/Makefile b/miniMAL/Makefile new file mode 100644 index 0000000..8c838d3 --- /dev/null +++ b/miniMAL/Makefile @@ -0,0 +1,12 @@ + +SOURCES_BASE = node_readline.js miniMAL-core.json \ + types.json reader.json printer.json +SOURCES_LISP = env.json core.json stepA_mal.json +SOURCES = $(SOURCES_BASE) $(SOURCES_LISP) + +.PHONY: stats tests $(TESTS) + +stats: $(SOURCES) + @wc $^ +stats-lisp: $(SOURCES_LISP) + @wc $^ diff --git a/miniMAL/core.json b/miniMAL/core.json new file mode 100644 index 0000000..164a846 --- /dev/null +++ b/miniMAL/core.json @@ -0,0 +1,154 @@ +["do", + +["def", "_path", ["require", ["`", "path"]]], + +["def", "_node_readline", ["require", [".", "_path", ["`", "resolve"], + ["`", "."], + ["`", "node_readline.js"]]]], + +["def", "div", ["fn", ["a", "b"], ["parseInt", ["/", "a", "b"]]]], + +["def", "time-ms", ["fn", [], + [".", ["new", "Date"], ["`", "getTime"]]]], + + +["def", "assoc", ["fn", ["src-hm", "&", "kvs"], + ["let", ["hm", ["clone", "src-hm"]], + ["assocs!", "hm", "kvs"]]]], + +["def", "dissoc", ["fn", ["src-hm", "&", "ks"], + ["let", ["hm", ["clone", "src-hm"]], + ["do", + ["map", ["fn", ["k"], ["del", "hm", "k"]], "ks"], + "hm"]]]], + +["def", "_get", ["fn", ["obj", "key"], + ["if", ["nil?", "obj"], + null, + ["if", ["contains?", "obj", "key"], + ["get", "obj", "key"], + null]]]], + +["def", "_count", ["fn", ["a"], + ["if", ["=", null, "a"], + 0, + ["count", "a"]]]], + +["def", "_nth", ["fn", ["seq", "idx"], + ["if", [">=", "idx", ["count", "seq"]], + ["throw", "nth: index out of range"], + ["nth", "seq", "idx"]]]], + +["def", "_first", ["fn", ["seq"], + ["if", ["empty?", "seq"], + null, + ["first", "seq"]]]], + +["def", "_apply", ["fn", ["f", "&", "args"], + ["let", ["fn", ["if", ["malfunc?", "f"], ["get", "f", ["`", "fn"]], "f"], + "fargs", ["concat", ["slice", "args", 0, ["-", ["count", "args"], 1]], + ["nth", "args", ["-", ["count", "args"], 1]]]], + ["apply", "fn", "fargs"]]]], + +["def", "_map", ["fn", ["f", "seq"], + ["let", ["fn", ["if", ["malfunc?", "f"], ["get", "f", ["`", "fn"]], "f"]], + ["map", "fn", "seq"]]]], + +["def", "with_meta", ["fn", ["obj", "m"], + ["let", ["new-obj", ["clone", "obj"]], + ["do", + ["set", "new-obj", ["`", "__meta__"], "m"], + "new-obj"]]]], + +["def", "meta", ["fn", ["obj"], + ["if", ["or", ["sequential?", "obj"], + ["map?", "obj"], + ["malfunc?", "obj"]], + ["if", ["contains?", "obj", ["`", "__meta__"]], + ["get", "obj", ["`", "__meta__"]], + null], + null]]], + +["def", "reset!", ["fn", ["atm", "val"], + ["set", "atm", ["`", "val"], "val"]]], + +["def", "swap!", ["fn", ["atm", "f", "&", "args"], + ["let", ["fn", ["if", ["malfunc?", "f"], ["get", "f", ["`", "fn"]], "f"], + "fargs", ["cons", ["get", "atm", ["`", "val"]], "args"], + "val", ["apply", "fn", "fargs"]], + ["do", + ["set", "atm", ["`", "val"], "val"], + "val"]]]], + +["def", "core-ns", + ["hash-map", + ["`", "="], "equal?", + ["`", "throw"], "throw", + + ["`", "nil?"], "nil?", + ["`", "true?"], "true?", + ["`", "false?"], "false?", + ["`", "symbol"], "symbol", + ["`", "symbol?"], "symbol?", + ["`", "keyword"], "keyword", + ["`", "keyword?"], "keyword?", + + ["`", "pr-str"], ["fn", ["&", "a"], ["pr-list", "a", true, ["`", " "]]], + ["`", "str"], ["fn", ["&", "a"], ["pr-list", "a", false, ["`", ""]]], + ["`", "prn"], ["fn", ["&", "a"], + ["do", + ["println", ["pr-list", "a", true, ["`", " "]]], + null]], + ["`", "println"], ["fn", ["&", "a"], + ["do", + ["println", ["pr-list", "a", false, ["`", " "]]], + null]], + ["`", "read-string"], "read-str", + ["`", "readline"], ["fn", ["p"], + [".", "_node_readline", ["`", "readline"], "p"]], + ["`", "slurp"], "slurp", + + ["`", "<"], "<", + ["`", "<="], "<=", + ["`", ">"], ">", + ["`", ">="], ">=", + ["`", "+"], "+", + ["`", "-"], "-", + ["`", "*"], "*", + ["`", "/"], "div", + ["`", "time-ms"], "time-ms", + + ["`", "list"], "list", + ["`", "list?"], "list?", + ["`", "vector"], "vector", + ["`", "vector?"], "vector?", + ["`", "hash-map"], "hash-map", + ["`", "assoc"], "assoc", + ["`", "dissoc"], "dissoc", + ["`", "map?"], "map?", + ["`", "get"], "_get", + ["`", "contains?"], "contains?", + ["`", "keys"], "keys", + ["`", "vals"], "vals", + + ["`", "sequential?"], "sequential?", + ["`", "cons"], "cons", + ["`", "concat"], "concat", + ["`", "nth"], "_nth", + ["`", "first"], "_first", + ["`", "rest"], ["fn", ["a"], ["rest", "a"]], + ["`", "empty?"], "empty?", + ["`", "count"], "_count", + ["`", "apply"], "_apply", + ["`", "map"], "_map", + ["`", "conj"], null, + + ["`", "with-meta"], "with_meta", + ["`", "meta"], "meta", + ["`", "atom"], "atom", + ["`", "atom?"], "atom?", + ["`", "deref"], ["fn", ["a"], ["get", "a", ["`", "val"]]], + ["`", "reset!"], "reset!", + ["`", "swap!"], "swap!"]], + +null] diff --git a/miniMAL/env.json b/miniMAL/env.json new file mode 100644 index 0000000..0dfa67b --- /dev/null +++ b/miniMAL/env.json @@ -0,0 +1,42 @@ +["do", + +["def", "env-bind", ["fn", ["env", "b", "e"], + ["if", ["empty?", "b"], + "env", + ["if", ["=", ["`", "&"], + ["get", ["first", "b"], ["`", "val"]]], + ["assoc!", "env", ["get", ["nth", "b", 1], ["`", "val"]], "e"], + ["env-bind", ["assoc!", "env", ["get", ["first", "b"], ["`", "val"]], + ["first", "e"]], + ["rest", "b"], + ["rest", "e"]]]]]], + +["def", "env-new", ["fn", ["&", "args"], + ["let", ["env", ["hash-map", ["`", "__outer__"], ["first", "args"]]], + ["if", ["<=", ["count", "args"], 1], + "env", + ["env-bind", "env", ["get", "args", 1], ["get", "args", 2]]]]]], + +["def", "env-find", ["fn", ["env", "key"], + ["let", ["k", ["get", "key", ["`", "val"]]], + ["if", ["contains?", "env", "k"], + "env", + ["if", ["get", "env", ["`", "__outer__"]], + ["env-find", ["get", "env", ["`", "__outer__"]], "key"], + null]]]]], + +["def", "env-get", ["fn", ["env", "key"], + ["let", ["k", ["get", "key", ["`", "val"]], + "e", ["env-find", "env", "key"]], + ["if", "e", + ["get", "e", "k"], + ["throw", ["str", ["`", "'"], "k", ["`", "' not found"]]]]]]], + +["def", "env-set", ["fn", ["env", "key", "val"], + ["let", ["k", ["get", "key", ["`", "val"]]], + ["do", + ["assoc!", "env", "k", "val"], + "val"]]]], + +null +] diff --git a/miniMAL/miniMAL-core.json b/miniMAL/miniMAL-core.json new file mode 100644 index 0000000..c22376a --- /dev/null +++ b/miniMAL/miniMAL-core.json @@ -0,0 +1,111 @@ +["do", + +["def", "map", ["fn", ["a", "b"], [".", "b", ["`", "map"], "a"]]], +["def", "not", ["fn", ["a"], ["if", "a", false, true]]], + +["def", "nil?", ["fn", ["a"], ["=", null, "a"]]], +["def", "true?", ["fn", ["a"], ["=", true, "a"]]], +["def", "false?", ["fn", ["a"], ["=", false, "a"]]], +["def", "string?", ["fn", ["a"], + ["if", ["=", "a", null], + false, + ["=", ["`", "String"], + [".-", [".-", "a", ["`", "constructor"]], + ["`", "name"]]]]]], + +["def", "pr-list*", ["fn", ["a", "pr", "sep"], + [".", ["map", ["fn", ["x"], + ["if", "pr", + [".", "JSON", ["`", "stringify"], "x"], + ["if", ["string?", "x"], + "x", + [".", "JSON", ["`", "stringify"], "x"]]]], + "a"], + ["`", "join"], "sep"]]], +["def", "pr-str", ["fn", ["&", "a"], + ["pr-list*", "a", true, ["`", " "]]]], +["def", "str", ["fn", ["&", "a"], + ["pr-list*", "a", false, ["`", ""]]]], +["def", "prn", ["fn", ["&", "a"], + [".", "console", ["`", "log"], + ["pr-list*", "a", true, ["`", " "]]]]], +["def", "println", ["fn", ["&", "a"], + [".", "console", ["`", "log"], + ["pr-list*", "a", false, ["`", " "]]]]], + +["def", ">=", ["fn", ["a", "b"], + ["if", ["<", "a", "b"], false, true]]], +["def", ">", ["fn", ["a", "b"], + ["if", [">=", "a", "b"], ["if", ["=", "a", "b"], false, true], false]]], +["def", "<=", ["fn", ["a", "b"], + ["if", [">", "a", "b"], false, true]]], + +["def", "list", ["fn", ["&", "a"], "a"]], +["def", "list?", ["fn", ["a"], [".", "Array", ["`", "isArray"], "a"]]], +["def", "get", ["fn", ["a", "b"], [".-", "a", "b"]]], +["def", "set", ["fn", ["a", "b", "c"], [".-", "a", "b", "c"]]], +["def", "contains?", ["fn", ["a", "b"], [".", "a", ["`", "hasOwnProperty"], "b"]]], +["def", "keys", ["fn", ["a"], [".", "Object", ["`", "keys"], "a"]]], +["def", "vals", ["fn", ["a"], ["map",["fn",["k"],["get","a","k"]],["keys", "a"]]]], + +["def", "cons", ["fn", ["a", "b"], + [".", ["`", []], + ["`", "concat"], ["list", "a"], "b"]]], +["def", "concat", ["fn", ["&", "a"], + [".", [".-", ["list"], ["`", "concat"]], + ["`", "apply"], ["list"], "a"]]], +["def", "nth", "get"], +["def", "first", ["fn", ["a"], ["nth", "a", 0]]], +["def", "rest", ["fn", ["a"], [".", "a", ["`", "slice"], 1]]], +["def", "empty?", ["fn", ["a"], ["if", ["list?", "a"], ["if", ["=", 0, [".-", "a", ["`", "length"]]], true, false], ["=", "a", null]]]], +["def", "count", ["fn", ["a"], + [".-", "a", ["`", "length"]]]], +["def", "slice", ["fn", ["a", "start", "&", "endl"], + ["let", ["end", ["if", ["count", "endl"], + ["get", "endl", 0], + [".-", "a", ["`", "length"]]]], + [".", "a", ["`", "slice"], "start", "end"]]]], + +["def", "apply", ["fn", ["a", "b"], [".", "a", ["`", "apply"], "a", "b"]]], + +["def", "and", ["~", ["fn", ["&", "xs"], + ["if", ["empty?", "xs"], + true, + ["if", ["=", 1, ["count", "xs"]], + ["first", "xs"], + ["list", ["`", "let"], ["list", ["`", "and_FIXME"], ["first", "xs"]], + ["list", ["`", "if"], ["`", "and_FIXME"], + ["concat", ["`", ["and"]], ["rest", "xs"]], + ["`", "and_FIXME"]]]]]]]], + +["def", "or", ["~", ["fn", ["&", "xs"], + ["if", ["empty?", "xs"], + null, + ["if", ["=", 1, ["count", "xs"]], + ["first", "xs"], + ["list", ["`", "let"], ["list", ["`", "or_FIXME"], ["first", "xs"]], + ["list", ["`", "if"], ["`", "or_FIXME"], + ["`", "or_FIXME"], + ["concat", ["`", ["or"]], ["rest", "xs"]]]]]]]]], + +["def", "classOf", ["fn", ["a"], + [".", [".-", [".-", "Object", ["`", "prototype"]], ["`", "toString"]], + ["`", "call"], "a"]]], + + +["def", "repl", ["fn",["prompt", "rep"], + ["let", ["r", ["require", ["`", "repl"]], + "evl", ["fn", ["l", "c", "f", "cb"], + ["let", ["line", ["slice", "l", 1, ["-", [".-", "l", ["`", "length"]], 2]]], + ["do", + ["println", ["rep", "line"]], + ["cb"]]]], + "opts", {"ignoreUndefined": true, + "terminal": false}, + "opts", ["assoc!", "opts", ["`", "prompt"], "prompt"], + "opts", ["assoc!", "opts", ["`", "eval"], "evl"]], + [".", "r", ["`", "start"], "opts"]]]], + +null +] + diff --git a/miniMAL/node_readline.js b/miniMAL/node_readline.js new file mode 120000 index 0000000..7771772 --- /dev/null +++ b/miniMAL/node_readline.js @@ -0,0 +1 @@ +../js/node_readline.js
\ No newline at end of file diff --git a/miniMAL/package.json b/miniMAL/package.json new file mode 100644 index 0000000..24d7a03 --- /dev/null +++ b/miniMAL/package.json @@ -0,0 +1,8 @@ +{ + "name": "mal-miniMAL", + "version": "0.0.1", + "description": "Make a Lisp (mal) language implemented in miniMAL", + "dependencies": { + "minimal-lisp": "0.0.3" + } +} diff --git a/miniMAL/printer.json b/miniMAL/printer.json new file mode 100644 index 0000000..7463c78 --- /dev/null +++ b/miniMAL/printer.json @@ -0,0 +1,66 @@ +["do", + +["def", "pr-str", ["fn", ["exp", "print_readably"], + ["if", ["list?", "exp"], + ["str", + ["`", "("], + [".", ["map", ["fn", ["x"], ["pr-str", "x", "print_readably"]], "exp"], + ["`", "join"], ["`", " "]], + ["`", ")"]], + ["if", ["vector?", "exp"], + ["str", + ["`", "["], + [".", ["map", ["fn", ["x"], ["pr-str", "x", "print_readably"]], "exp"], + ["`", "join"], ["`", " "]], + ["`", "]"]], + ["if", ["map?", "exp"], + ["str", + ["`", "{"], + [".", ["map", ["fn", ["k"], + ["str", ["pr-str", "k", "print_readably"], + ["`", " "], + ["pr-str", ["get", "exp", "k"], "print_readably"]]], + ["keys", "exp"]], + ["`", "join"], ["`", " "]], + ["`", "}"]], + ["if", ["=", ["`", "[object String]"], ["classOf", "exp"]], + ["if", ["=", ["`", "\u029e"], ["get", "exp", 0]], + ["str", ["`", ":"], ["slice", "exp", 1]], + ["if", "print_readably", + ["str", ["`", "\""], + [".", + [".", + [".", "exp", + ["`", "replace"], ["RegExp", ["`", "\\\\"], ["`", "g"]], ["`", "\\\\"]], + ["`", "replace"], ["RegExp", ["`", "\""], ["`", "g"]], ["`", "\\\""]], + ["`", "replace"], ["RegExp", ["`", "\n"], ["`", "g"]], ["`", "\\n"]], + ["`", "\""]], + "exp"]], + ["if", ["=", ["`", "[object Number]"], ["classOf", "exp"]], + "exp", + ["if", ["=", null, "exp"], + ["`", "nil"], + ["if", ["=", true, "exp"], + ["`", "true"], + ["if", ["=", false, "exp"], + ["`", "false"], + ["if", ["symbol?", "exp"], + ["get", "exp", ["`", "val"]], + ["if", ["malfunc?", "exp"], + ["str", ["`", "(fn* "], + ["pr-str", ["get", "exp", ["`", "params"]]], + ["`", " "], + ["pr-str", ["get", "exp", ["`", "ast"]]], + ["`", ")"]], + ["if", ["=", ["`", "[object Function]"], ["classOf", "exp"]], + ["str", ["`", "#<native function>"]], + ["if", ["atom?", "exp"], + ["str", ["`", "(atom "], ["get", "exp", ["`", "val"]], ["`", ")"]], + ["str", ["`", "#<unknown: "], "exp", ["`", ">"]]]]]]]]]]]]]]]], + +["def", "pr-list", ["fn", ["lst", "print_readably", "sep"], + [".", ["map", ["fn", ["s"], ["pr-str", "s", "print_readably"]], "lst"], + ["`", "join"], "sep"]]], + +null +] diff --git a/miniMAL/reader.json b/miniMAL/reader.json new file mode 100644 index 0000000..5fa113b --- /dev/null +++ b/miniMAL/reader.json @@ -0,0 +1,126 @@ +["do", + +["def", "rdr-new", ["fn", ["tokens"], + ["hash-map", ["`", "tokens"], "tokens", + ["`", "position"], 0]]], + +["def", "rdr-next", ["fn", ["rdr"], + ["let", ["pos", ["get", "rdr", ["`", "position"]], + "val", ["get", ["get", "rdr", ["`", "tokens"]], "pos"]], + ["do", + ["assoc!", "rdr", ["`", "position"], ["+", 1, "pos"]], + "val"]]]], + +["def", "rdr-peek", ["fn", ["rdr"], + ["let", ["pos", ["get", "rdr", ["`", "position"]]], + ["get", ["get", "rdr", ["`", "tokens"]], "pos"]]]], + + +["def", "re-matches", ["fn", ["re", "strn", "acc"], + ["let", ["match", [".", "re", ["`", "exec"], "strn"], + "g1", ["get", "match", 1]], + ["if", ["=", "g1", ["`", ""]], + "acc", + ["re-matches", "re", "strn", ["concat", "acc", "g1"]]]]]], + +["def", "tokenize", ["fn", ["strn"], + ["let", ["re-str", ["`", "[\\s,]*(~@|[\\[\\]{}()'`~^@]|\"(?:\\\\.|[^\\\\\"])*\"|;.*|[^\\s\\[\\]{}('\"`,;)]*)"], + "re", ["RegExp", "re-str", ["`", "g"]]], + [".", + ["re-matches", "re", "strn", ["`", []]], + ["`", "filter"], + ["fn", ["x"], ["not", ["=", ["get", "x", 0], + ["`", ";"]]]]]]]], + +["def", "read-atom", ["fn", ["rdr"], + ["let", ["token", ["rdr-next", "rdr"]], + ["if", [".", "token", ["`", "match"], ["RegExp", ["`", "^-?[0-9]+$"]]], + ["parseInt", "token", 10], + ["if", ["=", ["`", "\""], ["get", "token", 0]], + [".", + [".", + ["slice", "token", 1, ["-", ["count", "token"], 1]], + ["`", "replace"], ["RegExp", ["`", "\\\\\""], ["`", "g"]], ["`", "\""]], + ["`", "replace"], ["RegExp", ["`", "\\\\n"], ["`", "g"]], ["`", "\n"]], + ["if", ["=", ["`", ":"], ["get", "token", 0]], + ["keyword", ["slice", "token", 1]], + ["if", ["=", ["`", "nil"], "token"], + null, + ["if", ["=", ["`", "true"], "token"], + true, + ["if", ["=", ["`", "false"], "token"], + false, + ["symbol", "token"]]]]]]]]]], + +["def", "read-list-entries", ["fn", ["rdr", "start", "end"], + ["let", ["tok", ["rdr-peek", "rdr"]], + ["if", "tok", + ["if", ["=", "end", "tok"], + ["`", []], + ["cons", ["read-form", "rdr"], + ["read-list-entries", "rdr", "start", "end"]]], + ["throw", ["str", ["`", "expected "], "end"]]]]]], + +["def", "read-list", ["fn", ["rdr", "start", "end"], + ["let", ["token", ["rdr-next", "rdr"]], + ["if", ["=", "start", "token"], + ["let", ["lst", ["read-list-entries", "rdr", "start", "end"]], + ["do", + ["rdr-next", "rdr"], + "lst"]], + ["throw", ["str", ["`", "expected "], "start"]]]]]], + +["def", "read-form", ["fn", ["rdr"], + ["let", ["token", ["rdr-peek", "rdr"]], + ["if", ["=", ["`", "'"], "token"], + ["do", + ["rdr-next", "rdr"], + ["list", ["symbol", ["`", "quote"]], ["read-form", "rdr"]]], + ["if", ["=", ["`", "`"], "token"], + ["do", + ["rdr-next", "rdr"], + ["list", ["symbol", ["`", "quasiquote"]], ["read-form", "rdr"]]], + ["if", ["=", ["`", "~"], "token"], + ["do", + ["rdr-next", "rdr"], + ["list", ["symbol", ["`", "unquote"]], ["read-form", "rdr"]]], + ["if", ["=", ["`", "~@"], "token"], + ["do", + ["rdr-next", "rdr"], + ["list", ["symbol", ["`", "splice-unquote"]], ["read-form", "rdr"]]], + ["if", ["=", ["`", "^"], "token"], + ["do", + ["rdr-next", "rdr"], + ["let", ["meta", ["read-form", "rdr"]], + ["list", ["symbol", ["`", "with-meta"]], ["read-form", "rdr"], "meta"]]], + ["if", ["=", ["`", "@"], "token"], + ["do", + ["rdr-next", "rdr"], + ["list", ["symbol", ["`", "deref"]], ["read-form", "rdr"]]], + + ["if", ["=", ["`", ")"], "token"], + ["throw", ["`", "unexpected ')'"]], + ["if", ["=", ["`", "("], "token"], + ["read-list", "rdr", ["`", "("], ["`", ")"]], + + ["if", ["=", ["`", "]"], "token"], + ["throw", ["`", "unexpected ']'"]], + ["if", ["=", ["`", "["], "token"], + ["vectorl", ["read-list", "rdr", ["`", "["], ["`", "]"]]], + + ["if", ["=", ["`", "}"], "token"], + ["throw", ["`", "unexpected '}'"]], + ["if", ["=", ["`", "{"], "token"], + ["apply", "hash-map", ["read-list", "rdr", ["`", "{"], ["`", "}"]]], + + ["read-atom", "rdr"]]]]]]]]]]]]]]]], + +["def", "read-str", ["fn", ["strn"], + ["let", ["tokens", ["tokenize", "strn"], + "rdr", ["rdr-new", "tokens"]], + ["if", ["empty?", "tokens"], + null, + ["read-form", "rdr"]]]]], + +null +] diff --git a/miniMAL/step0_repl.json b/miniMAL/step0_repl.json new file mode 100644 index 0000000..6599930 --- /dev/null +++ b/miniMAL/step0_repl.json @@ -0,0 +1,21 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], + +["def", "READ", ["fn", ["strng"], + "strng"]], + +["def", "EVAL", ["fn", ["ast", "env"], + "ast"]], + +["def", "PRINT", ["fn", ["exp"], + "exp"]], + +["def", "rep", ["fn", ["strng"], + ["PRINT", ["EVAL", ["READ", "strng"], null]]]], + +["repl", ["`", "user> "], "rep"], + +null + +] diff --git a/miniMAL/step1_read_print.json b/miniMAL/step1_read_print.json new file mode 100644 index 0000000..dc1f269 --- /dev/null +++ b/miniMAL/step1_read_print.json @@ -0,0 +1,27 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], + +["def", "READ", ["fn", ["strng"], + ["read-str", "strng"]]], + +["def", "EVAL", ["fn", ["ast", "env"], + "ast"]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], null]], + ["catch", "exc", + ["str", ["`", "Error: "], "exc"]]]]], + +["repl", ["`", "user> "], "rep"], + +null + +] diff --git a/miniMAL/step2_eval.json b/miniMAL/step2_eval.json new file mode 100644 index 0000000..ee95fe2 --- /dev/null +++ b/miniMAL/step2_eval.json @@ -0,0 +1,60 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], + +["def", "READ", ["fn", ["strng"], + ["read-str", "strng"]]], + +["def", "eval-ast", ["fn", ["ast", "env"], + ["if", ["symbol?", "ast"], + ["let", ["sym", ["get", "ast", ["`", "val"]]], + ["if", ["contains?", "env", "sym"], + ["get", "env", "sym"], + ["throw", ["str", ["`", "'"], "sym", ["`", "' not found"]]]]], + ["if", ["list?", "ast"], + ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], + ["if", ["vector?", "ast"], + ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], + ["if", ["map?", "ast"], + ["let", ["new-hm", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], ["set", "new-hm", + ["EVAL", "k", "env"], + ["EVAL", ["get", "ast", "k"], "env"]]], + ["keys", "ast"]], + "new-hm"]], + "ast"]]]]]], + +["def", "EVAL", ["fn", ["ast", "env"], + ["if", ["not", ["list?", "ast"]], + ["eval-ast", "ast", "env"], + ["let", ["el", ["eval-ast", "ast", "env"], + "f", ["first", "el"], + "args", ["rest", "el"]], + ["apply", "f", "args"]]]]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + + +["def", "repl-env", + ["hash-map", + ["`", "+"], "+", + ["`", "-"], "-", + ["`", "*"], "*", + ["`", "/"], ["fn", ["a", "b"], ["parseInt", ["/", "a", "b"]]]]], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], + ["catch", "exc", + ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], + +["repl", ["`", "user> "], "rep"], + +null + +] diff --git a/miniMAL/step3_env.json b/miniMAL/step3_env.json new file mode 100644 index 0000000..e72de07 --- /dev/null +++ b/miniMAL/step3_env.json @@ -0,0 +1,74 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], +["load-file", ["`", "env.json"]], + +["def", "READ", ["fn", ["strng"], + ["read-str", "strng"]]], + +["def", "eval-ast", ["fn", ["ast", "env"], + ["if", ["symbol?", "ast"], + ["env-get", "env", "ast"], + ["if", ["list?", "ast"], + ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], + ["if", ["vector?", "ast"], + ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], + ["if", ["map?", "ast"], + ["let", ["new-hm", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], ["set", "new-hm", + ["EVAL", "k", "env"], + ["EVAL", ["get", "ast", "k"], "env"]]], + ["keys", "ast"]], + "new-hm"]], + "ast"]]]]]], + +["def", "LET", ["fn", ["env", "args"], + ["if", [">", ["count", "args"], 0], + ["do", + ["env-set", "env", ["nth", "args", 0], + ["EVAL", ["nth", "args", 1], "env"]], + ["LET", "env", ["rest", ["rest", "args"]]]]]]], + +["def", "EVAL", ["fn", ["ast", "env"], + ["if", ["not", ["list?", "ast"]], + ["eval-ast", "ast", "env"], + ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]], + ["if", ["=", ["`", "def!"], "a0"], + ["env-set", "env", ["nth", "ast", 1], + ["EVAL", ["nth", "ast", 2], "env"]], + ["if", ["=", ["`", "let*"], "a0"], + ["let", ["let-env", ["env-new", "env"]], + ["do", + ["LET", "let-env", ["nth", "ast", 1]], + ["EVAL", ["nth", "ast", 2], "let-env"]]], + ["let", ["el", ["eval-ast", "ast", "env"], + "f", ["first", "el"], + "args", ["rest", "el"]], + ["apply", "f", "args"]]]]]]]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + + +["def", "repl-env", ["env-new"]], +["env-set", "repl-env", ["symbol", ["`", "+"]], "+"], +["env-set", "repl-env", ["symbol", ["`", "-"]], "-"], +["env-set", "repl-env", ["symbol", ["`", "*"]], "*"], +["def", "div", ["fn", ["a", "b"], ["parseInt", ["/", "a", "b"]]]], +["env-set", "repl-env", ["symbol", ["`", "/"]], "div"], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], + ["catch", "exc", + ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], + +["repl", ["`", "user> "], "rep"], + +null + +] diff --git a/miniMAL/step4_if_fn_do.json b/miniMAL/step4_if_fn_do.json new file mode 100644 index 0000000..97e8021 --- /dev/null +++ b/miniMAL/step4_if_fn_do.json @@ -0,0 +1,92 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], +["load-file", ["`", "env.json"]], +["load-file", ["`", "core.json"]], + +["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]], + +["def", "eval-ast", ["fn", ["ast", "env"], + ["if", ["symbol?", "ast"], + ["env-get", "env", "ast"], + ["if", ["list?", "ast"], + ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], + ["if", ["vector?", "ast"], + ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], + ["if", ["map?", "ast"], + ["let", ["new-hm", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], ["set", "new-hm", + ["EVAL", "k", "env"], + ["EVAL", ["get", "ast", "k"], "env"]]], + ["keys", "ast"]], + "new-hm"]], + "ast"]]]]]], + +["def", "LET", ["fn", ["env", "args"], + ["if", [">", ["count", "args"], 0], + ["do", + ["env-set", "env", ["nth", "args", 0], + ["EVAL", ["nth", "args", 1], "env"]], + ["LET", "env", ["rest", ["rest", "args"]]]]]]], + +["def", "EVAL", ["fn", ["ast", "env"], + ["if", ["not", ["list?", "ast"]], + ["eval-ast", "ast", "env"], + ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]], + ["if", ["=", ["`", "def!"], "a0"], + ["env-set", "env", ["nth", "ast", 1], + ["EVAL", ["nth", "ast", 2], "env"]], + ["if", ["=", ["`", "let*"], "a0"], + ["let", ["let-env", ["env-new", "env"]], + ["do", + ["LET", "let-env", ["nth", "ast", 1]], + ["EVAL", ["nth", "ast", 2], "let-env"]]], + ["if", ["=", ["`", "do"], "a0"], + ["let", ["el", ["eval-ast", ["rest", "ast"], "env"]], + ["nth", "el", ["-", ["count", "el"], 1]]], + ["if", ["=", ["`", "if"], "a0"], + ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]], + ["if", ["or", ["=", "cond", null], ["=", "cond", false]], + ["if", [">", ["count", "ast"], 3], + ["EVAL", ["nth", "ast", 3], "env"], + null], + ["EVAL", ["nth", "ast", 2], "env"]]], + ["if", ["=", ["`", "fn*"], "a0"], + ["fn", ["&", "args"], + ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]], + ["EVAL", ["nth", "ast", 2], "e"]]], + ["let", ["el", ["eval-ast", "ast", "env"], + "f", ["first", "el"], + "args", ["rest", "el"]], + ["apply", "f", "args"]]]]]]]]]]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + + +["def", "repl-env", ["env-new"]], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], + ["catch", "exc", + ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], + +["`", "core.mal: defined using miniMAL"], +["map", ["fn", ["k"], ["env-set", "repl-env", + ["symbol", "k"], + ["get", "core-ns", "k"]]], + ["keys", "core-ns"]], + +["`", "core.mal: defined using mal itself"], +["rep", ["`", "(def! not (fn* (a) (if a false true)))"]], + +["repl", ["`", "user> "], "rep"], + +null + +] diff --git a/miniMAL/step5_tco.json b/miniMAL/step5_tco.json new file mode 100644 index 0000000..06fd342 --- /dev/null +++ b/miniMAL/step5_tco.json @@ -0,0 +1,100 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], +["load-file", ["`", "env.json"]], +["load-file", ["`", "core.json"]], + +["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]], + +["def", "eval-ast", ["fn", ["ast", "env"], + ["if", ["symbol?", "ast"], + ["env-get", "env", "ast"], + ["if", ["list?", "ast"], + ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], + ["if", ["vector?", "ast"], + ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], + ["if", ["map?", "ast"], + ["let", ["new-hm", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], ["set", "new-hm", + ["EVAL", "k", "env"], + ["EVAL", ["get", "ast", "k"], "env"]]], + ["keys", "ast"]], + "new-hm"]], + "ast"]]]]]], + +["def", "LET", ["fn", ["env", "args"], + ["if", [">", ["count", "args"], 0], + ["do", + ["env-set", "env", ["nth", "args", 0], + ["EVAL", ["nth", "args", 1], "env"]], + ["LET", "env", ["rest", ["rest", "args"]]]]]]], + +["def", "EVAL", ["fn", ["ast", "env"], + ["if", ["not", ["list?", "ast"]], + ["eval-ast", "ast", "env"], + ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]], + ["if", ["=", ["`", "def!"], "a0"], + ["env-set", "env", ["nth", "ast", 1], + ["EVAL", ["nth", "ast", 2], "env"]], + ["if", ["=", ["`", "let*"], "a0"], + ["let", ["let-env", ["env-new", "env"]], + ["do", + ["LET", "let-env", ["nth", "ast", 1]], + ["EVAL", ["nth", "ast", 2], "let-env"]]], + ["if", ["=", ["`", "do"], "a0"], + ["do", + ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"], + ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]], + ["if", ["=", ["`", "if"], "a0"], + ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]], + ["if", ["or", ["=", "cond", null], ["=", "cond", false]], + ["if", [">", ["count", "ast"], 3], + ["EVAL", ["nth", "ast", 3], "env"], + null], + ["EVAL", ["nth", "ast", 2], "env"]]], + ["if", ["=", ["`", "fn*"], "a0"], + ["malfunc", + ["fn", ["&", "args"], + ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]], + ["EVAL", ["nth", "ast", 2], "e"]]], + ["nth", "ast", 2], "env", ["nth", "ast", 1]], + ["let", ["el", ["eval-ast", "ast", "env"], + "f", ["first", "el"], + "args", ["rest", "el"]], + ["if", ["malfunc?", "f"], + ["EVAL", ["get", "f", ["`", "ast"]], + ["env-new", ["get", "f", ["`", "env"]], + ["get", "f", ["`", "params"]], + "args"]], + ["apply", "f", "args"]]]]]]]]]]]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + + +["def", "repl-env", ["env-new"]], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], + ["catch", "exc", + ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], + +["`", "core.mal: defined using miniMAL"], +["map", ["fn", ["k"], ["env-set", "repl-env", + ["symbol", "k"], + ["get", "core-ns", "k"]]], + ["keys", "core-ns"]], + +["`", "core.mal: defined using mal itself"], +["rep", ["`", "(def! not (fn* (a) (if a false true)))"]], + +["repl", ["`", "user> "], "rep"], + +null + +] diff --git a/miniMAL/step6_file.json b/miniMAL/step6_file.json new file mode 100644 index 0000000..08f22c4 --- /dev/null +++ b/miniMAL/step6_file.json @@ -0,0 +1,107 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], +["load-file", ["`", "env.json"]], +["load-file", ["`", "core.json"]], + +["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]], + +["def", "eval-ast", ["fn", ["ast", "env"], + ["if", ["symbol?", "ast"], + ["env-get", "env", "ast"], + ["if", ["list?", "ast"], + ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], + ["if", ["vector?", "ast"], + ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], + ["if", ["map?", "ast"], + ["let", ["new-hm", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], ["set", "new-hm", + ["EVAL", "k", "env"], + ["EVAL", ["get", "ast", "k"], "env"]]], + ["keys", "ast"]], + "new-hm"]], + "ast"]]]]]], + +["def", "LET", ["fn", ["env", "args"], + ["if", [">", ["count", "args"], 0], + ["do", + ["env-set", "env", ["nth", "args", 0], + ["EVAL", ["nth", "args", 1], "env"]], + ["LET", "env", ["rest", ["rest", "args"]]]]]]], + +["def", "EVAL", ["fn", ["ast", "env"], + ["if", ["not", ["list?", "ast"]], + ["eval-ast", "ast", "env"], + ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]], + ["if", ["=", ["`", "def!"], "a0"], + ["env-set", "env", ["nth", "ast", 1], + ["EVAL", ["nth", "ast", 2], "env"]], + ["if", ["=", ["`", "let*"], "a0"], + ["let", ["let-env", ["env-new", "env"]], + ["do", + ["LET", "let-env", ["nth", "ast", 1]], + ["EVAL", ["nth", "ast", 2], "let-env"]]], + ["if", ["=", ["`", "do"], "a0"], + ["do", + ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"], + ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]], + ["if", ["=", ["`", "if"], "a0"], + ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]], + ["if", ["or", ["=", "cond", null], ["=", "cond", false]], + ["if", [">", ["count", "ast"], 3], + ["EVAL", ["nth", "ast", 3], "env"], + null], + ["EVAL", ["nth", "ast", 2], "env"]]], + ["if", ["=", ["`", "fn*"], "a0"], + ["malfunc", + ["fn", ["&", "args"], + ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]], + ["EVAL", ["nth", "ast", 2], "e"]]], + ["nth", "ast", 2], "env", ["nth", "ast", 1]], + ["let", ["el", ["eval-ast", "ast", "env"], + "f", ["first", "el"], + "args", ["rest", "el"]], + ["if", ["malfunc?", "f"], + ["EVAL", ["get", "f", ["`", "ast"]], + ["env-new", ["get", "f", ["`", "env"]], + ["get", "f", ["`", "params"]], + "args"]], + ["apply", "f", "args"]]]]]]]]]]]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + + +["def", "repl-env", ["env-new"]], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], + ["catch", "exc", + ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], + +["`", "core.mal: defined using miniMAL"], +["map", ["fn", ["k"], ["env-set", "repl-env", + ["symbol", "k"], + ["get", "core-ns", "k"]]], + ["keys", "core-ns"]], +["env-set", "repl-env", ["symbol", ["`", "eval"]], + ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]], +["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]], + ["slice", "*ARGV*", 1]], + +["`", "core.mal: defined using mal itself"], +["rep", ["`", "(def! not (fn* (a) (if a false true)))"]], +["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]], + +["if", ["not", ["empty?", "*ARGV*"]], + ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]], + ["repl", ["`", "user> "], "rep"]], + +null + +] diff --git a/miniMAL/step7_quote.json b/miniMAL/step7_quote.json new file mode 100644 index 0000000..78fd49e --- /dev/null +++ b/miniMAL/step7_quote.json @@ -0,0 +1,131 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], +["load-file", ["`", "env.json"]], +["load-file", ["`", "core.json"]], + +["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]], + +["def", "pair?", ["fn", ["x"], + ["if", ["sequential?", "x"], + ["if", [">", ["count", "x"], 0], true, false], + false]]], + +["def", "quasiquote", ["fn", ["ast"], + ["if", ["not", ["pair?", "ast"]], + ["list", ["symbol", ["`", "quote"]], "ast"], + ["if", ["=", ["`", "unquote"], ["get", ["nth", "ast", 0], ["`", "val"]]], + ["nth", "ast", 1], + ["if", ["and", ["pair?", ["nth", "ast", 0]], + ["=", ["`", "splice-unquote"], + ["get", ["nth", ["nth", "ast", 0], 0], ["`", "val"]]]], + ["list", ["symbol", ["`", "concat"]], + ["nth", ["nth", "ast", 0], 1], + ["quasiquote", ["rest", "ast"]]], + ["list", ["symbol", ["`", "cons"]], + ["quasiquote", ["nth", "ast", 0]], + ["quasiquote", ["rest", "ast"]]]]]]]], + +["def", "eval-ast", ["fn", ["ast", "env"], + ["if", ["symbol?", "ast"], + ["env-get", "env", "ast"], + ["if", ["list?", "ast"], + ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], + ["if", ["vector?", "ast"], + ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], + ["if", ["map?", "ast"], + ["let", ["new-hm", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], ["set", "new-hm", + ["EVAL", "k", "env"], + ["EVAL", ["get", "ast", "k"], "env"]]], + ["keys", "ast"]], + "new-hm"]], + "ast"]]]]]], + +["def", "LET", ["fn", ["env", "args"], + ["if", [">", ["count", "args"], 0], + ["do", + ["env-set", "env", ["nth", "args", 0], + ["EVAL", ["nth", "args", 1], "env"]], + ["LET", "env", ["rest", ["rest", "args"]]]]]]], + +["def", "EVAL", ["fn", ["ast", "env"], + ["if", ["not", ["list?", "ast"]], + ["eval-ast", "ast", "env"], + ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]], + ["if", ["=", ["`", "def!"], "a0"], + ["env-set", "env", ["nth", "ast", 1], + ["EVAL", ["nth", "ast", 2], "env"]], + ["if", ["=", ["`", "let*"], "a0"], + ["let", ["let-env", ["env-new", "env"]], + ["do", + ["LET", "let-env", ["nth", "ast", 1]], + ["EVAL", ["nth", "ast", 2], "let-env"]]], + ["if", ["=", ["`", "quote"], "a0"], + ["nth", "ast", 1], + ["if", ["=", ["`", "quasiquote"], "a0"], + ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"], + ["if", ["=", ["`", "do"], "a0"], + ["do", + ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"], + ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]], + ["if", ["=", ["`", "if"], "a0"], + ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]], + ["if", ["or", ["=", "cond", null], ["=", "cond", false]], + ["if", [">", ["count", "ast"], 3], + ["EVAL", ["nth", "ast", 3], "env"], + null], + ["EVAL", ["nth", "ast", 2], "env"]]], + ["if", ["=", ["`", "fn*"], "a0"], + ["malfunc", + ["fn", ["&", "args"], + ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]], + ["EVAL", ["nth", "ast", 2], "e"]]], + ["nth", "ast", 2], "env", ["nth", "ast", 1]], + ["let", ["el", ["eval-ast", "ast", "env"], + "f", ["first", "el"], + "args", ["rest", "el"]], + ["if", ["malfunc?", "f"], + ["EVAL", ["get", "f", ["`", "ast"]], + ["env-new", ["get", "f", ["`", "env"]], + ["get", "f", ["`", "params"]], + "args"]], + ["apply", "f", "args"]]]]]]]]]]]]]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + + +["def", "repl-env", ["env-new"]], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], + ["catch", "exc", + ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], + +["`", "core.mal: defined using miniMAL"], +["map", ["fn", ["k"], ["env-set", "repl-env", + ["symbol", "k"], + ["get", "core-ns", "k"]]], + ["keys", "core-ns"]], +["env-set", "repl-env", ["symbol", ["`", "eval"]], + ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]], +["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]], + ["slice", "*ARGV*", 1]], + +["`", "core.mal: defined using mal itself"], +["rep", ["`", "(def! not (fn* (a) (if a false true)))"]], +["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]], + +["if", ["not", ["empty?", "*ARGV*"]], + ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]], + ["repl", ["`", "user> "], "rep"]], + +null + +] diff --git a/miniMAL/step8_macros.json b/miniMAL/step8_macros.json new file mode 100644 index 0000000..8b93fd0 --- /dev/null +++ b/miniMAL/step8_macros.json @@ -0,0 +1,157 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], +["load-file", ["`", "env.json"]], +["load-file", ["`", "core.json"]], + +["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]], + +["def", "pair?", ["fn", ["x"], + ["if", ["sequential?", "x"], + ["if", [">", ["count", "x"], 0], true, false], + false]]], + +["def", "quasiquote", ["fn", ["ast"], + ["if", ["not", ["pair?", "ast"]], + ["list", ["symbol", ["`", "quote"]], "ast"], + ["if", ["=", ["`", "unquote"], ["get", ["nth", "ast", 0], ["`", "val"]]], + ["nth", "ast", 1], + ["if", ["and", ["pair?", ["nth", "ast", 0]], + ["=", ["`", "splice-unquote"], + ["get", ["nth", ["nth", "ast", 0], 0], ["`", "val"]]]], + ["list", ["symbol", ["`", "concat"]], + ["nth", ["nth", "ast", 0], 1], + ["quasiquote", ["rest", "ast"]]], + ["list", ["symbol", ["`", "cons"]], + ["quasiquote", ["nth", "ast", 0]], + ["quasiquote", ["rest", "ast"]]]]]]]], + +["def", "macro?", ["fn", ["ast", "env"], + ["and", ["list?", "ast"], + ["symbol?", ["first", "ast"]], + ["not", ["=", null, ["env-find", "env", ["first", "ast"]]]], + ["let", ["fn", ["env-get", "env", ["first", "ast"]]], + ["and", ["malfunc?", "fn"], + ["get", "fn", ["`", "macro?"]]]]]]], + +["def", "macroexpand", ["fn", ["ast", "env"], + ["if", ["macro?", "ast", "env"], + ["let", ["mac", ["get", ["env-get", "env", ["first", "ast"]], ["`", "fn"]]], + ["macroexpand", ["apply", "mac", ["rest", "ast"]], "env"]], + "ast"]]], + +["def", "eval-ast", ["fn", ["ast", "env"], + ["if", ["symbol?", "ast"], + ["env-get", "env", "ast"], + ["if", ["list?", "ast"], + ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], + ["if", ["vector?", "ast"], + ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], + ["if", ["map?", "ast"], + ["let", ["new-hm", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], ["set", "new-hm", + ["EVAL", "k", "env"], + ["EVAL", ["get", "ast", "k"], "env"]]], + ["keys", "ast"]], + "new-hm"]], + "ast"]]]]]], + +["def", "LET", ["fn", ["env", "args"], + ["if", [">", ["count", "args"], 0], + ["do", + ["env-set", "env", ["nth", "args", 0], + ["EVAL", ["nth", "args", 1], "env"]], + ["LET", "env", ["rest", ["rest", "args"]]]]]]], + +["def", "EVAL", ["fn", ["ast", "env"], + ["if", ["not", ["list?", "ast"]], + ["eval-ast", "ast", "env"], + ["let", ["ast", ["macroexpand", "ast", "env"]], + ["if", ["not", ["list?", "ast"]], + "ast", + ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]], + ["if", ["=", ["`", "def!"], "a0"], + ["env-set", "env", ["nth", "ast", 1], + ["EVAL", ["nth", "ast", 2], "env"]], + ["if", ["=", ["`", "let*"], "a0"], + ["let", ["let-env", ["env-new", "env"]], + ["do", + ["LET", "let-env", ["nth", "ast", 1]], + ["EVAL", ["nth", "ast", 2], "let-env"]]], + ["if", ["=", ["`", "quote"], "a0"], + ["nth", "ast", 1], + ["if", ["=", ["`", "quasiquote"], "a0"], + ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"], + ["if", ["=", ["`", "defmacro!"], "a0"], + ["let", ["func", ["EVAL", ["nth", "ast", 2], "env"]], + ["do", + ["set", "func", ["`", "macro?"], true], + ["env-set", "env", ["nth", "ast", 1], "func"]]], + ["if", ["=", ["`", "macroexpand"], "a0"], + ["macroexpand", ["nth", "ast", 1], "env"], + ["if", ["=", ["`", "do"], "a0"], + ["do", + ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"], + ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]], + ["if", ["=", ["`", "if"], "a0"], + ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]], + ["if", ["or", ["=", "cond", null], ["=", "cond", false]], + ["if", [">", ["count", "ast"], 3], + ["EVAL", ["nth", "ast", 3], "env"], + null], + ["EVAL", ["nth", "ast", 2], "env"]]], + ["if", ["=", ["`", "fn*"], "a0"], + ["malfunc", + ["fn", ["&", "args"], + ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]], + ["EVAL", ["nth", "ast", 2], "e"]]], + ["nth", "ast", 2], "env", ["nth", "ast", 1]], + ["let", ["el", ["eval-ast", "ast", "env"], + "f", ["first", "el"], + "args", ["rest", "el"]], + ["if", ["malfunc?", "f"], + ["EVAL", ["get", "f", ["`", "ast"]], + ["env-new", ["get", "f", ["`", "env"]], + ["get", "f", ["`", "params"]], + "args"]], + ["apply", "f", "args"]]]]]]]]]]]]]]]]]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + + +["def", "repl-env", ["env-new"]], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], + ["catch", "exc", + ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], + +["`", "core.mal: defined using miniMAL"], +["map", ["fn", ["k"], ["env-set", "repl-env", + ["symbol", "k"], + ["get", "core-ns", "k"]]], + ["keys", "core-ns"]], +["env-set", "repl-env", ["symbol", ["`", "eval"]], + ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]], +["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]], + ["slice", "*ARGV*", 1]], + +["`", "core.mal: defined using mal itself"], +["rep", ["`", "(def! not (fn* (a) (if a false true)))"]], +["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]], +["rep", ["`", "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"]], +["rep", ["`", "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"]], + +["if", ["not", ["empty?", "*ARGV*"]], + ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]], + ["repl", ["`", "user> "], "rep"]], + +null + +] diff --git a/miniMAL/step9_try.json b/miniMAL/step9_try.json new file mode 100644 index 0000000..585a1b0 --- /dev/null +++ b/miniMAL/step9_try.json @@ -0,0 +1,168 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], +["load-file", ["`", "env.json"]], +["load-file", ["`", "core.json"]], + +["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]], + +["def", "pair?", ["fn", ["x"], + ["if", ["sequential?", "x"], + ["if", [">", ["count", "x"], 0], true, false], + false]]], + +["def", "quasiquote", ["fn", ["ast"], + ["if", ["not", ["pair?", "ast"]], + ["list", ["symbol", ["`", "quote"]], "ast"], + ["if", ["=", ["`", "unquote"], ["get", ["nth", "ast", 0], ["`", "val"]]], + ["nth", "ast", 1], + ["if", ["and", ["pair?", ["nth", "ast", 0]], + ["=", ["`", "splice-unquote"], + ["get", ["nth", ["nth", "ast", 0], 0], ["`", "val"]]]], + ["list", ["symbol", ["`", "concat"]], + ["nth", ["nth", "ast", 0], 1], + ["quasiquote", ["rest", "ast"]]], + ["list", ["symbol", ["`", "cons"]], + ["quasiquote", ["nth", "ast", 0]], + ["quasiquote", ["rest", "ast"]]]]]]]], + +["def", "macro?", ["fn", ["ast", "env"], + ["and", ["list?", "ast"], + ["symbol?", ["first", "ast"]], + ["not", ["=", null, ["env-find", "env", ["first", "ast"]]]], + ["let", ["fn", ["env-get", "env", ["first", "ast"]]], + ["and", ["malfunc?", "fn"], + ["get", "fn", ["`", "macro?"]]]]]]], + +["def", "macroexpand", ["fn", ["ast", "env"], + ["if", ["macro?", "ast", "env"], + ["let", ["mac", ["get", ["env-get", "env", ["first", "ast"]], ["`", "fn"]]], + ["macroexpand", ["apply", "mac", ["rest", "ast"]], "env"]], + "ast"]]], + +["def", "eval-ast", ["fn", ["ast", "env"], + ["if", ["symbol?", "ast"], + ["env-get", "env", "ast"], + ["if", ["list?", "ast"], + ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], + ["if", ["vector?", "ast"], + ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], + ["if", ["map?", "ast"], + ["let", ["new-hm", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], ["set", "new-hm", + ["EVAL", "k", "env"], + ["EVAL", ["get", "ast", "k"], "env"]]], + ["keys", "ast"]], + "new-hm"]], + "ast"]]]]]], + +["def", "LET", ["fn", ["env", "args"], + ["if", [">", ["count", "args"], 0], + ["do", + ["env-set", "env", ["nth", "args", 0], + ["EVAL", ["nth", "args", 1], "env"]], + ["LET", "env", ["rest", ["rest", "args"]]]]]]], + +["def", "EVAL", ["fn", ["ast", "env"], + ["if", ["not", ["list?", "ast"]], + ["eval-ast", "ast", "env"], + ["let", ["ast", ["macroexpand", "ast", "env"]], + ["if", ["not", ["list?", "ast"]], + "ast", + ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]], + ["if", ["=", ["`", "def!"], "a0"], + ["env-set", "env", ["nth", "ast", 1], + ["EVAL", ["nth", "ast", 2], "env"]], + ["if", ["=", ["`", "let*"], "a0"], + ["let", ["let-env", ["env-new", "env"]], + ["do", + ["LET", "let-env", ["nth", "ast", 1]], + ["EVAL", ["nth", "ast", 2], "let-env"]]], + ["if", ["=", ["`", "quote"], "a0"], + ["nth", "ast", 1], + ["if", ["=", ["`", "quasiquote"], "a0"], + ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"], + ["if", ["=", ["`", "defmacro!"], "a0"], + ["let", ["func", ["EVAL", ["nth", "ast", 2], "env"]], + ["do", + ["set", "func", ["`", "macro?"], true], + ["env-set", "env", ["nth", "ast", 1], "func"]]], + ["if", ["=", ["`", "macroexpand"], "a0"], + ["macroexpand", ["nth", "ast", 1], "env"], + ["if", ["=", ["`", "try*"], "a0"], + ["if", ["=", ["`", "catch*"], + ["get", ["nth", ["nth", "ast", 2], 0], ["`", "val"]]], + ["try", + ["EVAL", ["nth", "ast", 1], "env"], + ["catch", "exc", + ["EVAL", ["nth", ["nth", "ast", 2], 2], + ["env-new", "env", + ["list", ["nth", ["nth", "ast", 2], 1]], + ["list", "exc"]]]]], + ["EVAL", ["nth", "ast", 1], "env"]], + ["if", ["=", ["`", "do"], "a0"], + ["do", + ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"], + ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]], + ["if", ["=", ["`", "if"], "a0"], + ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]], + ["if", ["or", ["=", "cond", null], ["=", "cond", false]], + ["if", [">", ["count", "ast"], 3], + ["EVAL", ["nth", "ast", 3], "env"], + null], + ["EVAL", ["nth", "ast", 2], "env"]]], + ["if", ["=", ["`", "fn*"], "a0"], + ["malfunc", + ["fn", ["&", "args"], + ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]], + ["EVAL", ["nth", "ast", 2], "e"]]], + ["nth", "ast", 2], "env", ["nth", "ast", 1]], + ["let", ["el", ["eval-ast", "ast", "env"], + "f", ["first", "el"], + "args", ["rest", "el"]], + ["if", ["malfunc?", "f"], + ["EVAL", ["get", "f", ["`", "ast"]], + ["env-new", ["get", "f", ["`", "env"]], + ["get", "f", ["`", "params"]], + "args"]], + ["apply", "f", "args"]]]]]]]]]]]]]]]]]]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + + +["def", "repl-env", ["env-new"]], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], + ["catch", "exc", + ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], + +["`", "core.mal: defined using miniMAL"], +["map", ["fn", ["k"], ["env-set", "repl-env", + ["symbol", "k"], + ["get", "core-ns", "k"]]], + ["keys", "core-ns"]], +["env-set", "repl-env", ["symbol", ["`", "eval"]], + ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]], +["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]], + ["slice", "*ARGV*", 1]], + +["`", "core.mal: defined using mal itself"], +["rep", ["`", "(def! not (fn* (a) (if a false true)))"]], +["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]], +["rep", ["`", "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"]], +["rep", ["`", "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"]], + +["if", ["not", ["empty?", "*ARGV*"]], + ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]], + ["repl", ["`", "user> "], "rep"]], + +null + +] diff --git a/miniMAL/stepA_mal.json b/miniMAL/stepA_mal.json new file mode 100644 index 0000000..2181d30 --- /dev/null +++ b/miniMAL/stepA_mal.json @@ -0,0 +1,171 @@ +["do", + +["load-file", ["`", "miniMAL-core.json"]], +["load-file", ["`", "types.json"]], +["load-file", ["`", "reader.json"]], +["load-file", ["`", "printer.json"]], +["load-file", ["`", "env.json"]], +["load-file", ["`", "core.json"]], + +["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]], + +["def", "pair?", ["fn", ["x"], + ["if", ["sequential?", "x"], + ["if", [">", ["count", "x"], 0], true, false], + false]]], + +["def", "quasiquote", ["fn", ["ast"], + ["if", ["not", ["pair?", "ast"]], + ["list", ["symbol", ["`", "quote"]], "ast"], + ["if", ["=", ["`", "unquote"], ["get", ["nth", "ast", 0], ["`", "val"]]], + ["nth", "ast", 1], + ["if", ["and", ["pair?", ["nth", "ast", 0]], + ["=", ["`", "splice-unquote"], + ["get", ["nth", ["nth", "ast", 0], 0], ["`", "val"]]]], + ["list", ["symbol", ["`", "concat"]], + ["nth", ["nth", "ast", 0], 1], + ["quasiquote", ["rest", "ast"]]], + ["list", ["symbol", ["`", "cons"]], + ["quasiquote", ["nth", "ast", 0]], + ["quasiquote", ["rest", "ast"]]]]]]]], + +["def", "macro?", ["fn", ["ast", "env"], + ["and", ["list?", "ast"], + ["symbol?", ["first", "ast"]], + ["not", ["=", null, ["env-find", "env", ["first", "ast"]]]], + ["let", ["fn", ["env-get", "env", ["first", "ast"]]], + ["and", ["malfunc?", "fn"], + ["get", "fn", ["`", "macro?"]]]]]]], + +["def", "macroexpand", ["fn", ["ast", "env"], + ["if", ["macro?", "ast", "env"], + ["let", ["mac", ["get", ["env-get", "env", ["first", "ast"]], ["`", "fn"]]], + ["macroexpand", ["apply", "mac", ["rest", "ast"]], "env"]], + "ast"]]], + +["def", "eval-ast", ["fn", ["ast", "env"], + ["if", ["symbol?", "ast"], + ["env-get", "env", "ast"], + ["if", ["list?", "ast"], + ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"], + ["if", ["vector?", "ast"], + ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], + ["if", ["map?", "ast"], + ["let", ["new-hm", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], ["set", "new-hm", + ["EVAL", "k", "env"], + ["EVAL", ["get", "ast", "k"], "env"]]], + ["keys", "ast"]], + "new-hm"]], + "ast"]]]]]], + +["def", "LET", ["fn", ["env", "args"], + ["if", [">", ["count", "args"], 0], + ["do", + ["env-set", "env", ["nth", "args", 0], + ["EVAL", ["nth", "args", 1], "env"]], + ["LET", "env", ["rest", ["rest", "args"]]]]]]], + +["def", "EVAL", ["fn", ["ast", "env"], + ["if", ["not", ["list?", "ast"]], + ["eval-ast", "ast", "env"], + ["let", ["ast", ["macroexpand", "ast", "env"]], + ["if", ["not", ["list?", "ast"]], + "ast", + ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]], + ["if", ["=", ["`", "def!"], "a0"], + ["env-set", "env", ["nth", "ast", 1], + ["EVAL", ["nth", "ast", 2], "env"]], + ["if", ["=", ["`", "let*"], "a0"], + ["let", ["let-env", ["env-new", "env"]], + ["do", + ["LET", "let-env", ["nth", "ast", 1]], + ["EVAL", ["nth", "ast", 2], "let-env"]]], + ["if", ["=", ["`", "quote"], "a0"], + ["nth", "ast", 1], + ["if", ["=", ["`", "quasiquote"], "a0"], + ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"], + ["if", ["=", ["`", "defmacro!"], "a0"], + ["let", ["func", ["EVAL", ["nth", "ast", 2], "env"]], + ["do", + ["set", "func", ["`", "macro?"], true], + ["env-set", "env", ["nth", "ast", 1], "func"]]], + ["if", ["=", ["`", "macroexpand"], "a0"], + ["macroexpand", ["nth", "ast", 1], "env"], + ["if", ["=", ["`", "try*"], "a0"], + ["if", ["=", ["`", "catch*"], + ["get", ["nth", ["nth", "ast", 2], 0], ["`", "val"]]], + ["try", + ["EVAL", ["nth", "ast", 1], "env"], + ["catch", "exc", + ["EVAL", ["nth", ["nth", "ast", 2], 2], + ["env-new", "env", + ["list", ["nth", ["nth", "ast", 2], 1]], + ["list", "exc"]]]]], + ["EVAL", ["nth", "ast", 1], "env"]], + ["if", ["=", ["`", "do"], "a0"], + ["do", + ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"], + ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]], + ["if", ["=", ["`", "if"], "a0"], + ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]], + ["if", ["or", ["=", "cond", null], ["=", "cond", false]], + ["if", [">", ["count", "ast"], 3], + ["EVAL", ["nth", "ast", 3], "env"], + null], + ["EVAL", ["nth", "ast", 2], "env"]]], + ["if", ["=", ["`", "fn*"], "a0"], + ["malfunc", + ["fn", ["&", "args"], + ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]], + ["EVAL", ["nth", "ast", 2], "e"]]], + ["nth", "ast", 2], "env", ["nth", "ast", 1]], + ["let", ["el", ["eval-ast", "ast", "env"], + "f", ["first", "el"], + "args", ["rest", "el"]], + ["if", ["malfunc?", "f"], + ["EVAL", ["get", "f", ["`", "ast"]], + ["env-new", ["get", "f", ["`", "env"]], + ["get", "f", ["`", "params"]], + "args"]], + ["apply", "f", "args"]]]]]]]]]]]]]]]]]]], + +["def", "PRINT", ["fn", ["exp"], + ["pr-str", "exp", true]]], + + +["def", "repl-env", ["env-new"]], + +["def", "rep", ["fn", ["strng"], + ["try", + ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], + ["catch", "exc", + ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], + +["`", "core.mal: defined using miniMAL"], +["map", ["fn", ["k"], ["env-set", "repl-env", + ["symbol", "k"], + ["get", "core-ns", "k"]]], + ["keys", "core-ns"]], +["env-set", "repl-env", ["symbol", ["`", "eval"]], + ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]], +["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]], + ["slice", "*ARGV*", 1]], + +["`", "core.mal: defined using mal itself"], +["rep", ["`", "(def! *host-language* \"miniMAL\")"]], +["rep", ["`", "(def! not (fn* (a) (if a false true)))"]], +["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]], +["rep", ["`", "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"]], +["rep", ["`", "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"]], + +["if", ["not", ["empty?", "*ARGV*"]], + ["println", ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]]], + ["do", + ["rep", ["`", "(println (str \"Mal [\" *host-language* \"]\"))"]], + ["repl", ["`", "user> "], "rep"]]], + +null + +] diff --git a/miniMAL/types.json b/miniMAL/types.json new file mode 100644 index 0000000..372dff9 --- /dev/null +++ b/miniMAL/types.json @@ -0,0 +1,145 @@ +["do", + +["`", "Utility Functions"], +["def", "_cmp_seqs", ["fn", ["a", "b"], + ["if", ["not", ["=", ["count", "a"], ["count", "b"]]], + false, + ["if", ["empty?", "a"], + true, + ["if", ["equal?", ["get", "a", 0], ["get", "b", 0]], + ["_cmp_seqs", ["rest", "a"], ["rest", "b"]], + false]]]]], + +["def", "equal?", ["fn", ["a", "b"], + ["if", ["sequential?", "a"], + ["if", ["sequential?", "b"], + ["_cmp_seqs", "a", "b"], + false], + ["if", ["symbol?", "a"], + ["=", ["get", "a", ["`", "val"]], ["get", "b", ["`", "val"]]], + ["=", "a", "b"]]]]], + +["def", "_clone", ["fn", ["obj"], + ["if", ["list?", "obj"], + ["slice", "obj", 0], + ["if", ["vector?", "obj"], + ["let", ["new-obj", ["slice", "obj", 0]], + ["do", + ["set", "new-obj", ["`", "__vector?__"], true], + "new-obj"]], + ["if", ["map?", "obj"], + ["let", ["new-obj", ["hash-map"]], + ["do", + ["map", ["fn", ["k"], + ["if", [".", "obj", ["`", "hasOwnProperty"], "k"], + ["set", "new-obj", "k", ["get", "obj", "k"]], + null]], + ["keys", "obj"]], + "new-obj"]], + ["if", ["malfunc?", "obj"], + ["let", ["new-obj", ["malfunc", ["get", "obj", ["`", "fn"]], + ["get", "obj", ["`", "ast"]], + ["get", "obj", ["`", "env"]], + ["get", "obj", ["`", "params"]]]], + ["do", + ["set", "new-obj", ["`", "macro?"], ["get", "obj", ["`", "macro?"]]], + ["set", "new-obj", ["`", "__meta__"], ["get", "obj", ["`", "__meta__"]]], + "new-obj"]], + ["throw", "clone of unsupported type"]]]]]]], + +["def", "clone", ["fn", ["obj"], + ["let", ["new-obj", ["_clone", "obj"]], + ["do", + [".", "Object", ["`", "defineProperty"], "new-obj", ["`", "__meta__"], + {"enumerable": false, "writable": true}], + "new-obj"]]]], + +["def", "assoc!", ["fn", ["a", "b", "c"], ["do", ["set", "a", "b", "c"], "a"]]], +["def", "assocs!", ["fn", ["hm", "kvs"], + ["if", ["empty?", "kvs"], + "hm", + ["do", + ["assoc!", "hm", ["get", "kvs", 0], ["get", "kvs", 1]], + ["assocs!", "hm", ["slice", "kvs", 2]]]]]], + + +["def", "Symbol", ["fn", [], null]], +["def", "symbol", ["fn", ["name"], + ["assoc!", ["new", "Symbol"], ["`", "val"], "name"]]], + +["def", "symbol?", ["fn", ["a"], + ["isa", "a", "Symbol"]]], + + +["def", "keyword", ["fn", ["name"], + ["str", ["`", "\u029e"], "name"]]], + +["def", "keyword?", ["fn", ["kw"], + ["and", ["=", ["`", "[object String]"], ["classOf", "kw"]], + ["=", ["`", "\u029e"], ["get", "kw", 0]]]]], + + +["`", "Override some list defs to account for Vectors"], +["def", "sequential?", ["fn", ["a"], + [".", "Array", ["`", "isArray"], "a"]]], + +["def", "list?", ["fn", ["a"], + ["if", [".", "Array", ["`", "isArray"], "a"], + ["if", [".-", "a", ["`", "__vector?__"]], + false, + true], + false]]], + +["def", "empty?", ["fn", ["a"], + ["if", ["sequential?", "a"], + ["if", ["=", 0, [".-", "a", ["`", "length"]]], + true, + false], + ["=", "a", null]]]], + + +["def", "vectorl", ["fn", ["lst"], + ["let", ["vec", ["slice", "lst", 0]], + ["do", + ["set", "vec", ["`", "__vector?__"], true], + "vec"]]]], + +["def", "vector", ["fn", ["&", "args"], ["vectorl", "args"]]], + +["def", "vector?", ["fn", ["a"], + ["if", [".", "Array", ["`", "isArray"], "a"], + ["if", [".-", "a", ["`", "__vector?__"]], + true, + false], + false]]], + + +["def", "HashMap", ["fn", [], null]], +["def", "hash-map", ["fn", ["&", "a"], + ["assocs!", ["new", "HashMap"], "a"]]], +["def", "map?", ["fn", ["a"], + ["isa", "a", "HashMap"]]], + +["def", "MalFunc", ["fn", [], null]], +["def", "malfunc", ["fn", ["fn", "ast", "env", "params"], + ["assocs!", ["new", "MalFunc"], + ["list", ["`", "fn"], "fn", + ["`", "ast"], "ast", + ["`", "env"], "env", + ["`", "params"], "params", + ["`", "macro?"], false]]]], + +["def", "malfunc?", ["fn", ["a"], + ["isa", "a", "MalFunc"]]], + +["def", "Atom", ["fn", [], null]], +["def", "atom", ["fn", ["a"], + ["let", ["atm", ["new", "Atom"]], + ["do", + ["set", "atm", ["`", "val"], "a"], + "atm"]]]], +["def", "atom?", ["fn", ["a"], + ["isa", "a", "Atom"]]], + +null +] |
