diff options
| author | Joel Martin <github@martintribe.org> | 2015-02-15 16:34:55 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-02-15 16:34:55 -0600 |
| commit | c5d302358e0b519f9de6d570158d07d60fda6d97 (patch) | |
| tree | 4705f31f4485a1b9501a08dac5728beef08a352c | |
| parent | 7ebf5219e1be21ed10cecabda8262c77da7465ae (diff) | |
| download | mal-c5d302358e0b519f9de6d570158d07d60fda6d97.tar.gz mal-c5d302358e0b519f9de6d570158d07d60fda6d97.zip | |
miniMAL: step4.
| -rw-r--r-- | miniMAL/core.json | 40 | ||||
| -rw-r--r-- | miniMAL/printer.json | 17 | ||||
| -rw-r--r-- | miniMAL/reader.json | 6 | ||||
| -rw-r--r-- | miniMAL/step4_if_fn_do.json | 15 | ||||
| -rw-r--r-- | miniMAL/types.json | 16 |
5 files changed, 84 insertions, 10 deletions
diff --git a/miniMAL/core.json b/miniMAL/core.json new file mode 100644 index 0000000..4717f77 --- /dev/null +++ b/miniMAL/core.json @@ -0,0 +1,40 @@ +["do", + +["def", "div", ["fn", ["a", "b"], ["parseInt", ["/", "a", "b"]]]], + +["def", "_count", ["fn", ["a"], + ["if", ["=", null, "a"], + 0, + ["count", "a"]]]], + +["def", "core-ns", + ["hash-map", + ["`", "="], "equal?", + + ["`", "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]], + + ["`", "<"], "<", + ["`", "<="], "<=", + ["`", ">"], ">", + ["`", ">="], ">=", + ["`", "+"], "+", + ["`", "-"], "-", + ["`", "*"], "*", + ["`", "/"], "div", + + ["`", "list"], "list", + ["`", "list?"], "list?", + + ["`", "empty?"], "empty?", + ["`", "count"], "_count"]], + +null] diff --git a/miniMAL/printer.json b/miniMAL/printer.json index 99f4198..8029413 100644 --- a/miniMAL/printer.json +++ b/miniMAL/printer.json @@ -8,9 +8,18 @@ ["`", "join"], ["`", " "]], ["`", ")"]], ["if", ["=", ["`", "string"], ["type", "exp"]], - ["str", ["`", "\""], "exp", ["`", "\""]], + ["if", "print_readably", + ["str", ["`", "\""], + [".", + [".", + [".", "exp", + ["`", "replace"], ["RegExp", ["`", "\\\\"], ["`", "g"]], ["`", "\\\\"]], + ["`", "replace"], ["RegExp", ["`", "\""], ["`", "g"]], ["`", "\\\""]], + ["`", "replace"], ["RegExp", ["`", "\n"], ["`", "g"]], ["`", "\\n"]], + ["`", "\""]], + "exp"], ["if", ["=", ["`", "number"], ["type", "exp"]], - "exp", + "exp", ["if", ["=", null, "exp"], ["`", "nil"], ["if", ["=", true, "exp"], @@ -21,5 +30,9 @@ ["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 index 4e25b59..7058558 100644 --- a/miniMAL/reader.json +++ b/miniMAL/reader.json @@ -33,7 +33,11 @@ ["if", [".", "token", ["`", "match"], ["RegExp", ["`", "^-?[0-9]+$"]]], ["parseInt", "token", 10], ["if", ["=", ["`", "\""], ["get", "token", 0]], - ["slice", "token", 1, ["-", ["count", "token"], 1]], + [".", + [".", + ["slice", "token", 1, ["-", ["count", "token"], 1]], + ["`", "replace"], ["RegExp", ["`", "\\\\\""], ["`", "g"]], ["`", "\""]], + ["`", "replace"], ["RegExp", ["`", "\\\\n"], ["`", "g"]], ["`", "\n"]], ["if", ["=", ["`", "nil"], "token"], null, ["if", ["=", ["`", "true"], "token"], diff --git a/miniMAL/step4_if_fn_do.json b/miniMAL/step4_if_fn_do.json index 93c0929..1a9e5f1 100644 --- a/miniMAL/step4_if_fn_do.json +++ b/miniMAL/step4_if_fn_do.json @@ -5,9 +5,9 @@ ["load-file", ["`", "reader.json"]], ["load-file", ["`", "printer.json"]], ["load-file", ["`", "env.json"]], +["load-file", ["`", "core.json"]], -["def", "READ", ["fn", ["strng"], - ["read-str", "strng"]]], +["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]], ["def", "eval-ast", ["fn", ["ast", "env"], ["if", ["symbol?", "ast"], @@ -66,12 +66,13 @@ ["catch", "exc", ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], -["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"], +["`", "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"], diff --git a/miniMAL/types.json b/miniMAL/types.json index 37cc50e..9e19464 100644 --- a/miniMAL/types.json +++ b/miniMAL/types.json @@ -11,5 +11,21 @@ true, false]]]], +["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", ["list?", "a"], + ["if", ["list?", "b"], + ["_cmp_seqs", "a", "b"], + false], + ["=", "a", "b"]]]], + null ] |
