miniMAL: stepA. Comments. Add impl Makefile for stats.
[jackhill/mal.git] / miniMAL / step9_try.json
CommitLineData
2774a151
JM
1["do",
2
3["load-file", ["`", "miniMAL-core.json"]],
4["load-file", ["`", "types.json"]],
5["load-file", ["`", "reader.json"]],
6["load-file", ["`", "printer.json"]],
7["load-file", ["`", "env.json"]],
8["load-file", ["`", "core.json"]],
9
10["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]],
11
12["def", "pair?", ["fn", ["x"],
13 ["if", ["list?", "x"],
14 ["if", [">", ["count", "x"], 0], true, false],
15 false]]],
16
17["def", "quasiquote", ["fn", ["ast"],
18 ["if", ["not", ["pair?", "ast"]],
19 ["list", ["symbol", ["`", "quote"]], "ast"],
20 ["if", ["=", ["`", "unquote"], ["get", ["nth", "ast", 0], ["`", "val"]]],
21 ["nth", "ast", 1],
22 ["if", ["and", ["pair?", ["nth", "ast", 0]],
23 ["=", ["`", "splice-unquote"],
24 ["get", ["nth", ["nth", "ast", 0], 0], ["`", "val"]]]],
25 ["list", ["symbol", ["`", "concat"]],
26 ["nth", ["nth", "ast", 0], 1],
27 ["quasiquote", ["rest", "ast"]]],
28 ["list", ["symbol", ["`", "cons"]],
29 ["quasiquote", ["nth", "ast", 0]],
30 ["quasiquote", ["rest", "ast"]]]]]]]],
31
32["def", "macro?", ["fn", ["ast", "env"],
33 ["and", ["list?", "ast"],
34 ["symbol?", ["first", "ast"]],
35 ["not", ["=", null, ["env-find", "env", ["first", "ast"]]]],
36 ["let", ["fn", ["env-get", "env", ["first", "ast"]]],
37 ["and", ["malfunc?", "fn"],
38 ["get", "fn", ["`", "macro?"]]]]]]],
39
40["def", "macroexpand", ["fn", ["ast", "env"],
41 ["if", ["macro?", "ast", "env"],
42 ["let", ["mac", ["get", ["env-get", "env", ["first", "ast"]], ["`", "fn"]]],
43 ["macroexpand", ["apply", "mac", ["rest", "ast"]], "env"]],
44 "ast"]]],
45
46["def", "eval-ast", ["fn", ["ast", "env"],
47 ["if", ["symbol?", "ast"],
48 ["env-get", "env", "ast"],
49 ["if", ["list?", "ast"],
50 ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"],
51 "ast"]]]],
52
53["def", "LET", ["fn", ["env", "args"],
54 ["if", [">", ["count", "args"], 0],
55 ["do",
56 ["env-set", "env", ["nth", "args", 0],
57 ["EVAL", ["nth", "args", 1], "env"]],
58 ["LET", "env", ["rest", ["rest", "args"]]]]]]],
59
60["def", "EVAL", ["fn", ["ast", "env"],
61 ["if", ["not", ["list?", "ast"]],
62 ["eval-ast", "ast", "env"],
63 ["let", ["ast", ["macroexpand", "ast", "env"]],
64 ["if", ["not", ["list?", "ast"]],
65 "ast",
66 ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]],
67 ["if", ["=", ["`", "def!"], "a0"],
68 ["env-set", "env", ["nth", "ast", 1],
69 ["EVAL", ["nth", "ast", 2], "env"]],
70 ["if", ["=", ["`", "let*"], "a0"],
71 ["let", ["let-env", ["env-new", "env"]],
72 ["do",
73 ["LET", "let-env", ["nth", "ast", 1]],
74 ["EVAL", ["nth", "ast", 2], "let-env"]]],
75 ["if", ["=", ["`", "quote"], "a0"],
76 ["nth", "ast", 1],
77 ["if", ["=", ["`", "quasiquote"], "a0"],
78 ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"],
79 ["if", ["=", ["`", "defmacro!"], "a0"],
80 ["let", ["func", ["EVAL", ["nth", "ast", 2], "env"]],
81 ["do",
82 ["set", "func", ["`", "macro?"], true],
83 ["env-set", "env", ["nth", "ast", 1], "func"]]],
84 ["if", ["=", ["`", "macroexpand"], "a0"],
85 ["macroexpand", ["nth", "ast", 1], "env"],
86 ["if", ["=", ["`", "try*"], "a0"],
87 ["if", ["=", ["`", "catch*"],
88 ["get", ["nth", ["nth", "ast", 2], 0], ["`", "val"]]],
89 ["try",
90 ["EVAL", ["nth", "ast", 1], "env"],
91 ["catch", "exc",
92 ["EVAL", ["nth", ["nth", "ast", 2], 2],
93 ["env-new", "env",
94 ["list", ["nth", ["nth", "ast", 2], 1]],
95 ["list", "exc"]]]]],
96 ["EVAL", ["nth", "ast", 1], "env"]],
97 ["if", ["=", ["`", "do"], "a0"],
98 ["do",
99 ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"],
100 ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]],
101 ["if", ["=", ["`", "if"], "a0"],
102 ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]],
103 ["if", ["or", ["=", "cond", null], ["=", "cond", false]],
104 ["if", [">", ["count", "ast"], 3],
105 ["EVAL", ["nth", "ast", 3], "env"],
106 null],
107 ["EVAL", ["nth", "ast", 2], "env"]]],
108 ["if", ["=", ["`", "fn*"], "a0"],
109 ["malfunc",
110 ["fn", ["&", "args"],
111 ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]],
112 ["EVAL", ["nth", "ast", 2], "e"]]],
113 ["nth", "ast", 2], "env", ["nth", "ast", 1]],
114 ["let", ["el", ["eval-ast", "ast", "env"],
115 "f", ["first", "el"],
116 "args", ["rest", "el"]],
117 ["if", ["malfunc?", "f"],
118 ["EVAL", ["get", "f", ["`", "ast"]],
119 ["env-new", ["get", "f", ["`", "env"]],
120 ["get", "f", ["`", "params"]],
121 "args"]],
122 ["apply", "f", "args"]]]]]]]]]]]]]]]]]]],
123
124["def", "PRINT", ["fn", ["exp"],
125 ["pr-str", "exp", true]]],
126
127
128["def", "repl-env", ["env-new"]],
129
130["def", "rep", ["fn", ["strng"],
131 ["try",
132 ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]],
133 ["catch", "exc",
134 ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]],
135
136["`", "core.mal: defined using miniMAL"],
137["map", ["fn", ["k"], ["env-set", "repl-env",
138 ["symbol", "k"],
139 ["get", "core-ns", "k"]]],
140 ["keys", "core-ns"]],
141["env-set", "repl-env", ["symbol", ["`", "eval"]],
142 ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]],
143["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]],
144 ["slice", "*ARGV*", 1]],
145
146["`", "core.mal: defined using mal itself"],
147["rep", ["`", "(def! not (fn* (a) (if a false true)))"]],
148["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]],
149["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)))))))"]],
150["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))))))))"]],
151
152["if", ["not", ["empty?", "*ARGV*"]],
153 ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]],
154 ["repl", ["`", "user> "], "rep"]],
155
156null
157
158]