Commit | Line | Data |
---|---|---|
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 | ||
12 | ["def", "pair?", ["fn", ["x"], | |
32045546 | 13 | ["if", ["sequential?", "x"], |
2774a151 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"]]]], | |
2774a151 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", "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?"]]]]]]], | |
40 | ||
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"]], | |
45 | "ast"]]], | |
46 | ||
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"], | |
32045546 JM |
52 | ["if", ["vector?", "ast"], |
53 | ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]], | |
54 | ["if", ["map?", "ast"], | |
55 | ["let", ["new-hm", ["hash-map"]], | |
56 | ["do", | |
57 | ["map", ["fn", ["k"], ["set", "new-hm", | |
58 | ["EVAL", "k", "env"], | |
59 | ["EVAL", ["get", "ast", "k"], "env"]]], | |
60 | ["keys", "ast"]], | |
61 | "new-hm"]], | |
62 | "ast"]]]]]], | |
2774a151 JM |
63 | |
64 | ["def", "LET", ["fn", ["env", "args"], | |
65 | ["if", [">", ["count", "args"], 0], | |
66 | ["do", | |
67 | ["env-set", "env", ["nth", "args", 0], | |
68 | ["EVAL", ["nth", "args", 1], "env"]], | |
69 | ["LET", "env", ["rest", ["rest", "args"]]]]]]], | |
70 | ||
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"]], | |
bfa3dd35 | 76 | ["eval-ast", "ast", "env"], |
33783b04 DM |
77 | ["if", ["empty?", "ast"], |
78 | "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"]], | |
85 | ["do", | |
86 | ["LET", "let-env", ["nth", "ast", 1]], | |
87 | ["EVAL", ["nth", "ast", 2], "let-env"]]], | |
88 | ["if", ["=", ["`", "quote"], "a0"], | |
89 | ["nth", "ast", 1], | |
90 | ["if", ["=", ["`", "quasiquote"], "a0"], | |
91 | ["EVAL", ["quasiquote", ["nth", "ast", 1]], "env"], | |
92 | ["if", ["=", ["`", "defmacro!"], "a0"], | |
93 | ["let", ["func", ["EVAL", ["nth", "ast", 2], "env"]], | |
94 | ["do", | |
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", ["=", ["`", "catch*"], | |
101 | ["get", ["nth", ["nth", "ast", 2], 0], ["`", "val"]]], | |
102 | ["try", | |
103 | ["EVAL", ["nth", "ast", 1], "env"], | |
104 | ["catch", "exc", | |
105 | ["EVAL", ["nth", ["nth", "ast", 2], 2], | |
106 | ["env-new", "env", | |
107 | ["list", ["nth", ["nth", "ast", 2], 1]], | |
108 | ["list", "exc"]]]]], | |
109 | ["EVAL", ["nth", "ast", 1], "env"]], | |
110 | ["if", ["=", ["`", "do"], "a0"], | |
2774a151 | 111 | ["do", |
33783b04 DM |
112 | ["eval-ast", ["slice", "ast", 1, ["-", ["count", "ast"], 1]], "env"], |
113 | ["EVAL", ["nth", "ast", ["-", ["count", "ast"], 1]], "env"]], | |
114 | ["if", ["=", ["`", "if"], "a0"], | |
115 | ["let", ["cond", ["EVAL", ["nth", "ast", 1], "env"]], | |
116 | ["if", ["or", ["=", "cond", null], ["=", "cond", false]], | |
117 | ["if", [">", ["count", "ast"], 3], | |
118 | ["EVAL", ["nth", "ast", 3], "env"], | |
119 | null], | |
120 | ["EVAL", ["nth", "ast", 2], "env"]]], | |
121 | ["if", ["=", ["`", "fn*"], "a0"], | |
122 | ["malfunc", | |
123 | ["fn", ["&", "args"], | |
124 | ["let", ["e", ["env-new", "env", ["nth", "ast", 1], "args"]], | |
125 | ["EVAL", ["nth", "ast", 2], "e"]]], | |
126 | ["nth", "ast", 2], "env", ["nth", "ast", 1]], | |
127 | ["let", ["el", ["eval-ast", "ast", "env"], | |
128 | "f", ["first", "el"], | |
129 | "args", ["rest", "el"]], | |
130 | ["if", ["malfunc?", "f"], | |
131 | ["EVAL", ["get", "f", ["`", "ast"]], | |
132 | ["env-new", ["get", "f", ["`", "env"]], | |
133 | ["get", "f", ["`", "params"]], | |
134 | "args"]], | |
135 | ["apply", "f", "args"]]]]]]]]]]]]]]]]]]]], | |
2774a151 JM |
136 | |
137 | ["def", "PRINT", ["fn", ["exp"], | |
138 | ["pr-str", "exp", true]]], | |
139 | ||
140 | ||
141 | ["def", "repl-env", ["env-new"]], | |
142 | ||
143 | ["def", "rep", ["fn", ["strng"], | |
144 | ["try", | |
145 | ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]], | |
146 | ["catch", "exc", | |
147 | ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]], | |
148 | ||
149 | ["`", "core.mal: defined using miniMAL"], | |
150 | ["map", ["fn", ["k"], ["env-set", "repl-env", | |
151 | ["symbol", "k"], | |
152 | ["get", "core-ns", "k"]]], | |
153 | ["keys", "core-ns"]], | |
154 | ["env-set", "repl-env", ["symbol", ["`", "eval"]], | |
155 | ["fn", ["ast"], ["EVAL", "ast", "repl-env"]]], | |
156 | ["env-set", "repl-env", ["symbol", ["`", "*ARGV*"]], | |
b32495e9 | 157 | ["slice", "ARGS", 1]], |
2774a151 JM |
158 | |
159 | ["`", "core.mal: defined using mal itself"], | |
160 | ["rep", ["`", "(def! not (fn* (a) (if a false true)))"]], | |
161 | ["rep", ["`", "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]], | |
162 | ["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)))))))"]], | |
163 | ["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))))))))"]], | |
164 | ||
b32495e9 JM |
165 | ["if", ["not", ["empty?", "ARGS"]], |
166 | ["rep", ["str", ["`", "(load-file \""], ["get", "ARGS", 0], ["`", "\")"]]], | |
2774a151 JM |
167 | ["repl", ["`", "user> "], "rep"]], |
168 | ||
169 | null | |
170 | ||
171 | ] |