blob: bae4a391e1b2d8f649a3351470e68720804619ef [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{
swissChilia7568dc2021-08-08 16:52:52 -0700142 UNUSED(namespace);
143
swissChili6d02af42021-08-05 19:49:01 -0700144 E_INIT();
145
swissChilif1ba8c12021-07-02 18:45:38 -0700146 dasm_State *d;
147 dasm_State **Dst = &d;
148
swissChili484295d2021-07-09 21:25:55 -0700149 |.section code, imports;
swissChilif1ba8c12021-07-02 18:45:38 -0700150 dasm_init(&d, DASM_MAXSECTION);
151
152 |.globals lbl_;
153 void *labels[lbl__MAX];
154 dasm_setupglobal(&d, labels, lbl__MAX);
155
156 |.actionlist lisp_actions;
157 dasm_setup(&d, lisp_actions);
158
159 struct local local;
160 local.parent = NULL;
161 local.first = NULL;
162 local.num_vars = 0;
163 local.npc = 8;
164 local.nextpc = 0;
165 local.stack_slots = malloc(sizeof(bool) * 4);
166 memset(local.stack_slots, 0, sizeof(bool) * 4);
167 local.num_stack_slots = 4;
168 local.num_stack_entries = 0;
swissChiliddc97542021-07-04 11:47:42 -0700169 local.num_closure_slots = 0;
170 local.parent = local_parent;
swissChili74348422021-07-04 13:23:24 -0700171 local.current_function_name = name;
swissChili7e1393c2021-07-07 12:59:12 -0700172 local.current_file_path = path;
swissChilif1ba8c12021-07-02 18:45:38 -0700173
174 dasm_growpc(&d, local.npc);
175
swissChilif1ba8c12021-07-02 18:45:38 -0700176 value_t arglist = car(args);
177 value_t body = cdr(args);
178
swissChili15f1cae2021-07-05 19:08:47 -0700179 // This will add the arguments to local too.
swissChili6d02af42021-08-05 19:49:01 -0700180 struct args *ar;
181 TRY(list_to_args(env, arglist, &local, &ar));
swissChili15f1cae2021-07-05 19:08:47 -0700182 local.args = ar;
swissChili74348422021-07-04 13:23:24 -0700183
swissChili15f1cae2021-07-05 19:08:47 -0700184 if (!ar)
swissChilif1ba8c12021-07-02 18:45:38 -0700185 {
swissChili6d02af42021-08-05 19:49:01 -0700186 NEARVAL(arglist);
187 THROW(EMALFORMED, "Malformed argument list");
swissChilif1ba8c12021-07-02 18:45:38 -0700188 }
189
190 for (value_t body_ = body; !nilp(body_); body_ = cdr(body_))
191 {
swissChili36f2c692021-08-08 14:31:44 -0700192 TRY(walk_and_alloc(env, &local, carref(body_)));
swissChilif1ba8c12021-07-02 18:45:38 -0700193 }
194
swissChili484295d2021-07-09 21:25:55 -0700195 | setup (local.num_stack_entries);
swissChilif1ba8c12021-07-02 18:45:38 -0700196
197 memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool));
198 local.num_stack_entries = 0;
199
200 for (; !nilp(body); body = cdr(body))
201 {
swissChilib51552c2021-08-03 10:23:37 -0700202 bool tail = nilp(cdr(body));
swissChili6d02af42021-08-05 19:49:01 -0700203 TRY(compile_expression(env, &local, car(body), tail, Dst));
swissChilif1ba8c12021-07-02 18:45:38 -0700204 }
205
206 | cleanup;
207
208 if (local_out)
209 *local_out = local;
210
swissChili15f1cae2021-07-05 19:08:47 -0700211 if (args_out)
212 *args_out = ar;
swissChilif1ba8c12021-07-02 18:45:38 -0700213
swissChili6d02af42021-08-05 19:49:01 -0700214 *state = d;
215
216 OKAY();
swissChilif1ba8c12021-07-02 18:45:38 -0700217}
218
swissChili6d02af42021-08-05 19:49:01 -0700219struct error compile_tl(value_t val, struct environment *env, char *fname)
swissChilica107a02021-04-14 12:07:30 -0700220{
swissChili6d02af42021-08-05 19:49:01 -0700221 E_INIT();
222
223 NEARVAL(val);
224
swissChili53472e82021-05-08 16:06:32 -0700225 if (!listp(val))
swissChili6d02af42021-08-05 19:49:01 -0700226 {
227 THROW(EEXPECTED, "Top level form must be a list");
228 }
swissChilica107a02021-04-14 12:07:30 -0700229
swissChili53472e82021-05-08 16:06:32 -0700230 value_t form = car(val);
231 value_t args = cdr(val);
232
swissChili2999dd12021-07-02 14:19:53 -0700233 if (symstreq(form, "defun") || symstreq(form, "defmacro"))
swissChili8fc5e2f2021-04-22 13:45:10 -0700234 {
swissChili2999dd12021-07-02 14:19:53 -0700235 enum namespace namespace = NS_FUNCTION;
236
237 if (symstreq(form, "defmacro"))
swissChilia89ee442021-08-04 20:54:51 -0700238 namespace = NS_MACRO;
swissChili2999dd12021-07-02 14:19:53 -0700239
swissChili8fc5e2f2021-04-22 13:45:10 -0700240 struct local local;
swissChili15f1cae2021-07-05 19:08:47 -0700241 struct args *a;
swissChili74348422021-07-04 13:23:24 -0700242 char *name = (char *)(car(args) ^ SYMBOL_TAG);
swissChilif68671f2021-07-05 14:14:44 -0700243
swissChili6d02af42021-08-05 19:49:01 -0700244 dasm_State *d;
245 TRY(compile_function(cdr(args), namespace, env, &local,
246 NULL, &a, name, fname, &d));
swissChilia820dea2021-05-09 16:46:55 -0700247
swissChili7e1393c2021-07-07 12:59:12 -0700248 add_function(env, name, link_program(&d), a, namespace);
swissChili8fc5e2f2021-04-22 13:45:10 -0700249
swissChili53472e82021-05-08 16:06:32 -0700250 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700251 del_local(&local);
swissChili67bdf282021-06-06 18:46:08 -0700252 }
swissChilif68671f2021-07-05 14:14:44 -0700253 else if (symstreq(form, "progn"))
254 {
255 for (value_t val = args; !nilp(val); val = cdr(val))
256 {
swissChili6d02af42021-08-05 19:49:01 -0700257 TRY(compile_tl(car(val), env, fname));
swissChilif68671f2021-07-05 14:14:44 -0700258 }
259 }
swissChili484295d2021-07-09 21:25:55 -0700260 else if (symstreq(form, "load"))
261 {
262 if (length(args) != 1)
263 {
swissChili6d02af42021-08-05 19:49:01 -0700264 NEARVAL(args);
265 THROW(EARGS, "load expects exactly 1 argument, %d given",
266 length(args));
swissChili484295d2021-07-09 21:25:55 -0700267 }
268 load_relative(env, fname, car(args));
269 }
swissChili6d02af42021-08-05 19:49:01 -0700270
271 OKAY();
swissChili67bdf282021-06-06 18:46:08 -0700272}
273
swissChili36f2c692021-08-08 14:31:44 -0700274struct error walk_and_alloc(struct environment *env, struct local *local, value_t *bp)
swissChili67bdf282021-06-06 18:46:08 -0700275{
swissChili36f2c692021-08-08 14:31:44 -0700276 E_INIT();
277
278 value_t body = *bp;
279
swissChilib51552c2021-08-03 10:23:37 -0700280 // TODO: handle macros
swissChili67bdf282021-06-06 18:46:08 -0700281 if (!listp(body))
swissChili36f2c692021-08-08 14:31:44 -0700282 OKAY();
swissChili67bdf282021-06-06 18:46:08 -0700283
284 value_t args = cdr(body);
285
286 if (symstreq(car(body), "let1"))
287 {
288 int slot = local_alloc(local);
289
290 value_t expr = cdr(args);
swissChilif1ba8c12021-07-02 18:45:38 -0700291 for (; !nilp(expr); expr = cdr(expr))
292 {
swissChili36f2c692021-08-08 14:31:44 -0700293 walk_and_alloc(env, local, carref(expr));
swissChilif1ba8c12021-07-02 18:45:38 -0700294 }
swissChili67bdf282021-06-06 18:46:08 -0700295
296 local_free(local, slot);
297 }
swissChilif1ba8c12021-07-02 18:45:38 -0700298 else if (symstreq(car(body), "lambda"))
299 {
300 // We don't want to walk the lambda because it's another function. When
301 // the lambda is compiled it will be walked.
swissChili36f2c692021-08-08 14:31:44 -0700302 OKAY();
swissChilif1ba8c12021-07-02 18:45:38 -0700303 }
swissChili67bdf282021-06-06 18:46:08 -0700304 else
305 {
swissChili36f2c692021-08-08 14:31:44 -0700306 // Is this a macro?
307
308 struct function *mac = NULL;
309
310 if (symbolp(car(body)))
311 mac = find_function(env, (char *)(car(body) ^ SYMBOL_TAG));
312 else
313 walk_and_alloc(env, local, carref(body));
314
315 if (mac && mac->namespace == NS_MACRO)
swissChili67bdf282021-06-06 18:46:08 -0700316 {
swissChili36f2c692021-08-08 14:31:44 -0700317 unsigned char pool = push_pool(0);
318 value_t form = call_list(mac, args);
319 pop_pool(pool);
320
321 add_to_pool(form);
322 *bp = form;
323
324 walk_and_alloc(env, local, bp);
325 }
326 else
327 {
328 for (; !nilp(args); args = cdr(args))
329 {
330 walk_and_alloc(env, local, carref(args));
331 }
swissChili67bdf282021-06-06 18:46:08 -0700332 }
swissChili8fc5e2f2021-04-22 13:45:10 -0700333 }
swissChili36f2c692021-08-08 14:31:44 -0700334
335 OKAY();
swissChili8fc5e2f2021-04-22 13:45:10 -0700336}
337
swissChilif68671f2021-07-05 14:14:44 -0700338bool load(struct environment *env, char *path)
swissChili8fc5e2f2021-04-22 13:45:10 -0700339{
swissChilif68671f2021-07-05 14:14:44 -0700340 if (!file_exists(path))
341 return false;
342
343 add_load(env, path);
344
swissChilib8fd4712021-06-23 15:32:04 -0700345 unsigned char pool = make_pool();
346 unsigned char pop = push_pool(pool);
347
swissChilif68671f2021-07-05 14:14:44 -0700348 struct istream *is = new_fistream(path, false);
349 if (!is)
350 return false;
351
swissChili8fc5e2f2021-04-22 13:45:10 -0700352 value_t val;
swissChili53472e82021-05-08 16:06:32 -0700353
swissChili36f2c692021-08-08 14:31:44 -0700354 struct error read_error;
355
356 while (IS_OKAY((read_error = read1(is, &val))))
swissChili8fc5e2f2021-04-22 13:45:10 -0700357 {
swissChili6d02af42021-08-05 19:49:01 -0700358 if (!IS_OKAY(compile_tl(val, env, path)))
swissChili36f2c692021-08-08 14:31:44 -0700359 {
360 goto failure;
361 }
362 }
363
364 if (!read_error.safe_state)
365 {
366 goto failure;
swissChili8fc5e2f2021-04-22 13:45:10 -0700367 }
swissChilif3e7f182021-04-20 13:57:22 -0700368
swissChilif68671f2021-07-05 14:14:44 -0700369 del_fistream(is);
swissChilib8fd4712021-06-23 15:32:04 -0700370 pop_pool(pop);
371
swissChilif68671f2021-07-05 14:14:44 -0700372 return true;
swissChili36f2c692021-08-08 14:31:44 -0700373
374failure:
375 del_fistream(is);
376 pop_pool(pool);
377
378 return false;
swissChilif68671f2021-07-05 14:14:44 -0700379}
380
swissChili7e1393c2021-07-07 12:59:12 -0700381value_t load_relative(struct environment *env, char *to, value_t name)
382{
383 if (!stringp(name))
384 return nil;
385
386 char *new_path = (char *)(name ^ STRING_TAG);
387 char *relative_to = strdup(to);
388 char full_path[512];
389
390 snprintf(full_path, 512, "%s/%s", dirname(relative_to), new_path);
391
392 if (load(env, full_path))
393 return t;
394 else
395 return nil;
396}
397
swissChili6d02af42021-08-05 19:49:01 -0700398struct error compile_file(char *filename, struct environment **e)
swissChilif68671f2021-07-05 14:14:44 -0700399{
swissChili6d02af42021-08-05 19:49:01 -0700400 E_INIT();
401
swissChilif68671f2021-07-05 14:14:44 -0700402 value_t val;
swissChili7e1393c2021-07-07 12:59:12 -0700403 struct environment *env = malloc(sizeof(struct environment));
404 env->first = NULL;
405 env->first_loaded = NULL;
swissChilif68671f2021-07-05 14:14:44 -0700406
swissChili7e1393c2021-07-07 12:59:12 -0700407 add_load(env, filename);
swissChili6d02af42021-08-05 19:49:01 -0700408 TRY(load_std(env));
swissChilif68671f2021-07-05 14:14:44 -0700409
swissChili7e1393c2021-07-07 12:59:12 -0700410 bool ok_ = load(env, filename);
swissChilif68671f2021-07-05 14:14:44 -0700411
swissChili6d02af42021-08-05 19:49:01 -0700412 if (!ok_)
413 {
414 free(env);
415 THROWSAFE(ENOTFOUND);
416 }
swissChilif68671f2021-07-05 14:14:44 -0700417
swissChili6d02af42021-08-05 19:49:01 -0700418 *e = env;
419
420 OKAY();
swissChilica107a02021-04-14 12:07:30 -0700421}
swissChilib3ca4fb2021-04-20 10:33:00 -0700422
swissChili53472e82021-05-08 16:06:32 -0700423int nextpc(struct local *local, dasm_State **Dst)
swissChilib3ca4fb2021-04-20 10:33:00 -0700424{
swissChili53472e82021-05-08 16:06:32 -0700425 int n = local->nextpc++;
426 if (n > local->npc)
427 {
428 local->npc += 16;
429 dasm_growpc(Dst, local->npc);
430 }
431 return n;
432}
433
swissChili6d02af42021-08-05 19:49:01 -0700434struct error compile_backquote(struct environment *env, struct local *local,
435 value_t val, dasm_State **Dst)
swissChili6b47b6d2021-06-30 22:08:55 -0700436{
swissChili6d02af42021-08-05 19:49:01 -0700437 E_INIT();
438
swissChili6b47b6d2021-06-30 22:08:55 -0700439 if (!listp(val))
440 {
441 | mov eax, (val);
442 }
443 else
444 {
swissChili7e1393c2021-07-07 12:59:12 -0700445 value_t fsym = car(val), args = cdr(val);
swissChili9d151e62021-08-04 13:11:45 -0700446 int nargs = length(args),
447 n = length(val);
swissChili6b47b6d2021-06-30 22:08:55 -0700448
swissChili6d02af42021-08-05 19:49:01 -0700449 NEARVAL(val);
450
swissChili9d151e62021-08-04 13:11:45 -0700451 if (symstreq(fsym, "unquote"))
452 {
453 if (nargs != 1)
454 {
swissChili6d02af42021-08-05 19:49:01 -0700455 THROW(EARGS, "unquote (or ,) takes exactly 1 argument");
swissChili9d151e62021-08-04 13:11:45 -0700456 }
457
swissChili6d02af42021-08-05 19:49:01 -0700458 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili9d151e62021-08-04 13:11:45 -0700459 }
460 else
461 {
462 | push nil;
463
464 for (int i = n - 1; i >= 0; i--)
465 {
swissChilia7568dc2021-08-08 16:52:52 -0700466 value_t v = elt(val, i);
swissChili9d151e62021-08-04 13:11:45 -0700467
swissChilia7568dc2021-08-08 16:52:52 -0700468 if (listp(v) && symstreq(car(v), "unquote-splice"))
469 {
470 NEARVAL(v);
471
472 if (length(v) != 2)
473 {
474 THROW(EARGS, "unquote-splice (or ,@) takes exactly 1 argument");
475 }
476
477 value_t expr = car(cdr(v));
478
479 if (!listp(expr))
480 {
481 THROW(EINVALID, "unquote-splice (or ,@) argument must be a list");
482 }
483
484 TRY(compile_expression(env, local, expr, false, Dst));
485 | push eax;
486 | call_extern merge2;
487 | add esp, 8;
488 | push eax;
489 }
490 else
491 {
492 TRY(compile_backquote(env, local, v, Dst));
493 | push eax;
494 | call_extern cons;
495 | add esp, 8;
496
497 // Remove unnecessary pop
498 | push eax;
499 }
swissChili9d151e62021-08-04 13:11:45 -0700500 }
swissChilia89ee442021-08-04 20:54:51 -0700501 | pop eax;
swissChili9d151e62021-08-04 13:11:45 -0700502 }
swissChili6b47b6d2021-06-30 22:08:55 -0700503 }
swissChili6d02af42021-08-05 19:49:01 -0700504
505 OKAY();
swissChili6b47b6d2021-06-30 22:08:55 -0700506}
507
swissChili7e1393c2021-07-07 12:59:12 -0700508value_t eval(struct environment *env, value_t form)
509{
510 // Eval!
511 value_t function = cons(nil, cons(form, nil));
512
513 struct local local;
514 struct args *args;
515
swissChili6d02af42021-08-05 19:49:01 -0700516 dasm_State *d;
517 struct error err;
518
519 if (!IS_OKAY((err = compile_function(function, NS_ANONYMOUS, env, &local, NULL,
520 &args, NULL, "/", &d))))
521 {
522 ereport(err);
523 return nil;
524 }
swissChili7e1393c2021-07-07 12:59:12 -0700525
526 del_local(&local);
527
528 value_t (*f)() = link_program(&d);
529 return f();
530}
531
swissChili6d02af42021-08-05 19:49:01 -0700532struct error compile_variable(struct variable *v, dasm_State *Dst)
swissChiliddc97542021-07-04 11:47:42 -0700533{
swissChili6d02af42021-08-05 19:49:01 -0700534 E_INIT();
swissChiliddc97542021-07-04 11:47:42 -0700535 switch (v->type)
536 {
537 case V_ARGUMENT:
swissChili7e1393c2021-07-07 12:59:12 -0700538 | mov eax, dword[ebp + (value_size * (v->number + 2))];
swissChiliddc97542021-07-04 11:47:42 -0700539 break;
540 case V_BOUND:
swissChili7e1393c2021-07-07 12:59:12 -0700541 | mov eax, dword[ebp - ((v->number + 1) * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700542 break;
543 case V_FREE:
544 // edi is the closure context pointer
swissChili7e1393c2021-07-07 12:59:12 -0700545 | mov eax, dword[edi + (v->number * value_size)];
swissChiliddc97542021-07-04 11:47:42 -0700546 break;
547 default:
swissChili6d02af42021-08-05 19:49:01 -0700548 THROW(EUNIMPL, "Sorry, can only access V_ARGUMENT, V_BOUND, and V_FREE vars");
swissChiliddc97542021-07-04 11:47:42 -0700549 }
swissChili6d02af42021-08-05 19:49:01 -0700550 OKAY();
swissChiliddc97542021-07-04 11:47:42 -0700551}
552
swissChili6d02af42021-08-05 19:49:01 -0700553struct error compile_expression(struct environment *env, struct local *local,
554 value_t val, bool tail, dasm_State **Dst)
swissChili53472e82021-05-08 16:06:32 -0700555{
swissChili6d02af42021-08-05 19:49:01 -0700556 E_INIT();
557
558 NEARVAL(val);
559
swissChili7e1393c2021-07-07 12:59:12 -0700560 if (symstreq(val, "nil") || nilp(val))
swissChili53472e82021-05-08 16:06:32 -0700561 {
562 | mov eax, (nil);
563 }
swissChili923b5362021-05-09 20:31:43 -0700564 else if (symstreq(val, "t"))
565 {
566 | mov eax, (t);
567 }
568 else if (integerp(val) || stringp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700569 {
570 | mov eax, val;
571 }
swissChili53472e82021-05-08 16:06:32 -0700572 else if (listp(val))
swissChilib3ca4fb2021-04-20 10:33:00 -0700573 {
swissChili53472e82021-05-08 16:06:32 -0700574 value_t fsym = car(val);
575 value_t args = cdr(val);
576 int nargs = length(args);
swissChilib3ca4fb2021-04-20 10:33:00 -0700577
swissChili53472e82021-05-08 16:06:32 -0700578 if (!symbolp(fsym))
swissChilif3e7f182021-04-20 13:57:22 -0700579 {
swissChili6d02af42021-08-05 19:49:01 -0700580 THROW(EEXPECTED, "Function name must be a symbol");
swissChilif3e7f182021-04-20 13:57:22 -0700581 }
582
swissChili53472e82021-05-08 16:06:32 -0700583 if (symstreq(fsym, "if"))
swissChilib3ca4fb2021-04-20 10:33:00 -0700584 {
swissChili53472e82021-05-08 16:06:32 -0700585 if (nargs < 2 || nargs > 3)
swissChili6d02af42021-08-05 19:49:01 -0700586 {
587 THROW(EARGS, "Must give at least 2 arguments to if");
588 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700589
swissChili6d02af42021-08-05 19:49:01 -0700590 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili53472e82021-05-08 16:06:32 -0700591 int false_label = nextpc(local, Dst),
592 after_label = nextpc(local, Dst);
593
594 // result is in eax
595 | cmp eax, (nil);
swissChili484295d2021-07-09 21:25:55 -0700596 | je =>false_label;
swissChili53472e82021-05-08 16:06:32 -0700597
swissChili6d02af42021-08-05 19:49:01 -0700598 TRY(compile_expression(env, local, elt(args, 1), tail, Dst));
swissChili484295d2021-07-09 21:25:55 -0700599 | jmp =>after_label;
600 |=>false_label:;
swissChili53472e82021-05-08 16:06:32 -0700601 if (nargs == 3)
swissChili6d02af42021-08-05 19:49:01 -0700602 TRY(compile_expression(env, local, elt(args, 2), tail, Dst));
swissChili484295d2021-07-09 21:25:55 -0700603 |=>after_label:;
swissChili53472e82021-05-08 16:06:32 -0700604 }
swissChilia89ee442021-08-04 20:54:51 -0700605 else if (symstreq(fsym, "and") || symstreq(fsym, "or"))
606 {
607 bool or = symstreq(fsym, "or"); // false == and
608
609 // Boolean and and or, short circuit like &&/||
610 if (nargs < 1)
611 {
swissChili6d02af42021-08-05 19:49:01 -0700612 THROW(EARGS, "and & or require at least 1 argument.");
swissChilia89ee442021-08-04 20:54:51 -0700613 }
614
615 int after = nextpc(local, Dst);
616
617 for (; !nilp(args); args = cdr(args))
618 {
swissChili6d02af42021-08-05 19:49:01 -0700619 NEARVAL(args);
620
621 TRY(compile_expression(env, local, car(args), false, Dst));
swissChilia89ee442021-08-04 20:54:51 -0700622 if (!nilp(cdr(args)))
623 {
624 | cmp eax, nil;
625 if (or)
626 {
swissChilifbf525f2021-08-04 21:28:07 -0700627 | jne =>after;
swissChilia89ee442021-08-04 20:54:51 -0700628 }
629 else
630 {
swissChilifbf525f2021-08-04 21:28:07 -0700631 | je =>after;
swissChilia89ee442021-08-04 20:54:51 -0700632 }
633 }
634 }
635
636 |=>after:;
637 }
swissChilif68671f2021-07-05 14:14:44 -0700638 else if (symstreq(fsym, "progn"))
639 {
640 for (value_t val = args; !nilp(val); val = cdr(val))
641 {
swissChili6d02af42021-08-05 19:49:01 -0700642 NEARVAL(args);
643
swissChilib51552c2021-08-03 10:23:37 -0700644 bool t = tail && nilp(cdr(val));
swissChili6d02af42021-08-05 19:49:01 -0700645 TRY(compile_expression(env, local, car(val), t, Dst));
swissChilif68671f2021-07-05 14:14:44 -0700646 }
647 }
swissChili67bdf282021-06-06 18:46:08 -0700648 else if (symstreq(fsym, "let1"))
649 {
650 if (nargs < 2)
651 {
swissChili6d02af42021-08-05 19:49:01 -0700652 THROW(EARGS, "Must give at least 2 arguments to let1");
swissChili67bdf282021-06-06 18:46:08 -0700653 }
654 value_t binding = car(args);
655 value_t rest = cdr(args);
656
swissChili6d02af42021-08-05 19:49:01 -0700657 NEARVAL(binding);
swissChili67bdf282021-06-06 18:46:08 -0700658 if (length(binding) != 2)
659 {
swissChili6d02af42021-08-05 19:49:01 -0700660 THROW(EARGS, "Binding list in let1 must contain exactly two entries");
swissChili67bdf282021-06-06 18:46:08 -0700661 }
662
swissChili6d02af42021-08-05 19:49:01 -0700663 NEARVAL(rest);
664
swissChili67bdf282021-06-06 18:46:08 -0700665 value_t name = car(binding);
666 value_t value = car(cdr(binding));
667
swissChili6d02af42021-08-05 19:49:01 -0700668 TRY(compile_expression(env, local, value, false, Dst));
swissChili67bdf282021-06-06 18:46:08 -0700669
670 int i = local_alloc(local);
671
672 add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i);
673
swissChili7e1393c2021-07-07 12:59:12 -0700674 | mov dword[ebp - ((i + 1) * value_size)], eax;
swissChili67bdf282021-06-06 18:46:08 -0700675
676 for (; !nilp(rest); rest = cdr(rest))
677 {
swissChilib51552c2021-08-03 10:23:37 -0700678 bool t = tail && nilp(cdr(rest));
swissChili6d02af42021-08-05 19:49:01 -0700679 NEARVAL(rest);
680 TRY(compile_expression(env, local, car(rest), t, Dst));
swissChili67bdf282021-06-06 18:46:08 -0700681 }
682
683 local_free(local, i);
684 }
swissChilie9fec8b2021-06-22 13:59:33 -0700685 else if (symstreq(fsym, "gc"))
686 {
687 if (nargs)
688 {
swissChili6d02af42021-08-05 19:49:01 -0700689 THROW(EARGS, "gc takes no arguments");
swissChilie9fec8b2021-06-22 13:59:33 -0700690 }
691
692 | run_gc;
693 }
swissChili6b47b6d2021-06-30 22:08:55 -0700694 else if (symstreq(fsym, "quote"))
695 {
696 if (nargs != 1)
swissChili6d02af42021-08-05 19:49:01 -0700697 THROW(EARGS, "quote should take exactly 1 argument");
swissChili6b47b6d2021-06-30 22:08:55 -0700698
699 // Simple!
700 | mov eax, (car(args));
701 }
702 else if (symstreq(fsym, "backquote"))
703 {
704 if (nargs != 1)
swissChili6d02af42021-08-05 19:49:01 -0700705 THROW(EARGS, "backquote should take exactly 1 argument");
swissChili6b47b6d2021-06-30 22:08:55 -0700706
swissChili6d02af42021-08-05 19:49:01 -0700707 TRY(compile_backquote(env, local, car(args), Dst));
swissChili6b47b6d2021-06-30 22:08:55 -0700708 }
swissChili74348422021-07-04 13:23:24 -0700709 else if (symstreq(fsym, "function"))
710 {
711 if (nargs != 1)
712 {
swissChili6d02af42021-08-05 19:49:01 -0700713 THROW(EARGS, "function should take exactly 1 argument");
swissChili74348422021-07-04 13:23:24 -0700714 }
715
swissChili6d02af42021-08-05 19:49:01 -0700716 NEARVAL(args);
swissChili74348422021-07-04 13:23:24 -0700717 if (!symbolp(car(args)))
718 {
swissChili6d02af42021-08-05 19:49:01 -0700719 THROW(EINVALID, "argument to function should be a symbol resolvable at "
swissChili7e1393c2021-07-07 12:59:12 -0700720 "compile time");
swissChili74348422021-07-04 13:23:24 -0700721 }
722
swissChilia89ee442021-08-04 20:54:51 -0700723 char *name = (char *)(car(args) ^ SYMBOL_TAG);
swissChili74348422021-07-04 13:23:24 -0700724
swissChilia89ee442021-08-04 20:54:51 -0700725 if (!strcmp(name, local->current_function_name))
726 {
727 | push 0;
728 | push local->args;
729 | push <1;
730 | call_extern create_closure;
731 }
732 else
733 {
734 struct function *f = find_function(env, name);
735
736 if (!f)
737 {
swissChili6d02af42021-08-05 19:49:01 -0700738 THROW(EINVALID, "Function `%s' does not exist", (char *)(car(args) ^ SYMBOL_TAG));
swissChilia89ee442021-08-04 20:54:51 -0700739 }
740 value_t closure = create_closure(f->code_ptr, f->args, 0);
741 | mov eax, (closure);
742 }
swissChili74348422021-07-04 13:23:24 -0700743 }
swissChili6b47b6d2021-06-30 22:08:55 -0700744 else if (symstreq(fsym, "list"))
745 {
swissChili484295d2021-07-09 21:25:55 -0700746 | push (nil);
swissChili6b47b6d2021-06-30 22:08:55 -0700747
748 for (int i = nargs - 1; i >= 0; i--)
749 {
swissChili6d02af42021-08-05 19:49:01 -0700750 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili6b47b6d2021-06-30 22:08:55 -0700751
swissChili6b47b6d2021-06-30 22:08:55 -0700752 | push eax;
swissChili53e7cd12021-08-02 21:55:53 -0700753 | call_extern cons;
swissChili6b47b6d2021-06-30 22:08:55 -0700754 | add esp, (2 * value_size);
swissChili6b47b6d2021-06-30 22:08:55 -0700755 | push eax;
756 }
swissChili6d02af42021-08-05 19:49:01 -0700757 | pop eax;
swissChili6b47b6d2021-06-30 22:08:55 -0700758 }
swissChiliddc97542021-07-04 11:47:42 -0700759 else if (symstreq(fsym, "lambda"))
760 {
761 // Compile the function with this as the parent scope
762 struct local new_local;
763 int nargs_out;
swissChili6d02af42021-08-05 19:49:01 -0700764 dasm_State *d;
765 TRY(compile_function(
766 args, NS_ANONYMOUS, env, &new_local, local, &nargs_out,
767 "recurse", local->current_file_path, &d));
swissChiliddc97542021-07-04 11:47:42 -0700768
769 // Link the function
swissChilif68671f2021-07-05 14:14:44 -0700770 void *func_ptr = link_program(&d);
swissChiliddc97542021-07-04 11:47:42 -0700771
772 // Create a closure object with the correct number of captures at
773 // runtime
swissChili484295d2021-07-09 21:25:55 -0700774 | push (new_local.num_closure_slots);
775 | push (nargs_out);
776 | push (func_ptr);
swissChili53e7cd12021-08-02 21:55:53 -0700777 | call_extern create_closure;
swissChiliddc97542021-07-04 11:47:42 -0700778 | add esp, 12;
779
780 // Walk the generated local scope for V_FREE variables, since each
781 // of these exists in our scope (or higher), evaluate it and set it
782 // as a member of the lambda capture.
783
784 for (struct variable *var = new_local.first; var; var = var->prev)
785 {
786 if (var->type == V_FREE)
787 {
788 // Closure in eax
789 | push eax;
790 // Variable now in eax
swissChili6d02af42021-08-05 19:49:01 -0700791 TRY(compile_variable(find_variable(local, var->name), Dst));
swissChiliddc97542021-07-04 11:47:42 -0700792 | push eax;
793
swissChiliddc97542021-07-04 11:47:42 -0700794 // The capture offset
swissChili484295d2021-07-09 21:25:55 -0700795 | push (var->number);
swissChili53e7cd12021-08-02 21:55:53 -0700796 | call_extern set_closure_capture_variable;
swissChiliddc97542021-07-04 11:47:42 -0700797 // Skip the value and index
798 | add esp, 8;
799 // Pop the closure back in to eax
800 | pop eax;
801 }
802 }
803
804 // Closure is still in eax
805
806 dasm_free(&d);
swissChili708d4c42021-07-04 17:40:07 -0700807 del_local(&new_local);
swissChiliddc97542021-07-04 11:47:42 -0700808 }
swissChili7e1393c2021-07-07 12:59:12 -0700809 else if (symstreq(fsym, "eval"))
810 {
811 if (nargs != 1)
812 {
swissChili6d02af42021-08-05 19:49:01 -0700813 THROW(EARGS, "eval takes exactly 1 argument");
swissChili7e1393c2021-07-07 12:59:12 -0700814 }
815
swissChili6d02af42021-08-05 19:49:01 -0700816 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700817 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700818 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700819 | call_extern eval;
swissChili7e1393c2021-07-07 12:59:12 -0700820 }
821 else if (symstreq(fsym, "load"))
822 {
823 if (nargs != 1)
824 {
swissChili6d02af42021-08-05 19:49:01 -0700825 THROW(EARGS, "load takes exactly 1 argument, %d given", nargs);
swissChili7e1393c2021-07-07 12:59:12 -0700826 }
827
swissChili6d02af42021-08-05 19:49:01 -0700828 TRY(compile_expression(env, local, car(args), false, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700829 | push eax;
swissChili484295d2021-07-09 21:25:55 -0700830 | push (local->current_file_path);
831 | push (env);
swissChili53e7cd12021-08-02 21:55:53 -0700832 | call_extern load_relative;
swissChili7e1393c2021-07-07 12:59:12 -0700833 }
swissChili53472e82021-05-08 16:06:32 -0700834 else
835 {
swissChili74348422021-07-04 13:23:24 -0700836 char *name = (char *)(fsym ^ SYMBOL_TAG);
837 struct function *func = find_function(env, name);
swissChili7e1393c2021-07-07 12:59:12 -0700838
swissChili74348422021-07-04 13:23:24 -0700839 bool is_recursive = false;
swissChili15f1cae2021-07-05 19:08:47 -0700840 struct args *nargs_needed = NULL;
swissChili53472e82021-05-08 16:06:32 -0700841
swissChili53e7cd12021-08-02 21:55:53 -0700842 // The number of arguments actually passed on the stack,
843 // i.e. all varargs are 1.
swissChilib51552c2021-08-03 10:23:37 -0700844 int real_nargs;
swissChili53e7cd12021-08-02 21:55:53 -0700845
swissChili7e1393c2021-07-07 12:59:12 -0700846 if (local->current_function_name &&
847 symstreq(fsym, local->current_function_name))
swissChilif1ba8c12021-07-02 18:45:38 -0700848 {
swissChili74348422021-07-04 13:23:24 -0700849 is_recursive = true;
swissChili15f1cae2021-07-05 19:08:47 -0700850 nargs_needed = local->args;
swissChili74348422021-07-04 13:23:24 -0700851 }
852 else
853 {
854 if (func == NULL)
855 {
swissChili6d02af42021-08-05 19:49:01 -0700856 THROW(EINVALID, "Function %s undefined", name);
swissChili74348422021-07-04 13:23:24 -0700857 }
858
swissChili15f1cae2021-07-05 19:08:47 -0700859 nargs_needed = func->args;
swissChili74348422021-07-04 13:23:24 -0700860 }
861
swissChili15f1cae2021-07-05 19:08:47 -0700862 if (!are_args_acceptable(nargs_needed, nargs))
swissChili74348422021-07-04 13:23:24 -0700863 {
swissChili6d02af42021-08-05 19:49:01 -0700864 THROW(EARGS,
865 "wrong number of args in function call: %s, "
866 "want %d args but given %d\n",
867 name, nargs_needed->num_required, nargs);
swissChilif1ba8c12021-07-02 18:45:38 -0700868 }
swissChili53472e82021-05-08 16:06:32 -0700869
swissChili53e7cd12021-08-02 21:55:53 -0700870 int total_taken = nargs_needed->num_optional +
871 nargs_needed->num_required;
872
swissChilib51552c2021-08-03 10:23:37 -0700873 real_nargs = total_taken + (nargs_needed->variadic ? 1 : 0);
swissChili53e7cd12021-08-02 21:55:53 -0700874
swissChili74348422021-07-04 13:23:24 -0700875 if (is_recursive || func->namespace == NS_FUNCTION)
swissChili53472e82021-05-08 16:06:32 -0700876 {
swissChili15f1cae2021-07-05 19:08:47 -0700877 int nargs = length(args);
878
swissChili484295d2021-07-09 21:25:55 -0700879 int line = cons_line(val);
880 char *file = cons_file(val);
881
882 if (nargs_needed->variadic)
swissChili15f1cae2021-07-05 19:08:47 -0700883 {
swissChili484295d2021-07-09 21:25:55 -0700884 | push (nil);
885 }
886
887 if (nargs > total_taken && nargs_needed->variadic)
888 {
889 // We are passing varargs, which means we need to make a list
890
891 for (int i = nargs - 1; i >= total_taken; i--)
892 {
swissChili6d02af42021-08-05 19:49:01 -0700893 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili484295d2021-07-09 21:25:55 -0700894 | push eax;
swissChili53e7cd12021-08-02 21:55:53 -0700895 | call_extern cons;
swissChili484295d2021-07-09 21:25:55 -0700896 | add esp, 8;
897 | push eax;
898 }
swissChili15f1cae2021-07-05 19:08:47 -0700899 }
900
swissChili7e1393c2021-07-07 12:59:12 -0700901 for (int i = nargs_needed->num_optional - 1;
902 i >= nargs - nargs_needed->num_required; i--)
swissChili15f1cae2021-07-05 19:08:47 -0700903 {
904 // Push the default optional values
swissChili484295d2021-07-09 21:25:55 -0700905 | push (nargs_needed->optional_arguments[i].value);
swissChili15f1cae2021-07-05 19:08:47 -0700906 }
907
swissChili484295d2021-07-09 21:25:55 -0700908 int min = MIN(nargs, total_taken);
909
910 for (int i = min - 1; i >= 0; i--)
swissChili2999dd12021-07-02 14:19:53 -0700911 {
swissChili6d02af42021-08-05 19:49:01 -0700912 TRY(compile_expression(env, local, elt(args, i), false, Dst));
swissChili2999dd12021-07-02 14:19:53 -0700913 | push eax;
914 }
swissChili15f1cae2021-07-05 19:08:47 -0700915
swissChili74348422021-07-04 13:23:24 -0700916 if (is_recursive)
917 {
swissChilib51552c2021-08-03 10:23:37 -0700918 if (tail)
919 {
920 // Move all the arguments pushed to the stack
921 // back up to the argument bit of the stack.
922
923 for (int i = 0; i < real_nargs; i++)
924 {
925 | pop eax;
926 | mov dword[ebp + (value_size * (i + 2))], eax;
927 }
928
929 // Jmp back to start
930 | mov esp, ebp;
931 | pop ebp;
932 | jmp <1;
933 }
934 else
935 {
936 | call <1;
937 }
swissChili74348422021-07-04 13:23:24 -0700938 }
939 else
940 {
swissChili484295d2021-07-09 21:25:55 -0700941 // | mov ebx, (func->code_addr);
942 | call_extern func->code_addr;
swissChili74348422021-07-04 13:23:24 -0700943 }
swissChili53e7cd12021-08-02 21:55:53 -0700944 | add esp, (real_nargs * value_size);
swissChili2999dd12021-07-02 14:19:53 -0700945 // result in eax
946 }
947 else if (func->namespace == NS_MACRO)
948 {
swissChili7e1393c2021-07-07 12:59:12 -0700949 // Make sure that the stuff allocated by the macro isn't in a
950 // pool
swissChilif68671f2021-07-05 14:14:44 -0700951 unsigned char pool = push_pool(0);
952
swissChili2999dd12021-07-02 14:19:53 -0700953 value_t expanded_to = call_list(func, args);
954
swissChilif68671f2021-07-05 14:14:44 -0700955 pop_pool(pool);
956
swissChili6d02af42021-08-05 19:49:01 -0700957 TRY(compile_expression(env, local, expanded_to, false, Dst));
swissChili2999dd12021-07-02 14:19:53 -0700958 }
swissChili53472e82021-05-08 16:06:32 -0700959 }
swissChilib3ca4fb2021-04-20 10:33:00 -0700960 }
swissChili923b5362021-05-09 20:31:43 -0700961 else if (symbolp(val))
962 {
swissChili7e1393c2021-07-07 12:59:12 -0700963 if (symstreq(val, "+current-file+"))
swissChilie9fec8b2021-06-22 13:59:33 -0700964 {
swissChili7e1393c2021-07-07 12:59:12 -0700965 value_t file_name_val = strval(local->current_file_path);
966
967 | mov eax, (file_name_val);
swissChilie9fec8b2021-06-22 13:59:33 -0700968 }
swissChili7e1393c2021-07-07 12:59:12 -0700969 else
970 {
971 struct variable *v =
972 find_variable(local, (char *)(val ^ SYMBOL_TAG));
swissChili923b5362021-05-09 20:31:43 -0700973
swissChili7e1393c2021-07-07 12:59:12 -0700974 if (!v)
975 {
swissChili6d02af42021-08-05 19:49:01 -0700976 THROW(EINVALID, "Variable `%s' unbound", (char *)(val ^ SYMBOL_TAG));
swissChili7e1393c2021-07-07 12:59:12 -0700977 }
978
swissChili6d02af42021-08-05 19:49:01 -0700979 TRY(compile_variable(v, Dst));
swissChili7e1393c2021-07-07 12:59:12 -0700980 }
swissChili923b5362021-05-09 20:31:43 -0700981 }
swissChilia89ee442021-08-04 20:54:51 -0700982 else if (closurep(val))
983 {
984 | mov eax, val;
985 }
986 else
987 {
988 printval(val, 1);
swissChili6d02af42021-08-05 19:49:01 -0700989 THROW(EUNIMPL, "Don't know how to compile this, sorry.");
swissChilia89ee442021-08-04 20:54:51 -0700990 }
swissChili6d02af42021-08-05 19:49:01 -0700991
992 OKAY();
swissChilib3ca4fb2021-04-20 10:33:00 -0700993}
swissChilif3e7f182021-04-20 13:57:22 -0700994
swissChili923b5362021-05-09 20:31:43 -0700995struct variable *add_variable(struct local *local, enum var_type type,
996 char *name, int number)
997{
998 struct variable *var = malloc(sizeof(struct variable));
999 var->prev = local->first;
1000 var->type = type;
1001 var->name = name;
1002 var->number = number;
1003
1004 local->first = var;
1005
1006 return var;
1007}
1008
1009void destroy_local(struct local *local)
1010{
1011 for (struct variable *v = local->first; v;)
1012 {
1013 struct variable *t = v;
1014 v = v->prev;
1015 free(t);
1016 }
1017}
1018
1019struct variable *find_variable(struct local *local, char *name)
1020{
1021 struct variable *v = local->first;
1022
1023 for (; v && strcmp(v->name, name) != 0; v = v->prev)
swissChili7e1393c2021-07-07 12:59:12 -07001024 {
1025 }
swissChili923b5362021-05-09 20:31:43 -07001026
swissChiliddc97542021-07-04 11:47:42 -07001027 if (!v)
1028 {
1029 if (local->parent)
1030 {
1031 v = find_variable(local->parent, name);
1032
1033 if (v)
1034 {
swissChili15f1cae2021-07-05 19:08:47 -07001035 // We found this in a parent scope, add it as a V_FREE variable
1036 // to skip the search.
swissChili7e1393c2021-07-07 12:59:12 -07001037 v = add_variable(local, V_FREE, name,
1038 local->num_closure_slots++);
swissChiliddc97542021-07-04 11:47:42 -07001039 }
1040 }
1041 }
swissChili923b5362021-05-09 20:31:43 -07001042 return v;
1043}
swissChili2999dd12021-07-02 14:19:53 -07001044
swissChiliddc97542021-07-04 11:47:42 -07001045extern value_t _call_list(void *addr, value_t list, value_t *edi);
swissChili2999dd12021-07-02 14:19:53 -07001046
swissChili7e1393c2021-07-07 12:59:12 -07001047value_t call_list_args(void *code_ptr, struct args *args, value_t list,
1048 void *data)
swissChili2999dd12021-07-02 14:19:53 -07001049{
swissChili15f1cae2021-07-05 19:08:47 -07001050 list = deep_copy(list);
swissChili484295d2021-07-09 21:25:55 -07001051
swissChili15f1cae2021-07-05 19:08:47 -07001052 int nargs = length(list);
1053
swissChili484295d2021-07-09 21:25:55 -07001054 value_t *val = &list;
swissChili15f1cae2021-07-05 19:08:47 -07001055
1056 for (value_t i = list; !nilp(i); i = cdr(i))
1057 {
1058 val = cdrref(i);
1059 }
1060
1061 int total_required = args->num_required + args->num_optional;
1062
1063 if (nargs > total_required)
1064 {
1065 // Take the remainder of the list and put it as the last item in the
1066 // list.
1067 value_t trailing = cxdr(list, total_required);
1068 value_t last_item = cons(trailing, nil);
1069
1070 *cxdrref(&list, total_required) = last_item;
1071 }
1072 else if (nargs < total_required)
1073 {
1074 for (int i = nargs - args->num_required; i < args->num_optional; i++)
1075 {
1076 // Append the i-th defualt argument
1077 value_t appended = cons(args->optional_arguments[i].value, nil);
1078 *val = appended;
1079 val = cdrref(appended);
1080 }
1081 }
1082
1083 // We want to call this if we pass the correct # of arguments or less, just
1084 // not if we have already passed varargs. Appends a nil argument.
1085 if (nargs <= total_required)
1086 {
1087 // Enough real arguments but no variadic arguments. Pass a nil list.
1088 *val = cons(nil, nil);
1089 }
1090
1091 return _call_list(code_ptr, list, data);
1092}
1093
1094value_t call_list(struct function *fun, value_t list)
1095{
1096 return call_list_args(fun->code_ptr, fun->args, list, NULL);
swissChiliddc97542021-07-04 11:47:42 -07001097}
1098
1099value_t call_list_closure(struct closure *c, value_t list)
1100{
swissChili15f1cae2021-07-05 19:08:47 -07001101 return call_list_args(c->function, c->args, list, c->data);
1102}
1103
1104struct args *new_args()
1105{
1106 struct args *a = malloc(sizeof(struct args));
1107 a->num_optional = 0;
1108 a->num_required = 0;
1109 a->variadic = false;
1110
1111 return a;
1112}
1113
swissChili7e1393c2021-07-07 12:59:12 -07001114struct args *add_optional_arg(struct args *args, value_t name, value_t value)
swissChili15f1cae2021-07-05 19:08:47 -07001115{
1116 int i = args->num_optional++;
swissChili7e1393c2021-07-07 12:59:12 -07001117 args =
1118 realloc(args, sizeof(struct args) + sizeof(struct optional_argument) *
1119 args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -07001120
swissChili7e1393c2021-07-07 12:59:12 -07001121 args->optional_arguments[i] = (struct optional_argument){
1122 .value = value,
1123 .name = name,
swissChili15f1cae2021-07-05 19:08:47 -07001124 };
1125
1126 return args;
1127}
1128
1129bool are_args_acceptable(struct args *args, int number)
1130{
1131 if (args->variadic)
1132 {
1133 return number >= args->num_required;
1134 }
1135 else
1136 {
1137 return number >= args->num_required &&
swissChili7e1393c2021-07-07 12:59:12 -07001138 number <= args->num_required + args->num_optional;
swissChili15f1cae2021-07-05 19:08:47 -07001139 }
1140}
1141
swissChili6d02af42021-08-05 19:49:01 -07001142struct error list_to_args(struct environment *env, value_t list,
1143 struct local *local, struct args **a)
swissChili15f1cae2021-07-05 19:08:47 -07001144{
swissChili6d02af42021-08-05 19:49:01 -07001145 E_INIT();
1146
swissChili15f1cae2021-07-05 19:08:47 -07001147 struct args *args = new_args();
1148
1149 bool in_optional = false;
1150
1151 for (value_t i = list; !nilp(i); i = cdr(i))
1152 {
1153 value_t val = car(i);
swissChili6d02af42021-08-05 19:49:01 -07001154 NEARVAL(i);
1155
swissChili15f1cae2021-07-05 19:08:47 -07001156 if (symbolp(val))
1157 {
1158 if (!args->variadic && symstreq(val, "&"))
1159 {
1160 i = cdr(i);
1161 value_t name = car(i);
1162
1163 if (!symbolp(name))
1164 {
swissChili6d02af42021-08-05 19:49:01 -07001165 THROW(EEXPECTED, "You must provide a symbol after & in an argument list "
1166 "to bind the\n"
1167 "variadic arguments to.");
swissChili15f1cae2021-07-05 19:08:47 -07001168 }
1169
1170 args->variadic = true;
1171
1172 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
swissChili7e1393c2021-07-07 12:59:12 -07001173 args->num_optional + args->num_required);
swissChili15f1cae2021-07-05 19:08:47 -07001174
1175 continue;
1176 }
1177
1178 if (!in_optional)
1179 {
swissChili7e1393c2021-07-07 12:59:12 -07001180 add_variable(local, V_ARGUMENT, (char *)(val ^ SYMBOL_TAG),
1181 args->num_required++);
swissChili15f1cae2021-07-05 19:08:47 -07001182 }
1183 else
1184 {
1185 char *name = (char *)(val ^ SYMBOL_TAG);
1186 if (name[0] == '&')
1187 {
swissChili6d02af42021-08-05 19:49:01 -07001188 THROW(EINVALID, "Non-optional argument following optional arguments "
1189 "starts with a &\n"
1190 "did you mean to declare a variadic argument? If so "
1191 "leave a space\n"
1192 "between the & and name.");
swissChili15f1cae2021-07-05 19:08:47 -07001193 }
1194 else
1195 {
swissChili6d02af42021-08-05 19:49:01 -07001196 THROW(EINVALID, "Cannot define a non-optional argument after an "
1197 "optional one.");
swissChili15f1cae2021-07-05 19:08:47 -07001198 }
1199 }
1200 }
1201 else if (listp(val))
1202 {
swissChili6d02af42021-08-05 19:49:01 -07001203 NEARVAL(val);
1204
swissChili15f1cae2021-07-05 19:08:47 -07001205 in_optional = true;
1206 int len = length(val);
1207
1208 if (len != 2)
1209 {
swissChili6d02af42021-08-05 19:49:01 -07001210 THROW(EINVALID, "A list defining an optional value must be structured like "
1211 "(name expr)\n"
1212 "with exactly two arguments.");
swissChili15f1cae2021-07-05 19:08:47 -07001213 }
1214
1215 value_t name = car(val);
1216 value_t expr = car(cdr(val));
1217
1218 value_t function = cons(nil, cons(expr, nil));
1219
swissChili6d02af42021-08-05 19:49:01 -07001220 dasm_State *d;
1221 TRY(compile_function(function, NS_ANONYMOUS, env, NULL, NULL, NULL,
1222 NULL, local->current_file_path, &d));
swissChili15f1cae2021-07-05 19:08:47 -07001223
1224 // TODO: GC stack top!
1225 value_t (*compiled)() = link_program(&d);
1226
1227 value_t value = compiled();
1228 args = add_optional_arg(args, name, value);
1229
swissChili7e1393c2021-07-07 12:59:12 -07001230 add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG),
1231 args->num_required + args->num_optional - 1);
swissChili15f1cae2021-07-05 19:08:47 -07001232 }
1233 }
1234
swissChili6d02af42021-08-05 19:49:01 -07001235 *a = args;
1236 OKAY();
swissChili15f1cae2021-07-05 19:08:47 -07001237}
1238
1239void display_args(struct args *args)
1240{
1241 printf("Args object taking %d require arguments and %d optionals:\n",
swissChili7e1393c2021-07-07 12:59:12 -07001242 args->num_required, args->num_optional);
swissChili15f1cae2021-07-05 19:08:47 -07001243
1244 for (int i = 0; i < args->num_optional; i++)
1245 {
swissChili7e1393c2021-07-07 12:59:12 -07001246 printf(" %d\t%s\n", i,
1247 (char *)(args->optional_arguments[i].name ^ SYMBOL_TAG));
swissChili15f1cae2021-07-05 19:08:47 -07001248 printval(args->optional_arguments[i].value, 2);
1249 }
swissChili2999dd12021-07-02 14:19:53 -07001250}