12 MalVal
*EVAL(MalVal
*ast
, Env
*env
);
13 MalVal
*macroexpand(MalVal
*ast
, Env
*env
);
16 MalVal
*READ(char prompt
[], char *str
) {
22 line
= _readline(prompt
);
29 if (!str
) { free(line
); }
34 int is_pair(MalVal
*x
) {
35 return _sequential_Q(x
) && (_count(x
) > 0);
38 MalVal
*quasiquote(MalVal
*ast
) {
40 return _listX(2, malval_new_symbol("quote"), ast
);
42 MalVal
*a0
= _nth(ast
, 0);
43 if ((a0
->type
& MAL_SYMBOL
) &&
44 strcmp("unquote", a0
->val
.string
) == 0) {
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) {
50 return _listX(3, malval_new_symbol("concat"),
52 quasiquote(_rest(ast
)));
55 return _listX(3, malval_new_symbol("cons"),
57 quasiquote(_rest(ast
)));
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
;
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
75 ast
= _apply(mac
, _rest(ast
));
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
;
91 } else if (ast
->type
== MAL_HASH_MAP
) {
92 //g_print("EVAL hash_map: %s\n", _pr_str(ast,1));
95 MalVal
*seq
= malval_new_list(MAL_LIST
,
96 g_array_sized_new(TRUE
, TRUE
, sizeof(MalVal
*),
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
);
105 return _hash_map(seq
);
107 //g_print("EVAL scalar: %s\n", _pr_str(ast,1));
112 MalVal
*EVAL(MalVal
*ast
, Env
*env
) {
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
);
120 if (!ast
|| mal_error
) return NULL
;
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
; }
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),
136 MalVal
*res
= EVAL(a2
, env
);
137 env_set(env
, a1
->val
.string
, 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),
145 assert_type(a1
, MAL_LIST
|MAL_VECTOR
,
146 "let* bindings must be list or vector");
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
));
159 } else if ((a0
->type
& MAL_SYMBOL
) &&
160 strcmp("quote", a0
->val
.string
) == 0) {
161 //g_print("eval apply quote\n");
163 } else if ((a0
->type
& MAL_SYMBOL
) &&
164 strcmp("quasiquote", a0
->val
.string
) == 0) {
165 //g_print("eval apply quasiquote\n");
166 MalVal
*a1
= _nth(ast
, 1);
167 ast
= quasiquote(a1
);
169 } else if ((a0
->type
& MAL_SYMBOL
) &&
170 strcmp("defmacro!", a0
->val
.string
) == 0) {
171 //g_print("eval apply defmacro!\n");
172 MalVal
*a1
= _nth(ast
, 1),
174 MalVal
*res
= EVAL(a2
, env
);
176 env_set(env
, a1
->val
.string
, res
);
178 } else if ((a0
->type
& MAL_SYMBOL
) &&
179 strcmp("macroexpand", a0
->val
.string
) == 0) {
180 //g_print("eval apply macroexpand\n");
181 MalVal
*a1
= _nth(ast
, 1);
182 return macroexpand(a1
, env
);
183 } else if ((a0
->type
& MAL_SYMBOL
) &&
184 strcmp(".", a0
->val
.string
) == 0) {
185 //g_print("eval apply .\n");
186 MalVal
*el
= eval_ast(_slice(ast
, 1, _count(ast
)), env
);
187 return invoke_native(el
);
188 } else if ((a0
->type
& MAL_SYMBOL
) &&
189 strcmp("do", a0
->val
.string
) == 0) {
190 //g_print("eval apply do\n");
191 eval_ast(_slice(ast
, 1, _count(ast
)-1), env
);
194 } else if ((a0
->type
& MAL_SYMBOL
) &&
195 strcmp("if", a0
->val
.string
) == 0) {
196 //g_print("eval apply if\n");
197 MalVal
*a1
= _nth(ast
, 1);
198 MalVal
*cond
= EVAL(a1
, env
);
199 if (!cond
|| mal_error
) return NULL
;
200 if (cond
->type
& (MAL_FALSE
|MAL_NIL
)) {
201 // eval false slot form
207 // eval true slot form
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
);
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
;
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
),
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
);
234 return _apply(f
, args
);
242 char *PRINT(MalVal
*exp
) {
244 fprintf(stderr
, "Error: %s\n", mal_error
->val
.string
);
245 malval_free(mal_error
);
249 return _pr_str(exp
,1);
255 MalVal
*RE(Env
*env
, char *prompt
, char *str
) {
257 ast
= READ(prompt
, str
);
258 if (!ast
|| mal_error
) return NULL
;
259 exp
= EVAL(ast
, env
);
261 malval_free(ast
); // Free input structure
266 // Setup the initial REPL environment
269 void init_repl_env(int argc
, char *argv
[]) {
270 repl_env
= new_env(NULL
, NULL
, NULL
);
272 // core.c: defined using C
274 for(i
=0; i
< (sizeof(core_ns
) / sizeof(core_ns
[0])); i
++) {
275 env_set(repl_env
, core_ns
[i
].name
,
276 malval_new_function(core_ns
[i
].func
, core_ns
[i
].arg_cnt
));
278 MalVal
*do_eval(MalVal
*ast
) { return EVAL(ast
, repl_env
); }
279 env_set(repl_env
, "eval",
280 malval_new_function((void*(*)(void *))do_eval
, 1));
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
);
287 env_set(repl_env
, "*ARGV*", _argv
);
289 // core.mal: defined using the language itself
290 RE(repl_env
, "", "(def! not (fn* (a) (if a false true)))");
292 "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
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)))))))");
294 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))))))))");
297 int main(int argc
, char *argv
[])
303 // Set the initial prompt and environment
304 snprintf(prompt
, sizeof(prompt
), "user> ");
305 init_repl_env(argc
, argv
);
308 char *cmd
= g_strdup_printf("(load-file \"%s\")", argv
[1]);
309 RE(repl_env
, "", cmd
);
315 exp
= RE(repl_env
, prompt
, NULL
);
316 if (mal_error
&& strcmp("EOF", mal_error
->val
.string
) == 0) {
322 g_print("%s\n", output
);
323 free(output
); // Free output string
326 //malval_free(exp); // Free evaluated expression