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