13 MalVal
*EVAL(MalVal
*ast
, Env
*env
);
14 MalVal
*macroexpand(MalVal
*ast
, Env
*env
);
17 MalVal
*READ(char prompt
[], char *str
) {
23 line
= _readline(prompt
);
30 if (!str
) { free(line
); }
35 int is_pair(MalVal
*x
) {
36 return _sequential_Q(x
) && (_count(x
) > 0);
39 MalVal
*quasiquote(MalVal
*ast
) {
41 return _listX(2, malval_new_symbol("quote"), ast
);
43 MalVal
*a0
= _nth(ast
, 0);
44 if ((a0
->type
& MAL_SYMBOL
) &&
45 strcmp("unquote", a0
->val
.string
) == 0) {
47 } else if (is_pair(a0
)) {
48 MalVal
*a00
= _nth(a0
, 0);
49 if ((a00
->type
& MAL_SYMBOL
) &&
50 strcmp("splice-unquote", a00
->val
.string
) == 0) {
51 return _listX(3, malval_new_symbol("concat"),
53 quasiquote(_rest(ast
)));
56 return _listX(3, malval_new_symbol("cons"),
58 quasiquote(_rest(ast
)));
62 int is_macro_call(MalVal
*ast
, Env
*env
) {
63 if (!ast
|| ast
->type
!= MAL_LIST
) { return 0; }
64 MalVal
*a0
= _nth(ast
, 0);
65 return (a0
->type
& MAL_SYMBOL
) &&
67 env_get(env
, a0
)->ismacro
;
70 MalVal
*macroexpand(MalVal
*ast
, Env
*env
) {
71 if (!ast
|| mal_error
) return NULL
;
72 while (is_macro_call(ast
, env
)) {
73 MalVal
*a0
= _nth(ast
, 0);
74 MalVal
*mac
= env_get(env
, a0
);
75 // TODO: this is weird and limits it to 20. FIXME
76 ast
= _apply(mac
, _rest(ast
));
81 MalVal
*eval_ast(MalVal
*ast
, Env
*env
) {
82 if (!ast
|| mal_error
) return NULL
;
83 if (ast
->type
== MAL_SYMBOL
) {
84 //g_print("EVAL symbol: %s\n", ast->val.string);
85 return env_get(env
, ast
);
86 } else if ((ast
->type
== MAL_LIST
) || (ast
->type
== MAL_VECTOR
)) {
87 //g_print("EVAL sequential: %s\n", _pr_str(ast,1));
88 MalVal
*el
= _map2((MalVal
*(*)(void*, void*))EVAL
, ast
, env
);
89 if (!el
|| mal_error
) return NULL
;
92 } else if (ast
->type
== MAL_HASH_MAP
) {
93 //g_print("EVAL hash_map: %s\n", _pr_str(ast,1));
96 MalVal
*seq
= malval_new_list(MAL_LIST
,
97 g_array_sized_new(TRUE
, TRUE
, sizeof(MalVal
*),
99 g_hash_table_iter_init (&iter
, ast
->val
.hash_table
);
100 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
101 MalVal
*kname
= malval_new_string((char *)key
);
102 g_array_append_val(seq
->val
.array
, kname
);
103 MalVal
*new_val
= EVAL((MalVal
*)value
, env
);
104 g_array_append_val(seq
->val
.array
, new_val
);
106 return _hash_map(seq
);
108 //g_print("EVAL scalar: %s\n", _pr_str(ast,1));
113 MalVal
*EVAL(MalVal
*ast
, Env
*env
) {
116 if (!ast
|| mal_error
) return NULL
;
117 //g_print("EVAL: %s\n", _pr_str(ast,1));
118 if (ast
->type
!= MAL_LIST
) {
119 return eval_ast(ast
, env
);
121 if (!ast
|| mal_error
) return NULL
;
124 //g_print("EVAL apply list: %s\n", _pr_str(ast,1));
125 ast
= macroexpand(ast
, env
);
126 if (!ast
|| mal_error
) return NULL
;
127 if (ast
->type
!= MAL_LIST
) {
128 return eval_ast(ast
, env
);
130 if (_count(ast
) == 0) { return ast
; }
133 MalVal
*a0
= _nth(ast
, 0);
134 if ((a0
->type
& MAL_SYMBOL
) &&
135 strcmp("def!", a0
->val
.string
) == 0) {
136 //g_print("eval apply def!\n");
137 MalVal
*a1
= _nth(ast
, 1),
139 MalVal
*res
= EVAL(a2
, env
);
140 if (mal_error
) return NULL
;
141 env_set(env
, a1
, res
);
143 } else if ((a0
->type
& MAL_SYMBOL
) &&
144 strcmp("let*", a0
->val
.string
) == 0) {
145 //g_print("eval apply let*\n");
146 MalVal
*a1
= _nth(ast
, 1),
149 assert_type(a1
, MAL_LIST
|MAL_VECTOR
,
150 "let* bindings must be list or vector");
152 assert((len
% 2) == 0, "odd number of let* bindings forms");
153 Env
*let_env
= new_env(env
, NULL
, NULL
);
154 for(i
=0; i
<len
; i
+=2) {
155 key
= g_array_index(a1
->val
.array
, MalVal
*, i
);
156 val
= g_array_index(a1
->val
.array
, MalVal
*, i
+1);
157 assert_type(key
, MAL_SYMBOL
, "let* bind to non-symbol");
158 env_set(let_env
, key
, EVAL(val
, let_env
));
163 } else if ((a0
->type
& MAL_SYMBOL
) &&
164 strcmp("quote", a0
->val
.string
) == 0) {
165 //g_print("eval apply quote\n");
167 } else if ((a0
->type
& MAL_SYMBOL
) &&
168 strcmp("quasiquote", a0
->val
.string
) == 0) {
169 //g_print("eval apply quasiquote\n");
170 MalVal
*a1
= _nth(ast
, 1);
171 ast
= quasiquote(a1
);
173 } else if ((a0
->type
& MAL_SYMBOL
) &&
174 strcmp("defmacro!", a0
->val
.string
) == 0) {
175 //g_print("eval apply defmacro!\n");
176 MalVal
*a1
= _nth(ast
, 1),
178 MalVal
*res
= EVAL(a2
, env
);
179 if (mal_error
) return NULL
;
181 env_set(env
, a1
, res
);
183 } else if ((a0
->type
& MAL_SYMBOL
) &&
184 strcmp("macroexpand", a0
->val
.string
) == 0) {
185 //g_print("eval apply macroexpand\n");
186 MalVal
*a1
= _nth(ast
, 1);
187 return macroexpand(a1
, env
);
188 } else if ((a0
->type
& MAL_SYMBOL
) &&
189 strcmp("try*", a0
->val
.string
) == 0) {
190 //g_print("eval apply try*\n");
191 MalVal
*a1
= _nth(ast
, 1);
192 MalVal
*a2
= _nth(ast
, 2);
193 MalVal
*res
= EVAL(a1
, env
);
194 if (!mal_error
) { return res
; }
195 MalVal
*a20
= _nth(a2
, 0);
196 if (strcmp("catch*", a20
->val
.string
) == 0) {
197 MalVal
*a21
= _nth(a2
, 1);
198 MalVal
*a22
= _nth(a2
, 2);
199 Env
*catch_env
= new_env(env
,
201 _listX(1, mal_error
));
202 //malval_free(mal_error);
204 res
= EVAL(a22
, catch_env
);
209 } else if ((a0
->type
& MAL_SYMBOL
) &&
210 strcmp("do", a0
->val
.string
) == 0) {
211 //g_print("eval apply do\n");
212 eval_ast(_slice(ast
, 1, _count(ast
)-1), env
);
215 } else if ((a0
->type
& MAL_SYMBOL
) &&
216 strcmp("if", a0
->val
.string
) == 0) {
217 //g_print("eval apply if\n");
218 MalVal
*a1
= _nth(ast
, 1);
219 MalVal
*cond
= EVAL(a1
, env
);
220 if (!cond
|| mal_error
) return NULL
;
221 if (cond
->type
& (MAL_FALSE
|MAL_NIL
)) {
222 // eval false slot form
223 if (ast
->val
.array
->len
> 3) {
229 // eval true slot form
233 } else if ((a0
->type
& MAL_SYMBOL
) &&
234 strcmp("fn*", a0
->val
.string
) == 0) {
235 //g_print("eval apply fn*\n");
236 MalVal
*mf
= malval_new(MAL_FUNCTION_MAL
, NULL
);
238 mf
->val
.func
.evaluator
= EVAL
;
239 mf
->val
.func
.args
= _nth(ast
, 1);
240 mf
->val
.func
.body
= _nth(ast
, 2);
241 mf
->val
.func
.env
= env
;
244 //g_print("eval apply\n");
245 MalVal
*el
= eval_ast(ast
, env
);
246 if (!el
|| mal_error
) { return NULL
; }
247 MalVal
*f
= _first(el
),
249 assert_type(f
, MAL_FUNCTION_C
|MAL_FUNCTION_MAL
,
250 "cannot apply '%s'", _pr_str(f
,1));
251 if (f
->type
& MAL_FUNCTION_MAL
) {
252 ast
= f
->val
.func
.body
;
253 env
= new_env(f
->val
.func
.env
, f
->val
.func
.args
, args
);
256 return _apply(f
, args
);
264 char *PRINT(MalVal
*exp
) {
266 fprintf(stderr
, "Error: %s\n", mal_error
->val
.string
);
267 malval_free(mal_error
);
271 return _pr_str(exp
,1);
277 MalVal
*RE(Env
*env
, char *prompt
, char *str
) {
279 ast
= READ(prompt
, str
);
280 if (!ast
|| mal_error
) return NULL
;
281 exp
= EVAL(ast
, env
);
283 malval_free(ast
); // Free input structure
288 // Setup the initial REPL environment
291 MalVal
*do_eval(MalVal
*ast
) { return EVAL(ast
, repl_env
); }
293 void init_repl_env(int argc
, char *argv
[]) {
294 repl_env
= new_env(NULL
, NULL
, NULL
);
296 // core.c: defined using C
298 for(i
=0; i
< (sizeof(core_ns
) / sizeof(core_ns
[0])); i
++) {
300 malval_new_symbol(core_ns
[i
].name
),
301 malval_new_function(core_ns
[i
].func
, core_ns
[i
].arg_cnt
));
304 malval_new_symbol("eval"),
305 malval_new_function((void*(*)(void *))do_eval
, 1));
307 MalVal
*_argv
= _listX(0);
308 for (i
=2; i
< argc
; i
++) {
309 MalVal
*arg
= malval_new_string(argv
[i
]);
310 g_array_append_val(_argv
->val
.array
, arg
);
312 env_set(repl_env
, malval_new_symbol("*ARGV*"), _argv
);
314 // core.mal: defined using the language itself
315 RE(repl_env
, "", "(def! not (fn* (a) (if a false true)))");
317 "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
318 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)))))))");
319 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))))))))");
322 int main(int argc
, char *argv
[])
328 // Set the initial prompt and environment
329 snprintf(prompt
, sizeof(prompt
), "user> ");
330 init_repl_env(argc
, argv
);
333 char *cmd
= g_strdup_printf("(load-file \"%s\")", argv
[1]);
334 RE(repl_env
, "", cmd
);
340 exp
= RE(repl_env
, prompt
, NULL
);
341 if (mal_error
&& strcmp("EOF", mal_error
->val
.string
) == 0) {
347 g_print("%s\n", output
);
348 free(output
); // Free output string
351 //malval_free(exp); // Free evaluated expression