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"]],
10 ["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]],
12 ["def", "pair?", ["fn", ["x"],
13 ["if", ["sequential?", "x"],
14 ["if", [">", ["count", "x"], 0], true, false],
17 ["def", "quasiquote", ["fn", ["ast"],
18 ["if", ["not", ["pair?", "ast"]],
19 ["list", ["symbol", ["`", "quote"]], "ast"],
20 ["if", ["and", ["symbol?", ["nth", "ast", 0]],
21 ["=", ["`", "unquote"], ["get", ["nth", "ast", 0], ["`", "val"]]]],
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"]]]]]]]],
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?"]]]]]]],
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"]],
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"],
52 ["if", ["vector?", "ast"],
53 ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]],
54 ["if", ["map?", "ast"],
55 ["let", ["new-hm", ["hash-map"]],
57 ["map", ["fn", ["k"], ["set", "new-hm",
59 ["EVAL", ["get", "ast", "k"], "env"]]],
64 ["def", "LET", ["fn", ["env", "args"],
65 ["if", [">", ["count", "args"], 0],
67 ["env-set", "env", ["nth", "args", 0],
68 ["EVAL", ["nth", "args", 1], "env"]],
69 ["LET", "env", ["rest", ["rest", "args"]]]]]]],
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"]],
76 ["eval-ast", "ast", "env"],
77 ["if", ["empty?", "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"]],
86 ["LET", "let-env", ["nth", "ast", 1]],
87 ["EVAL", ["nth", "ast", 2], "let-env"]]],
88 ["if", ["=", ["`", "quote"], "a0"],
90 ["if", ["=", ["`", "quasiquote"], "a0"],
91 ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"],
92 ["if", ["=", ["`", "defmacro!"], "a0"],
93 ["let", ["func", ["EVAL", ["nth", "ast", 2], "env"]],
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"],
100 ["if", ["and", [">", ["count", "ast"], 2],
101 ["=", ["`", "catch*"],
102 ["get", ["nth", ["nth", "ast", 2], 0],
105 ["EVAL", ["nth", "ast", 1], "env"],
107 ["EVAL", ["nth", ["nth", "ast", 2], 2],
109 ["list", ["nth", ["nth", "ast", 2], 1]],
111 ["EVAL", ["nth", "ast", 1], "env"]],
112 ["if", ["=", ["`", "do"], "a0"],
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"],
122 ["EVAL", ["nth", "ast", 2], "env"]]],
123 ["if", ["=", ["`", "fn*"], "a0"],
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"]],
137 ["apply", "f", "args"]]]]]]]]]]]]]]]]]]]],
139 ["def", "PRINT", ["fn", ["exp"],
140 ["pr-str", "exp", true]]],
143 ["def", "repl-env", ["env-new"]],
145 ["def", "rep", ["fn", ["strng"],
147 ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]],
149 ["str", ["`", "Error: "],
150 ["if", ["isa", "exc", "Error"],
151 [".", "exc", ["`", "toString"]],
152 ["pr-str", "exc", true]]]]]]],
154 ["`", "core.mal: defined using miniMAL"],
155 ["map", ["fn", ["k"], ["env-set", "repl-env",
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*"]],
162 ["slice", "ARGS", 1]],
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) \"\nnil)\")))))"]],
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)))))))"]],
169 ["if", ["not", ["empty?", "ARGS"]],
170 ["rep", ["str", ["`", "(load-file \""], ["get", "ARGS", 0], ["`", "\")"]]],
171 ["repl", ["`", "user> "], "rep"]],