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