Commit | Line | Data |
---|---|---|
31690700 JM |
1 | #include <stdlib.h> |
2 | #include <stdio.h> | |
b81b2a7e | 3 | #include <string.h> |
31690700 | 4 | #include <unistd.h> |
8cb5cda4 | 5 | |
31690700 JM |
6 | #include "types.h" |
7 | #include "readline.h" | |
8 | #include "reader.h" | |
ea81a808 | 9 | #include "core.h" |
31690700 JM |
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); | |
6b3ecaa7 | 29 | if (!str) { MAL_GC_FREE(line); } |
31690700 JM |
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) { | |
9685275d | 62 | if (!ast || ast->type != MAL_LIST || _count(ast) == 0) { return 0; } |
31690700 JM |
63 | MalVal *a0 = _nth(ast, 0); |
64 | return (a0->type & MAL_SYMBOL) && | |
b8ee29b2 JM |
65 | env_find(env, a0) && |
66 | env_get(env, a0)->ismacro; | |
31690700 | 67 | } |
dd7a4f55 | 68 | |
31690700 JM |
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); | |
b8ee29b2 | 73 | MalVal *mac = env_get(env, a0); |
31690700 | 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); | |
b8ee29b2 | 84 | return env_get(env, ast); |
31690700 JM |
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; | |
0d629719 DM |
126 | if (ast->type != MAL_LIST) { |
127 | return eval_ast(ast, env); | |
128 | } | |
8cb5cda4 JM |
129 | if (_count(ast) == 0) { return ast; } |
130 | ||
131 | int i, len; | |
132 | MalVal *a0 = _nth(ast, 0); | |
133 | if ((a0->type & MAL_SYMBOL) && | |
134 | strcmp("def!", a0->val.string) == 0) { | |
135 | //g_print("eval apply def!\n"); | |
136 | MalVal *a1 = _nth(ast, 1), | |
137 | *a2 = _nth(ast, 2); | |
138 | MalVal *res = EVAL(a2, env); | |
b8ee29b2 JM |
139 | if (mal_error) return NULL; |
140 | env_set(env, a1, res); | |
8cb5cda4 JM |
141 | return res; |
142 | } else if ((a0->type & MAL_SYMBOL) && | |
143 | strcmp("let*", a0->val.string) == 0) { | |
144 | //g_print("eval apply let*\n"); | |
145 | MalVal *a1 = _nth(ast, 1), | |
146 | *a2 = _nth(ast, 2), | |
147 | *key, *val; | |
148 | assert_type(a1, MAL_LIST|MAL_VECTOR, | |
149 | "let* bindings must be list or vector"); | |
150 | len = _count(a1); | |
151 | assert((len % 2) == 0, "odd number of let* bindings forms"); | |
152 | Env *let_env = new_env(env, NULL, NULL); | |
153 | for(i=0; i<len; i+=2) { | |
154 | key = g_array_index(a1->val.array, MalVal*, i); | |
155 | val = g_array_index(a1->val.array, MalVal*, i+1); | |
156 | assert_type(key, MAL_SYMBOL, "let* bind to non-symbol"); | |
b8ee29b2 | 157 | env_set(let_env, key, EVAL(val, let_env)); |
8cb5cda4 | 158 | } |
6301e0b6 JM |
159 | ast = a2; |
160 | env = let_env; | |
161 | // Continue loop | |
8cb5cda4 JM |
162 | } else if ((a0->type & MAL_SYMBOL) && |
163 | strcmp("quote", a0->val.string) == 0) { | |
164 | //g_print("eval apply quote\n"); | |
165 | return _nth(ast, 1); | |
166 | } else if ((a0->type & MAL_SYMBOL) && | |
167 | strcmp("quasiquote", a0->val.string) == 0) { | |
168 | //g_print("eval apply quasiquote\n"); | |
169 | MalVal *a1 = _nth(ast, 1); | |
6301e0b6 JM |
170 | ast = quasiquote(a1); |
171 | // Continue loop | |
8cb5cda4 JM |
172 | } else if ((a0->type & MAL_SYMBOL) && |
173 | strcmp("defmacro!", a0->val.string) == 0) { | |
174 | //g_print("eval apply defmacro!\n"); | |
175 | MalVal *a1 = _nth(ast, 1), | |
176 | *a2 = _nth(ast, 2); | |
177 | MalVal *res = EVAL(a2, env); | |
b8ee29b2 | 178 | if (mal_error) return NULL; |
8cb5cda4 | 179 | res->ismacro = TRUE; |
b8ee29b2 | 180 | env_set(env, a1, res); |
8cb5cda4 JM |
181 | return res; |
182 | } else if ((a0->type & MAL_SYMBOL) && | |
183 | strcmp("macroexpand", a0->val.string) == 0) { | |
184 | //g_print("eval apply macroexpand\n"); | |
185 | MalVal *a1 = _nth(ast, 1); | |
186 | return macroexpand(a1, env); | |
187 | } else if ((a0->type & MAL_SYMBOL) && | |
188 | strcmp("do", a0->val.string) == 0) { | |
189 | //g_print("eval apply do\n"); | |
190 | eval_ast(_slice(ast, 1, _count(ast)-1), env); | |
191 | ast = _last(ast); | |
192 | // Continue loop | |
193 | } else if ((a0->type & MAL_SYMBOL) && | |
194 | strcmp("if", a0->val.string) == 0) { | |
195 | //g_print("eval apply if\n"); | |
196 | MalVal *a1 = _nth(ast, 1); | |
197 | MalVal *cond = EVAL(a1, env); | |
198 | if (!cond || mal_error) return NULL; | |
199 | if (cond->type & (MAL_FALSE|MAL_NIL)) { | |
200 | // eval false slot form | |
b8ee29b2 JM |
201 | if (ast->val.array->len > 3) { |
202 | ast = _nth(ast, 3); | |
203 | } else { | |
8cb5cda4 | 204 | return &mal_nil; |
31690700 | 205 | } |
8cb5cda4 JM |
206 | } else { |
207 | // eval true slot form | |
208 | ast = _nth(ast, 2); | |
209 | } | |
210 | // Continue loop | |
211 | } else if ((a0->type & MAL_SYMBOL) && | |
212 | strcmp("fn*", a0->val.string) == 0) { | |
213 | //g_print("eval apply fn*\n"); | |
214 | MalVal *mf = malval_new(MAL_FUNCTION_MAL, NULL); | |
215 | mf->ismacro = FALSE; | |
216 | mf->val.func.evaluator = EVAL; | |
217 | mf->val.func.args = _nth(ast, 1); | |
218 | mf->val.func.body = _nth(ast, 2); | |
219 | mf->val.func.env = env; | |
220 | return mf; | |
221 | } else { | |
222 | //g_print("eval apply\n"); | |
223 | MalVal *el = eval_ast(ast, env); | |
224 | if (!el || mal_error) { return NULL; } | |
225 | MalVal *f = _first(el), | |
226 | *args = _rest(el); | |
227 | assert_type(f, MAL_FUNCTION_C|MAL_FUNCTION_MAL, | |
228 | "cannot apply '%s'", _pr_str(f,1)); | |
229 | if (f->type & MAL_FUNCTION_MAL) { | |
230 | ast = f->val.func.body; | |
231 | env = new_env(f->val.func.env, f->val.func.args, args); | |
31690700 | 232 | // Continue loop |
31690700 | 233 | } else { |
8cb5cda4 | 234 | return _apply(f, args); |
31690700 JM |
235 | } |
236 | } | |
8cb5cda4 JM |
237 | |
238 | } // TCO while loop | |
31690700 | 239 | } |
1617910a | 240 | |
31690700 JM |
241 | |
242 | char *PRINT(MalVal *exp) { | |
243 | if (mal_error) { | |
31690700 JM |
244 | return NULL; |
245 | } | |
246 | return _pr_str(exp,1); | |
247 | } | |
248 | ||
249 | // repl | |
250 | ||
251 | // read and eval | |
252 | MalVal *RE(Env *env, char *prompt, char *str) { | |
253 | MalVal *ast, *exp; | |
254 | ast = READ(prompt, str); | |
255 | if (!ast || mal_error) return NULL; | |
256 | exp = EVAL(ast, env); | |
257 | if (ast != exp) { | |
258 | malval_free(ast); // Free input structure | |
259 | } | |
260 | return exp; | |
261 | } | |
262 | ||
263 | // Setup the initial REPL environment | |
264 | Env *repl_env; | |
265 | ||
b81b2a7e LB |
266 | MalVal *do_eval(MalVal *ast) { return EVAL(ast, repl_env); } |
267 | ||
86b689f3 | 268 | void init_repl_env(int argc, char *argv[]) { |
31690700 JM |
269 | repl_env = new_env(NULL, NULL, NULL); |
270 | ||
8cb5cda4 | 271 | // core.c: defined using C |
31690700 | 272 | int i; |
86b689f3 | 273 | for(i=0; i < (sizeof(core_ns) / sizeof(core_ns[0])); i++) { |
b8ee29b2 JM |
274 | env_set(repl_env, |
275 | malval_new_symbol(core_ns[i].name), | |
8cb5cda4 | 276 | malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); |
31690700 | 277 | } |
b8ee29b2 JM |
278 | env_set(repl_env, |
279 | malval_new_symbol("eval"), | |
8cb5cda4 | 280 | malval_new_function((void*(*)(void *))do_eval, 1)); |
31690700 | 281 | |
86b689f3 JM |
282 | MalVal *_argv = _listX(0); |
283 | for (i=2; i < argc; i++) { | |
284 | MalVal *arg = malval_new_string(argv[i]); | |
285 | g_array_append_val(_argv->val.array, arg); | |
286 | } | |
b8ee29b2 | 287 | env_set(repl_env, malval_new_symbol("*ARGV*"), _argv); |
86b689f3 | 288 | |
8cb5cda4 | 289 | // core.mal: defined using the language itself |
31690700 JM |
290 | RE(repl_env, "", "(def! not (fn* (a) (if a false true)))"); |
291 | RE(repl_env, "", | |
e6d41de4 | 292 | "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))"); |
8cb5cda4 | 293 | 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)))))))"); |
31690700 JM |
294 | } |
295 | ||
296 | int main(int argc, char *argv[]) | |
297 | { | |
298 | MalVal *exp; | |
299 | char *output; | |
300 | char prompt[100]; | |
301 | ||
6b3ecaa7 DM |
302 | MAL_GC_SETUP(); |
303 | ||
31690700 JM |
304 | // Set the initial prompt and environment |
305 | snprintf(prompt, sizeof(prompt), "user> "); | |
86b689f3 | 306 | init_repl_env(argc, argv); |
dd7a4f55 | 307 | |
31690700 JM |
308 | if (argc > 1) { |
309 | char *cmd = g_strdup_printf("(load-file \"%s\")", argv[1]); | |
310 | RE(repl_env, "", cmd); | |
8cb5cda4 JM |
311 | return 0; |
312 | } | |
31690700 | 313 | |
86b689f3 | 314 | // repl loop |
8cb5cda4 JM |
315 | for(;;) { |
316 | exp = RE(repl_env, prompt, NULL); | |
317 | if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { | |
318 | return 0; | |
319 | } | |
320 | output = PRINT(exp); | |
31690700 | 321 | |
dd7a4f55 JM |
322 | if (mal_error) { |
323 | fprintf(stderr, "Error: %s\n", _pr_str(mal_error,1)); | |
324 | malval_free(mal_error); | |
325 | mal_error = NULL; | |
326 | } else if (output) { | |
6b3ecaa7 DM |
327 | puts(output); |
328 | MAL_GC_FREE(output); // Free output string | |
31690700 | 329 | } |
8cb5cda4 JM |
330 | |
331 | //malval_free(exp); // Free evaluated expression | |
31690700 JM |
332 | } |
333 | } |