Commit | Line | Data |
---|---|---|
31690700 JM |
1 | #include <stdlib.h> |
2 | #include <stdio.h> | |
3 | #include <unistd.h> | |
8cb5cda4 | 4 | |
31690700 JM |
5 | #include "types.h" |
6 | #include "readline.h" | |
7 | #include "reader.h" | |
ea81a808 | 8 | #include "core.h" |
31690700 JM |
9 | #include "interop.h" |
10 | ||
11 | // Declarations | |
12 | MalVal *EVAL(MalVal *ast, Env *env); | |
13 | MalVal *macroexpand(MalVal *ast, Env *env); | |
14 | ||
15 | // read | |
16 | MalVal *READ(char prompt[], char *str) { | |
17 | char *line; | |
18 | MalVal *ast; | |
19 | if (str) { | |
20 | line = str; | |
21 | } else { | |
22 | line = _readline(prompt); | |
23 | if (!line) { | |
24 | _error("EOF"); | |
25 | return NULL; | |
26 | } | |
27 | } | |
28 | ast = read_str(line); | |
29 | if (!str) { free(line); } | |
30 | return ast; | |
31 | } | |
32 | ||
33 | // eval | |
34 | int is_pair(MalVal *x) { | |
35 | return _sequential_Q(x) && (_count(x) > 0); | |
36 | } | |
37 | ||
38 | MalVal *quasiquote(MalVal *ast) { | |
39 | if (!is_pair(ast)) { | |
ea81a808 | 40 | return _listX(2, malval_new_symbol("quote"), ast); |
31690700 JM |
41 | } else { |
42 | MalVal *a0 = _nth(ast, 0); | |
43 | if ((a0->type & MAL_SYMBOL) && | |
44 | strcmp("unquote", a0->val.string) == 0) { | |
45 | return _nth(ast, 1); | |
46 | } else if (is_pair(a0)) { | |
47 | MalVal *a00 = _nth(a0, 0); | |
48 | if ((a00->type & MAL_SYMBOL) && | |
49 | strcmp("splice-unquote", a00->val.string) == 0) { | |
ea81a808 JM |
50 | return _listX(3, malval_new_symbol("concat"), |
51 | _nth(a0, 1), | |
8cb5cda4 | 52 | quasiquote(_rest(ast))); |
31690700 JM |
53 | } |
54 | } | |
ea81a808 JM |
55 | return _listX(3, malval_new_symbol("cons"), |
56 | quasiquote(a0), | |
8cb5cda4 | 57 | quasiquote(_rest(ast))); |
31690700 JM |
58 | } |
59 | } | |
60 | ||
61 | int is_macro_call(MalVal *ast, Env *env) { | |
62 | if (!ast || ast->type != MAL_LIST) { return 0; } | |
63 | MalVal *a0 = _nth(ast, 0); | |
64 | return (a0->type & MAL_SYMBOL) && | |
65 | env_find(env, a0->val.string) && | |
66 | env_get(env, a0->val.string)->ismacro; | |
67 | } | |
68 | ||
69 | MalVal *macroexpand(MalVal *ast, Env *env) { | |
70 | if (!ast || mal_error) return NULL; | |
71 | while (is_macro_call(ast, env)) { | |
72 | MalVal *a0 = _nth(ast, 0); | |
73 | MalVal *mac = env_get(env, a0->val.string); | |
74 | // TODO: this is weird and limits it to 20. FIXME | |
8cb5cda4 | 75 | ast = _apply(mac, _rest(ast)); |
31690700 JM |
76 | } |
77 | return ast; | |
78 | } | |
79 | ||
80 | MalVal *eval_ast(MalVal *ast, Env *env) { | |
81 | if (!ast || mal_error) return NULL; | |
82 | if (ast->type == MAL_SYMBOL) { | |
83 | //g_print("EVAL symbol: %s\n", ast->val.string); | |
84 | return env_get(env, ast->val.string); | |
85 | } else if ((ast->type == MAL_LIST) || (ast->type == MAL_VECTOR)) { | |
86 | //g_print("EVAL sequential: %s\n", _pr_str(ast,1)); | |
87 | MalVal *el = _map2((MalVal *(*)(void*, void*))EVAL, ast, env); | |
88 | if (!el || mal_error) return NULL; | |
89 | el->type = ast->type; | |
90 | return el; | |
91 | } else if (ast->type == MAL_HASH_MAP) { | |
92 | //g_print("EVAL hash_map: %s\n", _pr_str(ast,1)); | |
93 | GHashTableIter iter; | |
94 | gpointer key, value; | |
95 | MalVal *seq = malval_new_list(MAL_LIST, | |
96 | g_array_sized_new(TRUE, TRUE, sizeof(MalVal*), | |
97 | _count(ast))); | |
98 | g_hash_table_iter_init (&iter, ast->val.hash_table); | |
99 | while (g_hash_table_iter_next (&iter, &key, &value)) { | |
100 | MalVal *kname = malval_new_string((char *)key); | |
101 | g_array_append_val(seq->val.array, kname); | |
102 | MalVal *new_val = EVAL((MalVal *)value, env); | |
103 | g_array_append_val(seq->val.array, new_val); | |
104 | } | |
8cb5cda4 | 105 | return _hash_map(seq); |
31690700 JM |
106 | } else { |
107 | //g_print("EVAL scalar: %s\n", _pr_str(ast,1)); | |
108 | return ast; | |
109 | } | |
110 | } | |
111 | ||
112 | MalVal *EVAL(MalVal *ast, Env *env) { | |
113 | while (TRUE) { | |
31690700 | 114 | |
8cb5cda4 JM |
115 | if (!ast || mal_error) return NULL; |
116 | //g_print("EVAL: %s\n", _pr_str(ast,1)); | |
117 | if (ast->type != MAL_LIST) { | |
118 | return eval_ast(ast, env); | |
119 | } | |
120 | if (!ast || mal_error) return NULL; | |
31690700 | 121 | |
8cb5cda4 JM |
122 | // apply list |
123 | //g_print("EVAL apply list: %s\n", _pr_str(ast,1)); | |
124 | ast = macroexpand(ast, env); | |
125 | if (!ast || mal_error) return NULL; | |
126 | if (ast->type != MAL_LIST) { return ast; } | |
127 | if (_count(ast) == 0) { return ast; } | |
128 | ||
129 | int i, len; | |
130 | MalVal *a0 = _nth(ast, 0); | |
131 | if ((a0->type & MAL_SYMBOL) && | |
132 | strcmp("def!", a0->val.string) == 0) { | |
133 | //g_print("eval apply def!\n"); | |
134 | MalVal *a1 = _nth(ast, 1), | |
135 | *a2 = _nth(ast, 2); | |
136 | MalVal *res = EVAL(a2, env); | |
137 | env_set(env, a1->val.string, res); | |
138 | return res; | |
139 | } else if ((a0->type & MAL_SYMBOL) && | |
140 | strcmp("let*", a0->val.string) == 0) { | |
141 | //g_print("eval apply let*\n"); | |
142 | MalVal *a1 = _nth(ast, 1), | |
143 | *a2 = _nth(ast, 2), | |
144 | *key, *val; | |
145 | assert_type(a1, MAL_LIST|MAL_VECTOR, | |
146 | "let* bindings must be list or vector"); | |
147 | len = _count(a1); | |
148 | assert((len % 2) == 0, "odd number of let* bindings forms"); | |
149 | Env *let_env = new_env(env, NULL, NULL); | |
150 | for(i=0; i<len; i+=2) { | |
151 | key = g_array_index(a1->val.array, MalVal*, i); | |
152 | val = g_array_index(a1->val.array, MalVal*, i+1); | |
153 | assert_type(key, MAL_SYMBOL, "let* bind to non-symbol"); | |
154 | env_set(let_env, key->val.string, EVAL(val, let_env)); | |
155 | } | |
156 | return EVAL(a2, let_env); | |
157 | } else if ((a0->type & MAL_SYMBOL) && | |
158 | strcmp("quote", a0->val.string) == 0) { | |
159 | //g_print("eval apply quote\n"); | |
160 | return _nth(ast, 1); | |
161 | } else if ((a0->type & MAL_SYMBOL) && | |
162 | strcmp("quasiquote", a0->val.string) == 0) { | |
163 | //g_print("eval apply quasiquote\n"); | |
164 | MalVal *a1 = _nth(ast, 1); | |
165 | return EVAL(quasiquote(a1), env); | |
166 | } else if ((a0->type & MAL_SYMBOL) && | |
167 | strcmp("defmacro!", a0->val.string) == 0) { | |
168 | //g_print("eval apply defmacro!\n"); | |
169 | MalVal *a1 = _nth(ast, 1), | |
170 | *a2 = _nth(ast, 2); | |
171 | MalVal *res = EVAL(a2, env); | |
172 | res->ismacro = TRUE; | |
173 | env_set(env, a1->val.string, res); | |
174 | return res; | |
175 | } else if ((a0->type & MAL_SYMBOL) && | |
176 | strcmp("macroexpand", a0->val.string) == 0) { | |
177 | //g_print("eval apply macroexpand\n"); | |
178 | MalVal *a1 = _nth(ast, 1); | |
179 | return macroexpand(a1, env); | |
180 | } else if ((a0->type & MAL_SYMBOL) && | |
181 | strcmp(".", a0->val.string) == 0) { | |
182 | //g_print("eval apply .\n"); | |
183 | MalVal *el = eval_ast(_slice(ast, 1, _count(ast)), env); | |
184 | return invoke_native(el); | |
185 | } else if ((a0->type & MAL_SYMBOL) && | |
186 | strcmp("try*", a0->val.string) == 0) { | |
187 | //g_print("eval apply try*\n"); | |
188 | MalVal *a1 = _nth(ast, 1); | |
189 | MalVal *a2 = _nth(ast, 2); | |
190 | MalVal *res = EVAL(a1, env); | |
191 | if (!mal_error) { return res; } | |
192 | MalVal *a20 = _nth(a2, 0); | |
193 | if (strcmp("catch*", a20->val.string) == 0) { | |
194 | MalVal *a21 = _nth(a2, 1); | |
195 | MalVal *a22 = _nth(a2, 2); | |
196 | Env *catch_env = new_env(env, | |
197 | _listX(1, a21), | |
198 | _listX(1, mal_error)); | |
199 | //malval_free(mal_error); | |
200 | mal_error = NULL; | |
201 | res = EVAL(a22, catch_env); | |
31690700 | 202 | return res; |
8cb5cda4 JM |
203 | } else { |
204 | return &mal_nil; | |
205 | } | |
206 | } else if ((a0->type & MAL_SYMBOL) && | |
207 | strcmp("do", a0->val.string) == 0) { | |
208 | //g_print("eval apply do\n"); | |
209 | eval_ast(_slice(ast, 1, _count(ast)-1), env); | |
210 | ast = _last(ast); | |
211 | // Continue loop | |
212 | } else if ((a0->type & MAL_SYMBOL) && | |
213 | strcmp("if", a0->val.string) == 0) { | |
214 | //g_print("eval apply if\n"); | |
215 | MalVal *a1 = _nth(ast, 1); | |
216 | MalVal *cond = EVAL(a1, env); | |
217 | if (!cond || mal_error) return NULL; | |
218 | if (cond->type & (MAL_FALSE|MAL_NIL)) { | |
219 | // eval false slot form | |
220 | ast = _nth(ast, 3); | |
221 | if (!ast) { | |
31690700 JM |
222 | return &mal_nil; |
223 | } | |
8cb5cda4 JM |
224 | } else { |
225 | // eval true slot form | |
226 | ast = _nth(ast, 2); | |
227 | } | |
228 | // Continue loop | |
229 | } else if ((a0->type & MAL_SYMBOL) && | |
230 | strcmp("fn*", a0->val.string) == 0) { | |
231 | //g_print("eval apply fn*\n"); | |
232 | MalVal *mf = malval_new(MAL_FUNCTION_MAL, NULL); | |
233 | mf->ismacro = FALSE; | |
234 | mf->val.func.evaluator = EVAL; | |
235 | mf->val.func.args = _nth(ast, 1); | |
236 | mf->val.func.body = _nth(ast, 2); | |
237 | mf->val.func.env = env; | |
238 | return mf; | |
239 | } else { | |
240 | //g_print("eval apply\n"); | |
241 | MalVal *el = eval_ast(ast, env); | |
242 | if (!el || mal_error) { return NULL; } | |
243 | MalVal *f = _first(el), | |
244 | *args = _rest(el); | |
245 | assert_type(f, MAL_FUNCTION_C|MAL_FUNCTION_MAL, | |
246 | "cannot apply '%s'", _pr_str(f,1)); | |
247 | if (f->type & MAL_FUNCTION_MAL) { | |
248 | ast = f->val.func.body; | |
249 | env = new_env(f->val.func.env, f->val.func.args, args); | |
31690700 | 250 | // Continue loop |
31690700 | 251 | } else { |
8cb5cda4 | 252 | return _apply(f, args); |
31690700 JM |
253 | } |
254 | } | |
8cb5cda4 JM |
255 | |
256 | } // TCO while loop | |
31690700 | 257 | } |
ea81a808 | 258 | |
31690700 JM |
259 | |
260 | char *PRINT(MalVal *exp) { | |
261 | if (mal_error) { | |
262 | fprintf(stderr, "Error: %s\n", mal_error->val.string); | |
263 | malval_free(mal_error); | |
264 | mal_error = NULL; | |
265 | return NULL; | |
266 | } | |
267 | return _pr_str(exp,1); | |
268 | } | |
269 | ||
270 | // repl | |
271 | ||
272 | // read and eval | |
273 | MalVal *RE(Env *env, char *prompt, char *str) { | |
274 | MalVal *ast, *exp; | |
275 | ast = READ(prompt, str); | |
276 | if (!ast || mal_error) return NULL; | |
277 | exp = EVAL(ast, env); | |
278 | if (ast != exp) { | |
279 | malval_free(ast); // Free input structure | |
280 | } | |
281 | return exp; | |
282 | } | |
283 | ||
284 | // Setup the initial REPL environment | |
285 | Env *repl_env; | |
286 | ||
31690700 | 287 | void init_repl_env() { |
31690700 JM |
288 | repl_env = new_env(NULL, NULL, NULL); |
289 | ||
8cb5cda4 | 290 | // core.c: defined using C |
31690700 | 291 | int i; |
ea81a808 | 292 | for(i=0; i< (sizeof(core_ns) / sizeof(core_ns[0])); i++) { |
8cb5cda4 JM |
293 | env_set(repl_env, core_ns[i].name, |
294 | malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); | |
31690700 | 295 | } |
8cb5cda4 JM |
296 | MalVal *do_eval(MalVal *ast) { return EVAL(ast, repl_env); } |
297 | env_set(repl_env, "eval", | |
298 | malval_new_function((void*(*)(void *))do_eval, 1)); | |
31690700 | 299 | |
8cb5cda4 | 300 | // core.mal: defined using the language itself |
31690700 | 301 | RE(repl_env, "", "(def! not (fn* (a) (if a false true)))"); |
31690700 | 302 | RE(repl_env, "", |
1617910a | 303 | "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); |
8cb5cda4 JM |
304 | RE(repl_env, "", "(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)))))))"); |
305 | RE(repl_env, "", "(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))))))))"); | |
31690700 JM |
306 | } |
307 | ||
308 | int main(int argc, char *argv[]) | |
309 | { | |
310 | MalVal *exp; | |
311 | char *output; | |
312 | char prompt[100]; | |
313 | ||
314 | // Set the initial prompt and environment | |
315 | snprintf(prompt, sizeof(prompt), "user> "); | |
316 | init_repl_env(); | |
317 | ||
318 | if (argc > 1) { | |
319 | char *cmd = g_strdup_printf("(load-file \"%s\")", argv[1]); | |
320 | RE(repl_env, "", cmd); | |
8cb5cda4 JM |
321 | return 0; |
322 | } | |
31690700 | 323 | |
8cb5cda4 JM |
324 | // REPL loop |
325 | for(;;) { | |
326 | exp = RE(repl_env, prompt, NULL); | |
327 | if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { | |
328 | return 0; | |
329 | } | |
330 | output = PRINT(exp); | |
31690700 | 331 | |
8cb5cda4 JM |
332 | if (output) { |
333 | g_print("%s\n", output); | |
334 | free(output); // Free output string | |
31690700 | 335 | } |
8cb5cda4 JM |
336 | |
337 | //malval_free(exp); // Free evaluated expression | |
31690700 JM |
338 | } |
339 | } |