aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-02-15 17:45:04 -0600
committerJoel Martin <github@martintribe.org>2015-02-15 17:45:04 -0600
commitdc2c5b3d8f41cbae56c6fb22847d0aac5806b51f (patch)
tree3a6f481ff4ebb5b15613af480f2e83df7cb3fac0
parentc5d302358e0b519f9de6d570158d07d60fda6d97 (diff)
downloadmal-dc2c5b3d8f41cbae56c6fb22847d0aac5806b51f.tar.gz
mal-dc2c5b3d8f41cbae56c6fb22847d0aac5806b51f.zip
miniMAL: add step5.
-rw-r--r--miniMAL/printer.json8
-rw-r--r--miniMAL/step4_if_fn_do.json6
-rw-r--r--miniMAL/step5_tco.json90
-rw-r--r--miniMAL/types.json36
4 files changed, 125 insertions, 15 deletions
diff --git a/miniMAL/printer.json b/miniMAL/printer.json
index 8029413..df2ac82 100644
--- a/miniMAL/printer.json
+++ b/miniMAL/printer.json
@@ -28,7 +28,13 @@
["`", "false"],
["if", ["symbol?", "exp"],
["get", "exp", ["`", "val"]],
- ["str", ["`", "#<unknown: "], "exp", ["`", ">"]]]]]]]]]]],
+ ["if", ["malfunc?", "exp"],
+ ["str", ["`", "(fn* "],
+ ["pr-str", ["get", "exp", ["`", "params"]]],
+ ["`", " "],
+ ["pr-str", ["get", "exp", ["`", "ast"]]],
+ ["`", ")"]],
+ ["str", ["`", "#<unknown: "], "exp", ["`", ">"]]]]]]]]]]]],
["def", "pr-list", ["fn", ["lst", "print_readably", "sep"],
[".", ["map", ["fn", ["s"], ["pr-str", "s", "print_readably"]], "lst"],
diff --git a/miniMAL/step4_if_fn_do.json b/miniMAL/step4_if_fn_do.json
index 1a9e5f1..7998139 100644
--- a/miniMAL/step4_if_fn_do.json
+++ b/miniMAL/step4_if_fn_do.json
@@ -46,9 +46,9 @@
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"]]],
+ ["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"]],
diff --git a/miniMAL/step5_tco.json b/miniMAL/step5_tco.json
new file mode 100644
index 0000000..fa70848
--- /dev/null
+++ b/miniMAL/step5_tco.json
@@ -0,0 +1,90 @@
+["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"],
+ "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]]],
+ ["EVAL", ["nth", "ast", ["-", ["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"],
+ ["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/types.json b/miniMAL/types.json
index 9e19464..2e85775 100644
--- a/miniMAL/types.json
+++ b/miniMAL/types.json
@@ -1,16 +1,5 @@
["do",
-["def", "symbol", ["fn", ["name"],
- ["hash-map", ["`", "type"], ["`", "Symbol"],
- ["`", "val"], "name"]]],
-
-["def", "symbol?", ["fn", ["obj"],
- ["if", ["=", "obj", null],
- false,
- ["if", ["=", ["`", "Symbol"], ["get", "obj", ["`", "type"]]],
- true,
- false]]]],
-
["def", "_cmp_seqs", ["fn", ["a", "b"],
["if", ["not", ["=", ["count", "a"], ["count", "b"]]],
false,
@@ -27,5 +16,30 @@
false],
["=", "a", "b"]]]],
+["def", "symbol", ["fn", ["name"],
+ ["hash-map", ["`", "type"], ["`", "Symbol"],
+ ["`", "val"], "name"]]],
+
+["def", "symbol?", ["fn", ["obj"],
+ ["if", ["=", "obj", null],
+ false,
+ ["if", ["=", ["`", "Symbol"], ["get", "obj", ["`", "type"]]],
+ true,
+ false]]]],
+
+["def", "malfunc", ["fn", ["fn", "ast", "env", "params"],
+ ["hash-map", ["`", "type"], ["`", "MalFunc"],
+ ["`", "fn"], "fn",
+ ["`", "ast"], "ast",
+ ["`", "env"], "env",
+ ["`", "params"], "params"]]],
+
+["def", "malfunc?", ["fn", ["obj"],
+ ["if", ["=", "obj", null],
+ false,
+ ["if", ["=", ["`", "MalFunc"], ["get", "obj", ["`", "type"]]],
+ true,
+ false]]]],
+
null
]