blob: e591ed324dff5fb58d9b7c8c48bbd58d4bbb52f1 [file] [log] [blame]
swissChili8cfb7c42021-04-18 21:17:58 -07001/* -*- mode:c -*- */
2
swissChilica107a02021-04-14 12:07:30 -07003#include "compiler.h"
swissChiliddc97542021-07-04 11:47:42 -07004#include "gc.h"
swissChili7e1393c2021-07-07 12:59:12 -07005#include "lib/std.h"
6#include "lisp.h"
7#include "plat/plat.h"
swissChilica107a02021-04-14 12:07:30 -07008
9#include <dasm_proto.h>
10#include <dasm_x86.h>
11
swissChili7e1393c2021-07-07 12:59:12 -070012#include <libgen.h>
13#include <stdio.h>
swissChili923b5362021-05-09 20:31:43 -070014#include <stdlib.h>
15#include <string.h>
16
swissChili53472e82021-05-08 16:06:32 -070017#define value_size sizeof(value_t)
swissChilica107a02021-04-14 12:07:30 -070018
19|.arch x86;
20
21|.macro setup, nvars;
swissChili53e7cd12021-08-02 21:55:53 -070022|1:;
swissChili484295d2021-07-09 21:25:55 -070023| push ebp;
swissChilica107a02021-04-14 12:07:30 -070024| mov ebp, esp;
swissChili8cfb7c42021-04-18 21:17:58 -070025| sub esp, (value_size * nvars);
swissChilica107a02021-04-14 12:07:30 -070026|.endmacro;
27
28|.macro cleanup;
29| mov esp, ebp;
30| pop ebp;
31| ret;
32|.endmacro;
33
swissChili484295d2021-07-09 21:25:55 -070034|.macro call_extern, address;
swissChili53e7cd12021-08-02 21:55:53 -070035| call &address;
swissChili67bdf282021-06-06 18:46:08 -070036|.endmacro;
37
swissChilica107a02021-04-14 12:07:30 -070038dasm_State *d;
39unsigned int npc = 8;
40
swissChili9e57da42021-06-15 22:22:46 -070041|.macro run_gc;
swissChilie9fec8b2021-06-22 13:59:33 -070042| mov eax, esp;
swissChili9e57da42021-06-15 22:22:46 -070043| push ebp;
swissChilie9fec8b2021-06-22 13:59:33 -070044| push eax;
swissChili9e57da42021-06-15 22:22:46 -070045| mov eax, _do_gc;
46| call eax;
47|.endmacro;
swissChili6d6525e2021-06-15 21:20:53 -070048
swissChili53472e82021-05-08 16:06:32 -070049struct function *find_function(struct environment *env, char *name)
swissChilica107a02021-04-14 12:07:30 -070050{
swissChilif68671f2021-07-05 14:14:44 -070051 struct function *f;
swissChilica107a02021-04-14 12:07:30 -070052
swissChilif68671f2021-07-05 14:14:44 -070053 for (f = env->first; f && strcmp(f->name, name); f = f->prev)
swissChilica107a02021-04-14 12:07:30 -070054 {
swissChilica107a02021-04-14 12:07:30 -070055 }
56
57 return f;
58}
59
swissChili67bdf282021-06-06 18:46:08 -070060unsigned int local_alloc(struct local *local)
61{
62 for (int i = 0; i < local->num_stack_slots; i++)
63 {
64 if (local->stack_slots[i] == false)
65 {
66 local->stack_slots[i] = true;
67
68 if (i >= local->num_stack_entries)
69 local->num_stack_entries++;
70
71 return i;
72 }
73 }
74
75 int old_size = local->num_stack_slots;
76 local->num_stack_slots += 4;
swissChili7e1393c2021-07-07 12:59:12 -070077 local->stack_slots =
78 realloc(local->stack_slots, local->num_stack_slots * sizeof(bool));
swissChili67bdf282021-06-06 18:46:08 -070079 // unreadable: set the remaining slots to unused
80 memset(local->stack_slots + old_size, 0, local->num_stack_slots - old_size);
81 local->stack_slots[old_size] = true;
82
83 return old_size;
84}
85
86void local_free(struct local *local, unsigned int slot)
87{
88 local->stack_slots[slot] = false;
89}
90
swissChili708d4c42021-07-04 17:40:07 -070091void del_local(struct local *local)
92{
93 free(local->stack_slots);
94
95 for (struct variable *next, *f = local->first; f; f = next)
96 {
97 next = f->prev;
98 free(f);
99 }
100}
101
102void del_env(struct environment *env)
103{
104 for (struct function *next, *f = env->first; f; f = next)
105 {
106 next = f->prev;
107 // We're not gonna bother munmap()ing the function
108 free(f);
109 }
swissChilif68671f2021-07-05 14:14:44 -0700110
111 for (struct loaded_file *next, *l = env->first_loaded; l; l = next)
112 {
113 next = l->previous;
114 free(l->resolved_path);
115 free(l);
116 }
swissChili7e1393c2021-07-07 12:59:12 -0700117
118 free(env);
swissChilif68671f2021-07-05 14:14:44 -0700119}
120
121void add_load(struct environment *env, char *path)
122{
123 static char buffer[512];
124 long size = readlink(path, buffer, 512);
125 buffer[size] = '\0';
126 char *resolved = strdup(buffer);
127
128 struct loaded_file *f = malloc(sizeof(struct loaded_file));
129 f->resolved_path = resolved;
130 f->previous = env->first_loaded;
131 env->first_loaded = f;
swissChili708d4c42021-07-04 17:40:07 -0700132}
133
swissChili6d02af42021-08-05 19:49:01 -0700134struct error compile_function(value_t args, enum namespace namespace,
135 struct environment *env,
136 struct local *local_out,
137 struct local *local_parent,
138 struct args **args_out, char *name,
139 char *path,
140 dasm_State **state)
swissChilif1ba8c12021-07-02 18:45:38 -0700141{
swissChili6d02af42021-08-05 19:49:01 -0700142 E_INIT();
143
swissChilif1ba8c12021-07-02 18:45:38 -0700144 dasm_State *d;
145 dasm_State **Dst = &d;
146
swissChili484295d2021-07-09 21:25:55 -0700147 |.section code, imports;
swissChilif1ba8c12021-07-02 18:45:38 -0700148 dasm_init(&d, DASM_MAXSECTION);
149
150 |.globals lbl_;
151 void *labels[lbl__MAX];
152 dasm_setupglobal(&d, labels, lbl__MAX);
153
154 |.actionlist lisp_actions;
155 dasm_setup(&d, lisp_actions);
156
157 struct local local;
158 local.parent = NULL;
159 local.first = NULL;
160 local.num_vars = 0;
161 local.npc = 8;
162 local.nextpc = 0;
163 local.stack_slots = malloc(sizeof(bool) * 4);
164 memset(local.stack_slots, 0, sizeof(bool) * 4);
165 local.num_stack_slots = 4;
166 local.num_stack_entries = 0;
swissChiliddc97542021-07-04 11:47:42 -0700167 local.num_closure_slots = 0;
168 local.parent = local_parent;
swissChili74348422021-07-04 13:23:24 -0700169 local.current_function_name = name;
swissChili7e1393c2021-07-07 12:59:12 -0700170 local.current_file_path = path;
swissChilif1ba8c12021-07-02 18:45:38 -0700171
172 dasm_growpc(&d, local.npc);
173
swissChilif1ba8c12021-07-02 18:45:38 -0700174 value_t arglist = car(args);
175 value_t body = cdr(args);
176
swissChili15f1cae2021-07-05 19:08:47 -0700177 // This will add the arguments to local too.
swissChili6d02af42021-08-05 19:49:01 -0700178 struct args *ar;
179 TRY(list_to_args(env, arglist, &local, &ar));
swissChili15f1cae2021-07-05 19:08:47 -0700180 local.args = ar;
swissChili74348422021-07-04 13:23:24 -0700181
swissChili15f1cae2021-07-05 19:08:47 -0700182 if (!ar)
swissChilif1ba8c12021-07-02 18:45:38 -0700183 {
swissChili6d02af42021-08-05 19:49:01 -0700184 NEARVAL(arglist);
185 THROW(EMALFORMED, "Malformed argument list");
swissChilif1ba8c12021-07-02 18:45:38 -0700186 }
187
188 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
189 {
190 walk_and_alloc(&local, car(body_));
191 }
192
swissChili484295d2021-07-09 21:25:55 -0700193 | setup (local.num_stack_entries);
swissChilif1ba8c12021-07-02 18:45:38 -0700194
195 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
196 local.num_stack_entries = 0;
197
198 for (; !nilp(body); body = cdr(body))
199 {
swissChilib51552c2021-08-03 10:23:37 -0700200 bool tail = nilp(cdr(body));
swissChili6d02af42021-08-05 19:49:01 -0700201 TRY(compile_expression(env, &local, car(body), tail, Dst));
swissChilif1ba8c12021-07-02 18:45:38 -0700202 }
203
204 | cleanup;
205
206 if (local_out)
207 *local_out = local;
208
swissChili15f1cae2021-07-05 19:08:47 -0700209 if (args_out)
210 *args_out = ar;
swissChilif1ba8c12021-07-02 18:45:38 -0700211
swissChili6d02af42021-08-05 19:49:01 -0700212 *state = d;
213
214 OKAY();
swissChilif1ba8c12021-07-02 18:45:38 -0700215}
216
swissChili6d02af42021-08-05 19:49:01 -0700217struct error compile_tl(value_t val, struct environment *env, char *fname)
swissChilica107a02021-04-14 12:07:30 -0700218{
swissChili6d02af42021-08-05 19:49:01 -0700219 E_INIT();
220
221 NEARVAL(val);
222
swissChili53472e82021-05-08 16:06:32 -0700223 if (!listp(val))
swissChili6d02af42021-08-05 19:49:01 -0700224 {
225 THROW(EEXPECTED, "Top level form must be a list");
226 }
swissChilica107a02021-04-14 12:07:30 -0700227
swissChili53472e82021-05-08 16:06:32 -0700228 value_t form = car(val);
229 value_t args = cdr(val);
230
swissChili2999dd12021-07-02 14:19:53 -0700231 if (symstreq(form, "defun") || symstreq(form, "defmacro"))
swissChili8fc5e2f2021-04-22 13:45:10 -0700232 {
swissChili2999dd12021-07-02 14:19:53 -0700233 enum namespace namespace = NS_FUNCTION;
234
235 if (symstreq(form, "defmacro"))
swissChilia89ee442021-08-04 20:54:51 -0700236 namespace = NS_MACRO;
swissChili2999dd12021-07-02 14:19:53 -0700237
swissChili8fc5e2f2021-04-22 13:45:10 -0700238 struct local local;
swissChili15f1cae2021-07-05 19:08:47 -0700239 struct args *a;
swissChili74348422021-07-04 13:23:24 -0700240 char *name = (char *)(car(args) ^ SYMBOL_TAG);
swissChilif68671f2021-07-05 14:14:44 -0700241
swissChili6d02af42021-08-05 19:49:01 -0700242 dasm_State *d;
243 TRY(compile_function(cdr(args), namespace, env, &local,
244 NULL, &a, name, fname, &d));
swissChilia820dea2021-05-09 16:46:55 -0700245
swissChili7e1393c2021-07-07 12:59:12 -0700246 add_function(env, name, link_program(&d), a, namespace);
swissChili8fc5e2f2021-04-22 13:45:10 -0700247
swissChili53472e82021-05-08 16:06:32 -0700248 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700249 del_local(&local);
swissChili67bdf282021-06-06 18:46:08 -0700250 }
swissChilif68671f2021-07-05 14:14:44 -0700251 else if (symstreq(form, "progn"))
252 {
253 for (value_t val = args; !nilp(val); val = cdr(val))
254 {
swissChili6d02af42021-08-05 19:49:01 -0700255 TRY(compile_tl(car(val), env, fname));
swissChilif68671f2021-07-05 14:14:44 -0700256 }
257 }
swissChili484295d2021-07-09 21:25:55 -0700258 else if (symstreq(form, "load"))
259 {
260 if (length(args) != 1)
261 {
swissChili6d02af42021-08-05 19:49:01 -0700262 NEARVAL(args);
263 THROW(EARGS, "load expects exactly 1 argument, %d given",
264 length(args));
swissChili484295d2021-07-09 21:25:55 -0700265 }
266 load_relative(env, fname, car(args));
267 }
swissChili6d02af42021-08-05 19:49:01 -0700268
269 OKAY();
swissChili67bdf282021-06-06 18:46:08 -0700270}
271
272void walk_and_alloc(struct local *local, value_t body)
273{
swissChilib51552c2021-08-03 10:23:37 -0700274 // TODO: handle macros
swissChili67bdf282021-06-06 18:46:08 -0700275 if (!listp(body))
276 return;
277
278 value_t args = cdr(body);
279
280 if (symstreq(car(body), "let1"))
281 {
282 int slot = local_alloc(local);
283
284 value_t expr = cdr(args);
swissChilif1ba8c12021-07-02 18:45:38 -0700285 for (; !nilp(expr); expr = cdr(expr))
286 {
swissChiliddc97542021-07-04 11:47:42 -0700287 walk_and_alloc(local, car(expr));
swissChilif1ba8c12021-07-02 18:45:38 -0700288 }
swissChili67bdf282021-06-06 18:46:08 -0700289
290 local_free(local, slot);
291 }
swissChilif1ba8c12021-07-02 18:45:38 -0700292 else if (symstreq(car(body), "lambda"))
293 {
294 // We don't want to walk the lambda because it's another function. When
295 // the lambda is compiled it will be walked.
296 return;
297 }
swissChili67bdf282021-06-06 18:46:08 -0700298 else
299 {
300 for (; !nilp(args); args = cdr(args))
301 {
302 walk_and_alloc(local, car(args));
303 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700304 }
305}
306
swissChilif68671f2021-07-05 14:14:44 -0700307bool load(struct environment *env, char *path)
swissChili8fc5e2f2021-04-22 13:45:10 -0700308{
swissChilif68671f2021-07-05 14:14:44 -0700309 if (!file_exists(path))
310 return false;
311
312 add_load(env, path);
313
swissChilib8fd4712021-06-23 15:32:04 -0700314 unsigned char pool = make_pool();
315 unsigned char pop = push_pool(pool);
316
swissChilif68671f2021-07-05 14:14:44 -0700317 struct istream *is = new_fistream(path, false);
318 if (!is)
319 return false;
320
swissChili8fc5e2f2021-04-22 13:45:10 -0700321 value_t val;
swissChili53472e82021-05-08 16:06:32 -0700322
swissChili6d02af42021-08-05 19:49:01 -0700323 while (IS_OKAY(read1(is, &val)))
swissChili8fc5e2f2021-04-22 13:45:10 -0700324 {
swissChili6d02af42021-08-05 19:49:01 -0700325 if (!IS_OKAY(compile_tl(val, env, path)))
326 break;
swissChili8fc5e2f2021-04-22 13:45:10 -0700327 }
swissChilif3e7f182021-04-20 13:57:22 -0700328
swissChilif68671f2021-07-05 14:14:44 -0700329 del_fistream(is);
swissChilib8fd4712021-06-23 15:32:04 -0700330 pop_pool(pop);
331
swissChilif68671f2021-07-05 14:14:44 -0700332 return true;
333}
334
swissChili7e1393c2021-07-07 12:59:12 -0700335value_t load_relative(struct environment *env, char *to, value_t name)
336{
337 if (!stringp(name))
338 return nil;
339
340 char *new_path = (char *)(name ^ STRING_TAG);
341 char *relative_to = strdup(to);
342 char full_path[512];
343
344 snprintf(full_path, 512, "%s/%s", dirname(relative_to), new_path);
345
346 if (load(env, full_path))
347 return t;
348 else
349 return nil;
350}
351
swissChili6d02af42021-08-05 19:49:01 -0700352struct error compile_file(char *filename, struct environment **e)
swissChilif68671f2021-07-05 14:14:44 -0700353{
swissChili6d02af42021-08-05 19:49:01 -0700354 E_INIT();
355
swissChilif68671f2021-07-05 14:14:44 -0700356 value_t val;
swissChili7e1393c2021-07-07 12:59:12 -0700357 struct environment *env = malloc(sizeof(struct environment));
358 env->first = NULL;
359 env->first_loaded = NULL;
swissChilif68671f2021-07-05 14:14:44 -0700360
swissChili7e1393c2021-07-07 12:59:12 -0700361 add_load(env, filename);
swissChili6d02af42021-08-05 19:49:01 -0700362 TRY(load_std(env));
swissChilif68671f2021-07-05 14:14:44 -0700363
swissChili7e1393c2021-07-07 12:59:12 -0700364 bool ok_ = load(env, filename);
swissChilif68671f2021-07-05 14:14:44 -0700365
swissChili6d02af42021-08-05 19:49:01 -0700366 if (!ok_)
367 {
368 free(env);
369 THROWSAFE(ENOTFOUND);
370 }
swissChilif68671f2021-07-05 14:14:44 -0700371
swissChili6d02af42021-08-05 19:49:01 -0700372 *e = env;
373
374 OKAY();
swissChilica107a02021-04-14 12:07:30 -0700375}
swissChilib3ca4fb2021-04-20 10:33:00 -0700376
swissChili53472e82021-05-08 16:06:32 -0700377int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700378{
swissChili53472e82021-05-08 16:06:32 -0700379 int n = local->nextpc++;
380 if (n > local->npc)
381 {
382 local->npc += 16;
383 dasm_growpc(Dst, local->npc);
384 }
385 return n;
386}
387
swissChili6d02af42021-08-05 19:49:01 -0700388struct error compile_backquote(struct environment *env, struct local *local,
389 value_t val, dasm_State **Dst)
swissChili6b47b6d2021-06-30 22:08:55 -0700390{
swissChili6d02af42021-08-05 19:49:01 -0700391 E_INIT();
392
swissChili6b47b6d2021-06-30 22:08:55 -0700393 if (!listp(val))
394 {
395 | mov eax, (val);
396 }
397 else
398 {
swissChili7e1393c2021-07-07 12:59:12 -0700399 value_t fsym = car(val), args = cdr(val);
swissChili9d151e62021-08-04 13:11:45 -0700400 int nargs = length(args),
401 n = length(val);
swissChili6b47b6d2021-06-30 22:08:55 -0700402
swissChili6d02af42021-08-05 19:49:01 -0700403 NEARVAL(val);
404
swissChili9d151e62021-08-04 13:11:45 -0700405 if (symstreq(fsym, "unquote"))
406 {
407 if (nargs != 1)
408 {
swissChili6d02af42021-08-05 19:49:01 -0700409 THROW(EARGS, "unquote (or ,) takes exactly 1 argument");
swissChili9d151e62021-08-04 13:11:45 -0700410 }
411
swissChili6d02af42021-08-05 19:49:01 -0700412 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili9d151e62021-08-04 13:11:45 -0700413 }
414 else
415 {
416 | push nil;
417
418 for (int i = n - 1; i >= 0; i--)
419 {
swissChili6d02af42021-08-05 19:49:01 -0700420 TRY(compile_backquote(env, local, elt(val, i), Dst));
swissChili9d151e62021-08-04 13:11:45 -0700421 | push eax;
422 | call_extern cons;
423 | add esp, 8;
424
425 // Remove unnecessary pop
426 | push eax;
427 }
swissChilia89ee442021-08-04 20:54:51 -0700428 | pop eax;
swissChili9d151e62021-08-04 13:11:45 -0700429 }
swissChili6b47b6d2021-06-30 22:08:55 -0700430 }
swissChili6d02af42021-08-05 19:49:01 -0700431
432 OKAY();
swissChili6b47b6d2021-06-30 22:08:55 -0700433}
434
swissChili7e1393c2021-07-07 12:59:12 -0700435value_t eval(struct environment *env, value_t form)
436{
437 // Eval!
438 value_t function = cons(nil, cons(form, nil));
439
440 struct local local;
441 struct args *args;
442
swissChili6d02af42021-08-05 19:49:01 -0700443 dasm_State *d;
444 struct error err;
445
446 if (!IS_OKAY((err = compile_function(function, NS_ANONYMOUS, env, &local, NULL,
447 &args, NULL, "/", &d))))
448 {
449 ereport(err);
450 return nil;
451 }
swissChili7e1393c2021-07-07 12:59:12 -0700452
453 del_local(&local);
454
455 value_t (*f)() = link_program(&d);
456 return f();
457}
458
swissChili6d02af42021-08-05 19:49:01 -0700459struct error compile_variable(struct variable *v, dasm_State *Dst)
swissChiliddc97542021-07-04 11:47:42 -0700460{
swissChili6d02af42021-08-05 19:49:01 -0700461 E_INIT();
swissChiliddc97542021-07-04 11:47:42 -0700462 switch (v->type)
463 {
464 case V_ARGUMENT:
swissChili7e1393c2021-07-07 12:59:12 -0700465 | mov eax, dword[ebp + (value_size * (v->number + 2))];
swissChiliddc97542021-07-04 11:47:42 -0700466 break;
467 case V_BOUND:
swissChili7e1393c2021-07-07 12:59:12 -0700468 | mov eax, dword[ebp - ((v->number + 1) * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700469 break;
470 case V_FREE:
471 // edi is the closure context pointer
swissChili7e1393c2021-07-07 12:59:12 -0700472 | mov eax, dword[edi + (v->number * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700473 break;
474 default:
swissChili6d02af42021-08-05 19:49:01 -0700475 THROW(EUNIMPL, "Sorry, can only access V_ARGUMENT, V_BOUND, and V_FREE vars");
swissChiliddc97542021-07-04 11:47:42 -0700476 }
swissChili6d02af42021-08-05 19:49:01 -0700477 OKAY();
swissChiliddc97542021-07-04 11:47:42 -0700478}
479
swissChili6d02af42021-08-05 19:49:01 -0700480struct error compile_expression(struct environment *env, struct local *local,
481 value_t val, bool tail, dasm_State **Dst)
swissChili53472e82021-05-08 16:06:32 -0700482{
swissChili6d02af42021-08-05 19:49:01 -0700483 E_INIT();
484
485 NEARVAL(val);
486
swissChili7e1393c2021-07-07 12:59:12 -0700487 if (symstreq(val, "nil") || nilp(val))
swissChili53472e82021-05-08 16:06:32 -0700488 {
489 | mov eax, (nil);
490 }
swissChili923b5362021-05-09 20:31:43 -0700491 else if (symstreq(val, "t"))
492 {
493 | mov eax, (t);
494 }
495 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700496 {
497 | mov eax, val;
498 }
swissChili53472e82021-05-08 16:06:32 -0700499 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700500 {
swissChili53472e82021-05-08 16:06:32 -0700501 value_t fsym = car(val);
502 value_t args = cdr(val);
503 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700504
swissChili53472e82021-05-08 16:06:32 -0700505 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700506 {
swissChili6d02af42021-08-05 19:49:01 -0700507 THROW(EEXPECTED, "Function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700508 }
509
swissChili53472e82021-05-08 16:06:32 -0700510 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700511 {
swissChili53472e82021-05-08 16:06:32 -0700512 if (nargs < 2 || nargs > 3)
swissChili6d02af42021-08-05 19:49:01 -0700513 {
514 THROW(EARGS, "Must give at least 2 arguments to if");
515 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700516
swissChili6d02af42021-08-05 19:49:01 -0700517 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili53472e82021-05-08 16:06:32 -0700518 int false_label = nextpc(local, Dst),
519 after_label = nextpc(local, Dst);
520
521 // result is in eax
522 | cmp eax, (nil);
swissChili484295d2021-07-09 21:25:55 -0700523 | je =>false_label;
swissChili53472e82021-05-08 16:06:32 -0700524
swissChili6d02af42021-08-05 19:49:01 -0700525 TRY(compile_expression(env, local, elt(args, 1), tail, Dst));
swissChili484295d2021-07-09 21:25:55 -0700526 | jmp =>after_label;
527 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700528 if (nargs == 3)
swissChili6d02af42021-08-05 19:49:01 -0700529 TRY(compile_expression(env, local, elt(args, 2), tail, Dst));
swissChili484295d2021-07-09 21:25:55 -0700530 |=>after_label:;
swissChili53472e82021-05-08 16:06:32 -0700531 }
swissChilia89ee442021-08-04 20:54:51 -0700532 else if (symstreq(fsym, "and") || symstreq(fsym, "or"))
533 {
534 bool or = symstreq(fsym, "or"); // false == and
535
536 // Boolean and and or, short circuit like &&/||
537 if (nargs < 1)
538 {
swissChili6d02af42021-08-05 19:49:01 -0700539 THROW(EARGS, "and & or require at least 1 argument.");
swissChilia89ee442021-08-04 20:54:51 -0700540 }
541
542 int after = nextpc(local, Dst);
543
544 for (; !nilp(args); args = cdr(args))
545 {
swissChili6d02af42021-08-05 19:49:01 -0700546 NEARVAL(args);
547
548 TRY(compile_expression(env, local, car(args), false, Dst));
swissChilia89ee442021-08-04 20:54:51 -0700549 if (!nilp(cdr(args)))
550 {
551 | cmp eax, nil;
552 if (or)
553 {
swissChilifbf525f2021-08-04 21:28:07 -0700554 | jne =>after;
swissChilia89ee442021-08-04 20:54:51 -0700555 }
556 else
557 {
swissChilifbf525f2021-08-04 21:28:07 -0700558 | je =>after;
swissChilia89ee442021-08-04 20:54:51 -0700559 }
560 }
561 }
562
563 |=>after:;
564 }
swissChilif68671f2021-07-05 14:14:44 -0700565 else if (symstreq(fsym, "progn"))
566 {
567 for (value_t val = args; !nilp(val); val = cdr(val))
568 {
swissChili6d02af42021-08-05 19:49:01 -0700569 NEARVAL(args);
570
swissChilib51552c2021-08-03 10:23:37 -0700571 bool t = tail && nilp(cdr(val));
swissChili6d02af42021-08-05 19:49:01 -0700572 TRY(compile_expression(env, local, car(val), t, Dst));
swissChilif68671f2021-07-05 14:14:44 -0700573 }
574 }
swissChili67bdf282021-06-06 18:46:08 -0700575 else if (symstreq(fsym, "let1"))
576 {
577 if (nargs < 2)
578 {
swissChili6d02af42021-08-05 19:49:01 -0700579 THROW(EARGS, "Must give at least 2 arguments to let1");
swissChili67bdf282021-06-06 18:46:08 -0700580 }
581 value_t binding = car(args);
582 value_t rest = cdr(args);
583
swissChili6d02af42021-08-05 19:49:01 -0700584 NEARVAL(binding);
swissChili67bdf282021-06-06 18:46:08 -0700585 if (length(binding) != 2)
586 {
swissChili6d02af42021-08-05 19:49:01 -0700587 THROW(EARGS, "Binding list in let1 must contain exactly two entries");
swissChili67bdf282021-06-06 18:46:08 -0700588 }
589
swissChili6d02af42021-08-05 19:49:01 -0700590 NEARVAL(rest);
591
swissChili67bdf282021-06-06 18:46:08 -0700592 value_t name = car(binding);
593 value_t value = car(cdr(binding));
594
swissChili6d02af42021-08-05 19:49:01 -0700595 TRY(compile_expression(env, local, value, false, Dst));
swissChili67bdf282021-06-06 18:46:08 -0700596
597 int i = local_alloc(local);
598
599 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
600
swissChili7e1393c2021-07-07 12:59:12 -0700601 | mov dword[ebp - ((i + 1) * value_size)], eax;
swissChili67bdf282021-06-06 18:46:08 -0700602
603 for (; !nilp(rest); rest = cdr(rest))
604 {
swissChilib51552c2021-08-03 10:23:37 -0700605 bool t = tail && nilp(cdr(rest));
swissChili6d02af42021-08-05 19:49:01 -0700606 NEARVAL(rest);
607 TRY(compile_expression(env, local, car(rest), t, Dst));
swissChili67bdf282021-06-06 18:46:08 -0700608 }
609
610 local_free(local, i);
611 }
swissChilie9fec8b2021-06-22 13:59:33 -0700612 else if (symstreq(fsym, "gc"))
613 {
614 if (nargs)
615 {
swissChili6d02af42021-08-05 19:49:01 -0700616 THROW(EARGS, "gc takes no arguments");
swissChilie9fec8b2021-06-22 13:59:33 -0700617 }
618
619 | run_gc;
620 }
swissChili6b47b6d2021-06-30 22:08:55 -0700621 else if (symstreq(fsym, "quote"))
622 {
623 if (nargs != 1)
swissChili6d02af42021-08-05 19:49:01 -0700624 THROW(EARGS, "quote should take exactly 1 argument");
swissChili6b47b6d2021-06-30 22:08:55 -0700625
626 // Simple!
627 | mov eax, (car(args));
628 }
629 else if (symstreq(fsym, "backquote"))
630 {
631 if (nargs != 1)
swissChili6d02af42021-08-05 19:49:01 -0700632 THROW(EARGS, "backquote should take exactly 1 argument");
swissChili6b47b6d2021-06-30 22:08:55 -0700633
swissChili6d02af42021-08-05 19:49:01 -0700634 TRY(compile_backquote(env, local, car(args), Dst));
swissChili6b47b6d2021-06-30 22:08:55 -0700635 }
swissChili74348422021-07-04 13:23:24 -0700636 else if (symstreq(fsym, "function"))
637 {
638 if (nargs != 1)
639 {
swissChili6d02af42021-08-05 19:49:01 -0700640 THROW(EARGS, "function should take exactly 1 argument");
swissChili74348422021-07-04 13:23:24 -0700641 }
642
swissChili6d02af42021-08-05 19:49:01 -0700643 NEARVAL(args);
swissChili74348422021-07-04 13:23:24 -0700644 if (!symbolp(car(args)))
645 {
swissChili6d02af42021-08-05 19:49:01 -0700646 THROW(EINVALID, "argument to function should be a symbol resolvable at "
swissChili7e1393c2021-07-07 12:59:12 -0700647 "compile time");
swissChili74348422021-07-04 13:23:24 -0700648 }
649
swissChilia89ee442021-08-04 20:54:51 -0700650 char *name = (char *)(car(args) ^ SYMBOL_TAG);
swissChili74348422021-07-04 13:23:24 -0700651
swissChilia89ee442021-08-04 20:54:51 -0700652 if (!strcmp(name, local->current_function_name))
653 {
654 | push 0;
655 | push local->args;
656 | push <1;
657 | call_extern create_closure;
658 }
659 else
660 {
661 struct function *f = find_function(env, name);
662
663 if (!f)
664 {
swissChili6d02af42021-08-05 19:49:01 -0700665 THROW(EINVALID, "Function `%s' does not exist", (char *)(car(args) ^ SYMBOL_TAG));
swissChilia89ee442021-08-04 20:54:51 -0700666 }
667 value_t closure = create_closure(f->code_ptr, f->args, 0);
668 | mov eax, (closure);
669 }
swissChili74348422021-07-04 13:23:24 -0700670 }
swissChili6b47b6d2021-06-30 22:08:55 -0700671 else if (symstreq(fsym, "list"))
672 {
swissChili484295d2021-07-09 21:25:55 -0700673 | push (nil);
swissChili6b47b6d2021-06-30 22:08:55 -0700674
675 for (int i = nargs - 1; i >= 0; i--)
676 {
swissChili6d02af42021-08-05 19:49:01 -0700677 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili6b47b6d2021-06-30 22:08:55 -0700678
swissChili6b47b6d2021-06-30 22:08:55 -0700679 | push eax;
swissChili53e7cd12021-08-02 21:55:53 -0700680 | call_extern cons;
swissChili6b47b6d2021-06-30 22:08:55 -0700681 | add esp, (2 * value_size);
swissChili6b47b6d2021-06-30 22:08:55 -0700682 | push eax;
683 }
swissChili6d02af42021-08-05 19:49:01 -0700684 | pop eax;
swissChili6b47b6d2021-06-30 22:08:55 -0700685 }
swissChiliddc97542021-07-04 11:47:42 -0700686 else if (symstreq(fsym, "lambda"))
687 {
688 // Compile the function with this as the parent scope
689 struct local new_local;
690 int nargs_out;
swissChili6d02af42021-08-05 19:49:01 -0700691 dasm_State *d;
692 TRY(compile_function(
693 args, NS_ANONYMOUS, env, &new_local, local, &nargs_out,
694 "recurse", local->current_file_path, &d));
swissChiliddc97542021-07-04 11:47:42 -0700695
696 // Link the function
swissChilif68671f2021-07-05 14:14:44 -0700697 void *func_ptr = link_program(&d);
swissChiliddc97542021-07-04 11:47:42 -0700698
699 // Create a closure object with the correct number of captures at
700 // runtime
swissChili484295d2021-07-09 21:25:55 -0700701 | push (new_local.num_closure_slots);
702 | push (nargs_out);
703 | push (func_ptr);
swissChili53e7cd12021-08-02 21:55:53 -0700704 | call_extern create_closure;
swissChiliddc97542021-07-04 11:47:42 -0700705 | add esp, 12;
706
707 // Walk the generated local scope for V_FREE variables, since each
708 // of these exists in our scope (or higher), evaluate it and set it
709 // as a member of the lambda capture.
710
711 for (struct variable *var = new_local.first; var; var = var->prev)
712 {
713 if (var->type == V_FREE)
714 {
715 // Closure in eax
716 | push eax;
717 // Variable now in eax
swissChili6d02af42021-08-05 19:49:01 -0700718 TRY(compile_variable(find_variable(local, var->name), Dst));
swissChiliddc97542021-07-04 11:47:42 -0700719 | push eax;
720
swissChiliddc97542021-07-04 11:47:42 -0700721 // The capture offset
swissChili484295d2021-07-09 21:25:55 -0700722 | push (var->number);
swissChili53e7cd12021-08-02 21:55:53 -0700723 | call_extern set_closure_capture_variable;
swissChiliddc97542021-07-04 11:47:42 -0700724 // Skip the value and index
725 | add esp, 8;
726 // Pop the closure back in to eax
727 | pop eax;
728 }
729 }
730
731 // Closure is still in eax
732
733 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700734 del_local(&new_local);
swissChiliddc97542021-07-04 11:47:42 -0700735 }
swissChili7e1393c2021-07-07 12:59:12 -0700736 else if (symstreq(fsym, "eval"))
737 {
738 if (nargs != 1)
739 {
swissChili6d02af42021-08-05 19:49:01 -0700740 THROW(EARGS, "eval takes exactly 1 argument");
swissChili7e1393c2021-07-07 12:59:12 -0700741 }
742
swissChili6d02af42021-08-05 19:49:01 -0700743 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700744 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700745 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700746 | call_extern eval;
swissChili7e1393c2021-07-07 12:59:12 -0700747 }
748 else if (symstreq(fsym, "load"))
749 {
750 if (nargs != 1)
751 {
swissChili6d02af42021-08-05 19:49:01 -0700752 THROW(EARGS, "load takes exactly 1 argument, %d given", nargs);
swissChili7e1393c2021-07-07 12:59:12 -0700753 }
754
swissChili6d02af42021-08-05 19:49:01 -0700755 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700756 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700757 | push (local->current_file_path);
758 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700759 | call_extern load_relative;
swissChili7e1393c2021-07-07 12:59:12 -0700760 }
swissChili53472e82021-05-08 16:06:32 -0700761 else
762 {
swissChili74348422021-07-04 13:23:24 -0700763 char *name = (char *)(fsym ^ SYMBOL_TAG);
764 struct function *func = find_function(env, name);
swissChili7e1393c2021-07-07 12:59:12 -0700765
swissChili74348422021-07-04 13:23:24 -0700766 bool is_recursive = false;
swissChili15f1cae2021-07-05 19:08:47 -0700767 struct args *nargs_needed = NULL;
swissChili53472e82021-05-08 16:06:32 -0700768
swissChili53e7cd12021-08-02 21:55:53 -0700769 // The number of arguments actually passed on the stack,
770 // i.e. all varargs are 1.
swissChilib51552c2021-08-03 10:23:37 -0700771 int real_nargs;
swissChili53e7cd12021-08-02 21:55:53 -0700772
swissChili7e1393c2021-07-07 12:59:12 -0700773 if (local->current_function_name &&
774 symstreq(fsym, local->current_function_name))
swissChilif1ba8c12021-07-02 18:45:38 -0700775 {
swissChili74348422021-07-04 13:23:24 -0700776 is_recursive = true;
swissChili15f1cae2021-07-05 19:08:47 -0700777 nargs_needed = local->args;
swissChili74348422021-07-04 13:23:24 -0700778 }
779 else
780 {
781 if (func == NULL)
782 {
swissChili6d02af42021-08-05 19:49:01 -0700783 THROW(EINVALID, "Function %s undefined", name);
swissChili74348422021-07-04 13:23:24 -0700784 }
785
swissChili15f1cae2021-07-05 19:08:47 -0700786 nargs_needed = func->args;
swissChili74348422021-07-04 13:23:24 -0700787 }
788
swissChili15f1cae2021-07-05 19:08:47 -0700789 if (!are_args_acceptable(nargs_needed, nargs))
swissChili74348422021-07-04 13:23:24 -0700790 {
swissChili6d02af42021-08-05 19:49:01 -0700791 THROW(EARGS,
792 "wrong number of args in function call: %s, "
793 "want %d args but given %d\n",
794 name, nargs_needed->num_required, nargs);
swissChilif1ba8c12021-07-02 18:45:38 -0700795 }
swissChili53472e82021-05-08 16:06:32 -0700796
swissChili53e7cd12021-08-02 21:55:53 -0700797 int total_taken = nargs_needed->num_optional +
798 nargs_needed->num_required;
799
swissChilib51552c2021-08-03 10:23:37 -0700800 real_nargs = total_taken + (nargs_needed->variadic ? 1 : 0);
swissChili53e7cd12021-08-02 21:55:53 -0700801
swissChili74348422021-07-04 13:23:24 -0700802 if (is_recursive || func->namespace == NS_FUNCTION)
swissChili53472e82021-05-08 16:06:32 -0700803 {
swissChili15f1cae2021-07-05 19:08:47 -0700804 int nargs = length(args);
805
swissChili484295d2021-07-09 21:25:55 -0700806 int line = cons_line(val);
807 char *file = cons_file(val);
808
809 if (nargs_needed->variadic)
swissChili15f1cae2021-07-05 19:08:47 -0700810 {
swissChili484295d2021-07-09 21:25:55 -0700811 | push (nil);
812 }
813
814 if (nargs > total_taken && nargs_needed->variadic)
815 {
816 // We are passing varargs, which means we need to make a list
817
818 for (int i = nargs - 1; i >= total_taken; i--)
819 {
swissChili6d02af42021-08-05 19:49:01 -0700820 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili484295d2021-07-09 21:25:55 -0700821 | push eax;
swissChili53e7cd12021-08-02 21:55:53 -0700822 | call_extern cons;
swissChili484295d2021-07-09 21:25:55 -0700823 | add esp, 8;
824 | push eax;
825 }
swissChili15f1cae2021-07-05 19:08:47 -0700826 }
827
swissChili7e1393c2021-07-07 12:59:12 -0700828 for (int i = nargs_needed->num_optional - 1;
829 i >= nargs - nargs_needed->num_required; i--)
swissChili15f1cae2021-07-05 19:08:47 -0700830 {
831 // Push the default optional values
swissChili484295d2021-07-09 21:25:55 -0700832 | push (nargs_needed->optional_arguments[i].value);
swissChili15f1cae2021-07-05 19:08:47 -0700833 }
834
swissChili484295d2021-07-09 21:25:55 -0700835 int min = MIN(nargs, total_taken);
836
837 for (int i = min - 1; i >= 0; i--)
swissChili2999dd12021-07-02 14:19:53 -0700838 {
swissChili6d02af42021-08-05 19:49:01 -0700839 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili2999dd12021-07-02 14:19:53 -0700840 | push eax;
841 }
swissChili15f1cae2021-07-05 19:08:47 -0700842
swissChili74348422021-07-04 13:23:24 -0700843 if (is_recursive)
844 {
swissChilib51552c2021-08-03 10:23:37 -0700845 if (tail)
846 {
847 // Move all the arguments pushed to the stack
848 // back up to the argument bit of the stack.
849
850 for (int i = 0; i < real_nargs; i++)
851 {
852 | pop eax;
853 | mov dword[ebp + (value_size * (i + 2))], eax;
854 }
855
856 // Jmp back to start
857 | mov esp, ebp;
858 | pop ebp;
859 | jmp <1;
860 }
861 else
862 {
863 | call <1;
864 }
swissChili74348422021-07-04 13:23:24 -0700865 }
866 else
867 {
swissChili484295d2021-07-09 21:25:55 -0700868 // | mov ebx, (func->code_addr);
869 | call_extern func->code_addr;
swissChili74348422021-07-04 13:23:24 -0700870 }
swissChili53e7cd12021-08-02 21:55:53 -0700871 | add esp, (real_nargs * value_size);
swissChili2999dd12021-07-02 14:19:53 -0700872 // result in eax
873 }
874 else if (func->namespace == NS_MACRO)
875 {
swissChili7e1393c2021-07-07 12:59:12 -0700876 // Make sure that the stuff allocated by the macro isn't in a
877 // pool
swissChilif68671f2021-07-05 14:14:44 -0700878 unsigned char pool = push_pool(0);
879
swissChili2999dd12021-07-02 14:19:53 -0700880 value_t expanded_to = call_list(func, args);
881
swissChilif68671f2021-07-05 14:14:44 -0700882 pop_pool(pool);
883
swissChili6d02af42021-08-05 19:49:01 -0700884 TRY(compile_expression(env, local, expanded_to, false, Dst));
swissChili2999dd12021-07-02 14:19:53 -0700885 }
swissChili53472e82021-05-08 16:06:32 -0700886 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700887 }
swissChili923b5362021-05-09 20:31:43 -0700888 else if (symbolp(val))
889 {
swissChili7e1393c2021-07-07 12:59:12 -0700890 if (symstreq(val, "+current-file+"))
swissChilie9fec8b2021-06-22 13:59:33 -0700891 {
swissChili7e1393c2021-07-07 12:59:12 -0700892 value_t file_name_val = strval(local->current_file_path);
893
894 | mov eax, (file_name_val);
swissChilie9fec8b2021-06-22 13:59:33 -0700895 }
swissChili7e1393c2021-07-07 12:59:12 -0700896 else
897 {
898 struct variable *v =
899 find_variable(local, (char *)(val ^ SYMBOL_TAG));
swissChili923b5362021-05-09 20:31:43 -0700900
swissChili7e1393c2021-07-07 12:59:12 -0700901 if (!v)
902 {
swissChili6d02af42021-08-05 19:49:01 -0700903 THROW(EINVALID, "Variable `%s' unbound", (char *)(val ^ SYMBOL_TAG));
swissChili7e1393c2021-07-07 12:59:12 -0700904 }
905
swissChili6d02af42021-08-05 19:49:01 -0700906 TRY(compile_variable(v, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700907 }
swissChili923b5362021-05-09 20:31:43 -0700908 }
swissChilia89ee442021-08-04 20:54:51 -0700909 else if (closurep(val))
910 {
911 | mov eax, val;
912 }
913 else
914 {
915 printval(val, 1);
swissChili6d02af42021-08-05 19:49:01 -0700916 THROW(EUNIMPL, "Don't know how to compile this, sorry.");
swissChilia89ee442021-08-04 20:54:51 -0700917 }
swissChili6d02af42021-08-05 19:49:01 -0700918
919 OKAY();
swissChilib3ca4fb2021-04-20 10:33:00 -0700920}
swissChilif3e7f182021-04-20 13:57:22 -0700921
swissChili923b5362021-05-09 20:31:43 -0700922struct variable *add_variable(struct local *local, enum var_type type,
923 char *name, int number)
924{
925 struct variable *var = malloc(sizeof(struct variable));
926 var->prev = local->first;
927 var->type = type;
928 var->name = name;
929 var->number = number;
930
931 local->first = var;
932
933 return var;
934}
935
936void destroy_local(struct local *local)
937{
938 for (struct variable *v = local->first; v;)
939 {
940 struct variable *t = v;
941 v = v->prev;
942 free(t);
943 }
944}
945
946struct variable *find_variable(struct local *local, char *name)
947{
948 struct variable *v = local->first;
949
950 for (; v && strcmp(v->name, name) != 0; v = v->prev)
swissChili7e1393c2021-07-07 12:59:12 -0700951 {
952 }
swissChili923b5362021-05-09 20:31:43 -0700953
swissChiliddc97542021-07-04 11:47:42 -0700954 if (!v)
955 {
956 if (local->parent)
957 {
958 v = find_variable(local->parent, name);
959
960 if (v)
961 {
swissChili15f1cae2021-07-05 19:08:47 -0700962 // We found this in a parent scope, add it as a V_FREE variable
963 // to skip the search.
swissChili7e1393c2021-07-07 12:59:12 -0700964 v = add_variable(local, V_FREE, name,
965 local->num_closure_slots++);
swissChiliddc97542021-07-04 11:47:42 -0700966 }
967 }
968 }
swissChili923b5362021-05-09 20:31:43 -0700969 return v;
970}
swissChili2999dd12021-07-02 14:19:53 -0700971
swissChiliddc97542021-07-04 11:47:42 -0700972extern value_t _call_list(void *addr, value_t list, value_t *edi);
swissChili2999dd12021-07-02 14:19:53 -0700973
swissChili7e1393c2021-07-07 12:59:12 -0700974value_t call_list_args(void *code_ptr, struct args *args, value_t list,
975 void *data)
swissChili2999dd12021-07-02 14:19:53 -0700976{
swissChili15f1cae2021-07-05 19:08:47 -0700977 list = deep_copy(list);
swissChili484295d2021-07-09 21:25:55 -0700978
swissChili15f1cae2021-07-05 19:08:47 -0700979 int nargs = length(list);
980
swissChili484295d2021-07-09 21:25:55 -0700981 value_t *val = &list;
swissChili15f1cae2021-07-05 19:08:47 -0700982
983 for (value_t i = list; !nilp(i); i = cdr(i))
984 {
985 val = cdrref(i);
986 }
987
988 int total_required = args->num_required + args->num_optional;
989
990 if (nargs > total_required)
991 {
992 // Take the remainder of the list and put it as the last item in the
993 // list.
994 value_t trailing = cxdr(list, total_required);
995 value_t last_item = cons(trailing, nil);
996
997 *cxdrref(&list, total_required) = last_item;
998 }
999 else if (nargs < total_required)
1000 {
1001 for (int i = nargs - args->num_required; i < args->num_optional; i++)
1002 {
1003 // Append the i-th defualt argument
1004 value_t appended = cons(args->optional_arguments[i].value, nil);
1005 *val = appended;
1006 val = cdrref(appended);
1007 }
1008 }
1009
1010 // We want to call this if we pass the correct # of arguments or less, just
1011 // not if we have already passed varargs. Appends a nil argument.
1012 if (nargs <= total_required)
1013 {
1014 // Enough real arguments but no variadic arguments. Pass a nil list.
1015 *val = cons(nil, nil);
1016 }
1017
1018 return _call_list(code_ptr, list, data);
1019}
1020
1021value_t call_list(struct function *fun, value_t list)
1022{
1023 return call_list_args(fun->code_ptr, fun->args, list, NULL);
swissChiliddc97542021-07-04 11:47:42 -07001024}
1025
1026value_t call_list_closure(struct closure *c, value_t list)
1027{
swissChili15f1cae2021-07-05 19:08:47 -07001028 return call_list_args(c->function, c->args, list, c->data);
1029}
1030
1031struct args *new_args()
1032{
1033 struct args *a = malloc(sizeof(struct args));
1034 a->num_optional = 0;
1035 a->num_required = 0;
1036 a->variadic = false;
1037
1038 return a;
1039}
1040
swissChili7e1393c2021-07-07 12:59:12 -07001041struct args *add_optional_arg(struct args *args, value_t name, value_t value)
swissChili15f1cae2021-07-05 19:08:47 -07001042{
1043 int i = args->num_optional++;
swissChili7e1393c2021-07-07 12:59:12 -07001044 args =
1045 realloc(args, sizeof(struct args) + sizeof(struct optional_argument) *
1046 args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -07001047
swissChili7e1393c2021-07-07 12:59:12 -07001048 args->optional_arguments[i] = (struct optional_argument){
1049 .value = value,
1050 .name = name,
swissChili15f1cae2021-07-05 19:08:47 -07001051 };
1052
1053 return args;
1054}
1055
1056bool are_args_acceptable(struct args *args, int number)
1057{
1058 if (args->variadic)
1059 {
1060 return number >= args->num_required;
1061 }
1062 else
1063 {
1064 return number >= args->num_required &&
swissChili7e1393c2021-07-07 12:59:12 -07001065 number <= args->num_required + args->num_optional;
swissChili15f1cae2021-07-05 19:08:47 -07001066 }
1067}
1068
swissChili6d02af42021-08-05 19:49:01 -07001069struct error list_to_args(struct environment *env, value_t list,
1070 struct local *local, struct args **a)
swissChili15f1cae2021-07-05 19:08:47 -07001071{
swissChili6d02af42021-08-05 19:49:01 -07001072 E_INIT();
1073
swissChili15f1cae2021-07-05 19:08:47 -07001074 struct args *args = new_args();
1075
1076 bool in_optional = false;
1077
1078 for (value_t i = list; !nilp(i); i = cdr(i))
1079 {
1080 value_t val = car(i);
swissChili6d02af42021-08-05 19:49:01 -07001081 NEARVAL(i);
1082
swissChili15f1cae2021-07-05 19:08:47 -07001083 if (symbolp(val))
1084 {
1085 if (!args->variadic && symstreq(val, "&"))
1086 {
1087 i = cdr(i);
1088 value_t name = car(i);
1089
1090 if (!symbolp(name))
1091 {
swissChili6d02af42021-08-05 19:49:01 -07001092 THROW(EEXPECTED, "You must provide a symbol after & in an argument list "
1093 "to bind the\n"
1094 "variadic arguments to.");
swissChili15f1cae2021-07-05 19:08:47 -07001095 }
1096
1097 args->variadic = true;
1098
1099 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
swissChili7e1393c2021-07-07 12:59:12 -07001100 args->num_optional + args->num_required);
swissChili15f1cae2021-07-05 19:08:47 -07001101
1102 continue;
1103 }
1104
1105 if (!in_optional)
1106 {
swissChili7e1393c2021-07-07 12:59:12 -07001107 add_variable(local, V_ARGUMENT, (char *)(val ^ SYMBOL_TAG),
1108 args->num_required++);
swissChili15f1cae2021-07-05 19:08:47 -07001109 }
1110 else
1111 {
1112 char *name = (char *)(val ^ SYMBOL_TAG);
1113 if (name[0] == '&')
1114 {
swissChili6d02af42021-08-05 19:49:01 -07001115 THROW(EINVALID, "Non-optional argument following optional arguments "
1116 "starts with a &\n"
1117 "did you mean to declare a variadic argument? If so "
1118 "leave a space\n"
1119 "between the & and name.");
swissChili15f1cae2021-07-05 19:08:47 -07001120 }
1121 else
1122 {
swissChili6d02af42021-08-05 19:49:01 -07001123 THROW(EINVALID, "Cannot define a non-optional argument after an "
1124 "optional one.");
swissChili15f1cae2021-07-05 19:08:47 -07001125 }
1126 }
1127 }
1128 else if (listp(val))
1129 {
swissChili6d02af42021-08-05 19:49:01 -07001130 NEARVAL(val);
1131
swissChili15f1cae2021-07-05 19:08:47 -07001132 in_optional = true;
1133 int len = length(val);
1134
1135 if (len != 2)
1136 {
swissChili6d02af42021-08-05 19:49:01 -07001137 THROW(EINVALID, "A list defining an optional value must be structured like "
1138 "(name expr)\n"
1139 "with exactly two arguments.");
swissChili15f1cae2021-07-05 19:08:47 -07001140 }
1141
1142 value_t name = car(val);
1143 value_t expr = car(cdr(val));
1144
1145 value_t function = cons(nil, cons(expr, nil));
1146
swissChili6d02af42021-08-05 19:49:01 -07001147 dasm_State *d;
1148 TRY(compile_function(function, NS_ANONYMOUS, env, NULL, NULL, NULL,
1149 NULL, local->current_file_path, &d));
swissChili15f1cae2021-07-05 19:08:47 -07001150
1151 // TODO: GC stack top!
1152 value_t (*compiled)() = link_program(&d);
1153
1154 value_t value = compiled();
1155 args = add_optional_arg(args, name, value);
1156
swissChili7e1393c2021-07-07 12:59:12 -07001157 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
1158 args->num_required + args->num_optional - 1);
swissChili15f1cae2021-07-05 19:08:47 -07001159 }
1160 }
1161
swissChili6d02af42021-08-05 19:49:01 -07001162 *a = args;
1163 OKAY();
swissChili15f1cae2021-07-05 19:08:47 -07001164}
1165
1166void display_args(struct args *args)
1167{
1168 printf("Args object taking %d require arguments and %d optionals:\n",
swissChili7e1393c2021-07-07 12:59:12 -07001169 args->num_required, args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -07001170
1171 for (int i = 0; i < args->num_optional; i++)
1172 {
swissChili7e1393c2021-07-07 12:59:12 -07001173 printf(" %d\t%s\n", i,
1174 (char *)(args->optional_arguments[i].name ^ SYMBOL_TAG));
swissChili15f1cae2021-07-05 19:08:47 -07001175 printval(args->optional_arguments[i].value, 2);
1176 }
swissChili2999dd12021-07-02 14:19:53 -07001177}