js: Fix exception in `(nil)
[jackhill/mal.git] / miniMAL / step8_macros.json
CommitLineData
f618f6a1
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"],
32045546 13 ["if", ["sequential?", "x"],
f618f6a1
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"],
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"],
32045546
JM
51 ["if", ["vector?", "ast"],
52 ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]],
53 ["if", ["map?", "ast"],
54 ["let", ["new-hm", ["hash-map"]],
55 ["do",
56 ["map", ["fn", ["k"], ["set", "new-hm",
57 ["EVAL", "k", "env"],
58 ["EVAL", ["get", "ast", "k"], "env"]]],
59 ["keys", "ast"]],
60 "new-hm"]],
61 "ast"]]]]]],
f618f6a1
JM
62
63["def", "LET", ["fn", ["env", "args"],
64 ["if", [">", ["count", "args"], 0],
65 ["do",
66 ["env-set", "env", ["nth", "args", 0],
67 ["EVAL", ["nth", "args", 1], "env"]],
68 ["LET", "env", ["rest", ["rest", "args"]]]]]]],
69
70["def", "EVAL", ["fn", ["ast", "env"],
71 ["if", ["not", ["list?", "ast"]],
72 ["eval-ast", "ast", "env"],
73 ["let", ["ast", ["macroexpand", "ast", "env"]],
74 ["if", ["not", ["list?", "ast"]],
75 "ast",
76 ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]],
77 ["if", ["=", ["`", "def!"], "a0"],
78 ["env-set", "env", ["nth", "ast", 1],
79 ["EVAL", ["nth", "ast", 2], "env"]],
80 ["if", ["=", ["`", "let*"], "a0"],
81 ["let", ["let-env", ["env-new", "env"]],
82 ["do",
83 ["LET", "let-env", ["nth", "ast", 1]],
84 ["EVAL", ["nth", "ast", 2], "let-env"]]],
85 ["if", ["=", ["`", "quote"], "a0"],
86 ["nth", "ast", 1],
87 ["if", ["=", ["`", "quasiquote"], "a0"],
88 ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"],
89 ["if", ["=", ["`", "defmacro!"], "a0"],
90 ["let", ["func", ["EVAL", ["nth", "ast", 2], "env"]],
91 ["do",
92 ["set", "func", ["`", "macro?"], true],
93 ["env-set", "env", ["nth", "ast", 1], "func"]]],
94 ["if", ["=", ["`", "macroexpand"], "a0"],
95 ["macroexpand", ["nth", "ast", 1], "env"],
96 ["if", ["=", ["`", "do"], "a0"],
97 ["do",
98 ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"],
99 ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]],
100 ["if", ["=", ["`", "if"], "a0"],
101 ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]],
102 ["if", ["or", ["=", "cond", null], ["=", "cond", false]],
103 ["if", [">", ["count", "ast"], 3],
104 ["EVAL", ["nth", "ast", 3], "env"],
105 null],
106 ["EVAL", ["nth", "ast", 2], "env"]]],
107 ["if", ["=", ["`", "fn*"], "a0"],
108 ["malfunc",
109 ["fn", ["&", "args"],
110 ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]],
111 ["EVAL", ["nth", "ast", 2], "e"]]],
112 ["nth", "ast", 2], "env", ["nth", "ast", 1]],
113 ["let", ["el", ["eval-ast", "ast", "env"],
114 "f", ["first", "el"],
115 "args", ["rest", "el"]],
116 ["if", ["malfunc?", "f"],
117 ["EVAL", ["get", "f", ["`", "ast"]],
118 ["env-new", ["get", "f", ["`", "env"]],
119 ["get", "f", ["`", "params"]],
120 "args"]],
121 ["apply", "f", "args"]]]]]]]]]]]]]]]]]],
122
123["def", "PRINT", ["fn", ["exp"],
124 ["pr-str", "exp", true]]],
125
126
127["def", "repl-env", ["env-new"]],
128
129["def", "rep", ["fn", ["strng"],
130 ["try",
131 ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]],
132 ["catch", "exc",
133 ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]],
134
135["`", "core.mal: defined using miniMAL"],
136["map", ["fn", ["k"], ["env-set", "repl-env",
137 ["symbol", "k"],
138 ["get", "core-ns", "k"]]],
139 ["keys", "core-ns"]],
140["env-set", "repl-env", ["symbol", ["`", "eval"]],
141 ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]],
142["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]],
143 ["slice", "*ARGV*", 1]],
144
145["`", "core.mal: defined using mal itself"],
146["rep", ["`", "(def! not (fn* (a) (if a false true)))"]],
147["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]],
148["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)))))))"]],
149["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))))))))"]],
150
151["if", ["not", ["empty?", "*ARGV*"]],
152 ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]],
153 ["repl", ["`", "user> "], "rep"]],
154
155null
156
157]