miniMAL: Fix exception in `(nil)
[jackhill/mal.git] / miniMAL / step7_quote.json
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", ["sequential?", "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", ["and", ["symbol?", ["nth", "ast", 0]],
21 ["=", ["`", "unquote"], ["get", ["nth", "ast", 0], ["`", "val"]]]],
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", "eval-ast", ["fn", ["ast", "env"],
34 ["if", ["symbol?", "ast"],
35 ["env-get", "env", "ast"],
36 ["if", ["list?", "ast"],
37 ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"],
38 ["if", ["vector?", "ast"],
39 ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]],
40 ["if", ["map?", "ast"],
41 ["let", ["new-hm", ["hash-map"]],
42 ["do",
43 ["map", ["fn", ["k"], ["set", "new-hm",
44 ["EVAL", "k", "env"],
45 ["EVAL", ["get", "ast", "k"], "env"]]],
46 ["keys", "ast"]],
47 "new-hm"]],
48 "ast"]]]]]],
49
50 ["def", "LET", ["fn", ["env", "args"],
51 ["if", [">", ["count", "args"], 0],
52 ["do",
53 ["env-set", "env", ["nth", "args", 0],
54 ["EVAL", ["nth", "args", 1], "env"]],
55 ["LET", "env", ["rest", ["rest", "args"]]]]]]],
56
57 ["def", "EVAL", ["fn", ["ast", "env"],
58 ["if", ["not", ["list?", "ast"]],
59 ["eval-ast", "ast", "env"],
60 ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]],
61 ["if", ["=", ["`", "def!"], "a0"],
62 ["env-set", "env", ["nth", "ast", 1],
63 ["EVAL", ["nth", "ast", 2], "env"]],
64 ["if", ["=", ["`", "let*"], "a0"],
65 ["let", ["let-env", ["env-new", "env"]],
66 ["do",
67 ["LET", "let-env", ["nth", "ast", 1]],
68 ["EVAL", ["nth", "ast", 2], "let-env"]]],
69 ["if", ["=", ["`", "quote"], "a0"],
70 ["nth", "ast", 1],
71 ["if", ["=", ["`", "quasiquote"], "a0"],
72 ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"],
73 ["if", ["=", ["`", "do"], "a0"],
74 ["do",
75 ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"],
76 ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]],
77 ["if", ["=", ["`", "if"], "a0"],
78 ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]],
79 ["if", ["or", ["=", "cond", null], ["=", "cond", false]],
80 ["if", [">", ["count", "ast"], 3],
81 ["EVAL", ["nth", "ast", 3], "env"],
82 null],
83 ["EVAL", ["nth", "ast", 2], "env"]]],
84 ["if", ["=", ["`", "fn*"], "a0"],
85 ["malfunc",
86 ["fn", ["&", "args"],
87 ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]],
88 ["EVAL", ["nth", "ast", 2], "e"]]],
89 ["nth", "ast", 2], "env", ["nth", "ast", 1]],
90 ["let", ["el", ["eval-ast", "ast", "env"],
91 "f", ["first", "el"],
92 "args", ["rest", "el"]],
93 ["if", ["malfunc?", "f"],
94 ["EVAL", ["get", "f", ["`", "ast"]],
95 ["env-new", ["get", "f", ["`", "env"]],
96 ["get", "f", ["`", "params"]],
97 "args"]],
98 ["apply", "f", "args"]]]]]]]]]]]]]],
99
100 ["def", "PRINT", ["fn", ["exp"],
101 ["pr-str", "exp", true]]],
102
103
104 ["def", "repl-env", ["env-new"]],
105
106 ["def", "rep", ["fn", ["strng"],
107 ["try",
108 ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]],
109 ["catch", "exc",
110 ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]],
111
112 ["`", "core.mal: defined using miniMAL"],
113 ["map", ["fn", ["k"], ["env-set", "repl-env",
114 ["symbol", "k"],
115 ["get", "core-ns", "k"]]],
116 ["keys", "core-ns"]],
117 ["env-set", "repl-env", ["symbol", ["`", "eval"]],
118 ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]],
119 ["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]],
120 ["slice", "*ARGV*", 1]],
121
122 ["`", "core.mal: defined using mal itself"],
123 ["rep", ["`", "(def! not (fn* (a) (if a false true)))"]],
124 ["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]],
125
126 ["if", ["not", ["empty?", "*ARGV*"]],
127 ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]],
128 ["repl", ["`", "user> "], "rep"]],
129
130 null
131
132 ]