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