Merge pull request #420 from asarhaddon/load-file-once
[jackhill/mal.git] / miniMAL / step7_quote.json
CommitLineData
1981bf57
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"]],
1981bf57
JM
9
10["def", "READ", ["fn", ["strng"], ["read-str", "strng"]]],
11
12["def", "pair?", ["fn", ["x"],
32045546 13 ["if", ["sequential?", "x"],
1981bf57
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"]]]],
1981bf57
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", "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"],
32045546
JM
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"]]]]]],
1981bf57
JM
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"]],
f618f6a1 59 ["eval-ast", "ast", "env"],
33783b04
DM
60 ["if", ["empty?", "ast"],
61 "ast",
62 ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]],
63 ["if", ["=", ["`", "def!"], "a0"],
64 ["env-set", "env", ["nth", "ast", 1],
65 ["EVAL", ["nth", "ast", 2], "env"]],
66 ["if", ["=", ["`", "let*"], "a0"],
67 ["let", ["let-env", ["env-new", "env"]],
68 ["do",
69 ["LET", "let-env", ["nth", "ast", 1]],
70 ["EVAL", ["nth", "ast", 2], "let-env"]]],
71 ["if", ["=", ["`", "quote"], "a0"],
72 ["nth", "ast", 1],
73 ["if", ["=", ["`", "quasiquote"], "a0"],
74 ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"],
75 ["if", ["=", ["`", "do"], "a0"],
f618f6a1 76 ["do",
33783b04
DM
77 ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"],
78 ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]],
79 ["if", ["=", ["`", "if"], "a0"],
80 ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]],
81 ["if", ["or", ["=", "cond", null], ["=", "cond", false]],
82 ["if", [">", ["count", "ast"], 3],
83 ["EVAL", ["nth", "ast", 3], "env"],
84 null],
85 ["EVAL", ["nth", "ast", 2], "env"]]],
86 ["if", ["=", ["`", "fn*"], "a0"],
87 ["malfunc",
88 ["fn", ["&", "args"],
89 ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]],
90 ["EVAL", ["nth", "ast", 2], "e"]]],
91 ["nth", "ast", 2], "env", ["nth", "ast", 1]],
92 ["let", ["el", ["eval-ast", "ast", "env"],
93 "f", ["first", "el"],
94 "args", ["rest", "el"]],
95 ["if", ["malfunc?", "f"],
96 ["EVAL", ["get", "f", ["`", "ast"]],
97 ["env-new", ["get", "f", ["`", "env"]],
98 ["get", "f", ["`", "params"]],
99 "args"]],
100 ["apply", "f", "args"]]]]]]]]]]]]]]],
1981bf57
JM
101
102["def", "PRINT", ["fn", ["exp"],
103 ["pr-str", "exp", true]]],
104
105
106["def", "repl-env", ["env-new"]],
107
108["def", "rep", ["fn", ["strng"],
109 ["try",
110 ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]],
111 ["catch", "exc",
dd7a4f55
JM
112 ["str", ["`", "Error: "],
113 ["if", ["isa", "exc", "Error"],
114 [".", "exc", ["`", "toString"]],
115 ["pr-str", "exc", true]]]]]]],
1981bf57
JM
116
117["`", "core.mal: defined using miniMAL"],
118["map", ["fn", ["k"], ["env-set", "repl-env",
119 ["symbol", "k"],
120 ["get", "core-ns", "k"]]],
121 ["keys", "core-ns"]],
122["env-set", "repl-env", ["symbol", ["`", "eval"]],
123 ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]],
124["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]],
b32495e9 125 ["slice", "ARGS", 1]],
1981bf57
JM
126
127["`", "core.mal: defined using mal itself"],
128["rep", ["`", "(def! not (fn* (a) (if a false true)))"]],
129["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]],
130
b32495e9
JM
131["if", ["not", ["empty?", "ARGS"]],
132 ["rep", ["str", ["`", "(load-file \""], ["get", "ARGS", 0], ["`", "\")"]]],
1981bf57
JM
133 ["repl", ["`", "user> "], "rep"]],
134
135null
136
137]