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); | |
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) && | |
b8ee29b2 JM |
65 | env_find(env, a0) && |
66 | env_get(env, a0)->ismacro; | |
31690700 JM |
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); | |
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) { | |
244 | fprintf(stderr, "Error: %s\n", mal_error->val.string); | |
245 | malval_free(mal_error); | |
246 | mal_error = NULL; | |
247 | return NULL; | |
248 | } | |
249 | return _pr_str(exp,1); | |
250 | } | |
251 | ||
252 | // repl | |
253 | ||
254 | // read and eval | |
255 | MalVal *RE(Env *env, char *prompt, char *str) { | |
256 | MalVal *ast, *exp; | |
257 | ast = READ(prompt, str); | |
258 | if (!ast || mal_error) return NULL; | |
259 | exp = EVAL(ast, env); | |
260 | if (ast != exp) { | |
261 | malval_free(ast); // Free input structure | |
262 | } | |
263 | return exp; | |
264 | } | |
265 | ||
266 | // Setup the initial REPL environment | |
267 | Env *repl_env; | |
268 | ||
b81b2a7e LB |
269 | MalVal *do_eval(MalVal *ast) { return EVAL(ast, repl_env); } |
270 | ||
86b689f3 | 271 | void init_repl_env(int argc, char *argv[]) { |
31690700 JM |
272 | repl_env = new_env(NULL, NULL, NULL); |
273 | ||
8cb5cda4 | 274 | // core.c: defined using C |
31690700 | 275 | int i; |
86b689f3 | 276 | for(i=0; i < (sizeof(core_ns) / sizeof(core_ns[0])); i++) { |
b8ee29b2 JM |
277 | env_set(repl_env, |
278 | malval_new_symbol(core_ns[i].name), | |
8cb5cda4 | 279 | malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); |
31690700 | 280 | } |
b8ee29b2 JM |
281 | env_set(repl_env, |
282 | malval_new_symbol("eval"), | |
8cb5cda4 | 283 | malval_new_function((void*(*)(void *))do_eval, 1)); |
31690700 | 284 | |
86b689f3 JM |
285 | MalVal *_argv = _listX(0); |
286 | for (i=2; i < argc; i++) { | |
287 | MalVal *arg = malval_new_string(argv[i]); | |
288 | g_array_append_val(_argv->val.array, arg); | |
289 | } | |
b8ee29b2 | 290 | env_set(repl_env, malval_new_symbol("*ARGV*"), _argv); |
86b689f3 | 291 | |
8cb5cda4 | 292 | // core.mal: defined using the language itself |
31690700 JM |
293 | RE(repl_env, "", "(def! not (fn* (a) (if a false true)))"); |
294 | RE(repl_env, "", | |
1617910a | 295 | "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); |
8cb5cda4 JM |
296 | 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)))))))"); |
297 | 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 |
298 | } |
299 | ||
300 | int main(int argc, char *argv[]) | |
301 | { | |
302 | MalVal *exp; | |
303 | char *output; | |
304 | char prompt[100]; | |
305 | ||
306 | // Set the initial prompt and environment | |
307 | snprintf(prompt, sizeof(prompt), "user> "); | |
86b689f3 | 308 | init_repl_env(argc, argv); |
31690700 JM |
309 | |
310 | if (argc > 1) { | |
311 | char *cmd = g_strdup_printf("(load-file \"%s\")", argv[1]); | |
312 | RE(repl_env, "", cmd); | |
8cb5cda4 JM |
313 | return 0; |
314 | } | |
31690700 | 315 | |
86b689f3 | 316 | // repl loop |
8cb5cda4 JM |
317 | for(;;) { |
318 | exp = RE(repl_env, prompt, NULL); | |
319 | if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { | |
320 | return 0; | |
321 | } | |
322 | output = PRINT(exp); | |
31690700 | 323 | |
8cb5cda4 JM |
324 | if (output) { |
325 | g_print("%s\n", output); | |
326 | free(output); // Free output string | |
31690700 | 327 | } |
8cb5cda4 JM |
328 | |
329 | //malval_free(exp); // Free evaluated expression | |
31690700 JM |
330 | } |
331 | } |