swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 1 | /* -*- mode:c -*- */ |
| 2 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 3 | #include "compiler.h" |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 4 | #include "gc.h" |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 5 | #include "lib/std.h" |
| 6 | #include "lisp.h" |
| 7 | #include "plat/plat.h" |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 8 | |
| 9 | #include <dasm_proto.h> |
| 10 | #include <dasm_x86.h> |
| 11 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 12 | #include <libgen.h> |
| 13 | #include <stdio.h> |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 17 | #define value_size sizeof(value_t) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 18 | |
| 19 | |.arch x86; |
| 20 | |
| 21 | |.macro setup, nvars; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 22 | |1:; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 23 | | push ebp; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 24 | | mov ebp, esp; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 25 | | sub esp, (value_size * nvars); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 26 | |.endmacro; |
| 27 | |
| 28 | |.macro cleanup; |
| 29 | | mov esp, ebp; |
| 30 | | pop ebp; |
| 31 | | ret; |
| 32 | |.endmacro; |
| 33 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 34 | |.macro call_extern, address; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 35 | | call &address; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 36 | |.endmacro; |
| 37 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 38 | dasm_State *d; |
| 39 | unsigned int npc = 8; |
| 40 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 41 | |.macro run_gc; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 42 | | mov eax, esp; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 43 | | push ebp; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 44 | | push eax; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 45 | | mov eax, _do_gc; |
| 46 | | call eax; |
| 47 | |.endmacro; |
swissChili | 6d6525e | 2021-06-15 21:20:53 -0700 | [diff] [blame] | 48 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 49 | struct function *find_function(struct environment *env, char *name) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 50 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 51 | struct function *f; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 52 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 53 | for (f = env->first; f && strcmp(f->name, name); f = f->prev) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 54 | { |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return f; |
| 58 | } |
| 59 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 60 | unsigned 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; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 77 | local->stack_slots = |
| 78 | realloc(local->stack_slots, local->num_stack_slots * sizeof(bool)); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 79 | // 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 | |
| 86 | void local_free(struct local *local, unsigned int slot) |
| 87 | { |
| 88 | local->stack_slots[slot] = false; |
| 89 | } |
| 90 | |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 91 | void 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 | |
| 102 | void 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 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 110 | |
| 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 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 117 | |
| 118 | free(env); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void 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; |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 132 | } |
| 133 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 134 | struct 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) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 141 | { |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 142 | UNUSED(namespace); |
| 143 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 144 | E_INIT(); |
| 145 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 146 | dasm_State *d; |
| 147 | dasm_State **Dst = &d; |
| 148 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 149 | |.section code, imports; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 150 | 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; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 169 | local.num_closure_slots = 0; |
| 170 | local.parent = local_parent; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 171 | local.current_function_name = name; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 172 | local.current_file_path = path; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 173 | |
| 174 | dasm_growpc(&d, local.npc); |
| 175 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 176 | value_t arglist = car(args); |
| 177 | value_t body = cdr(args); |
| 178 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 179 | // This will add the arguments to local too. |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 180 | struct args *ar; |
| 181 | TRY(list_to_args(env, arglist, &local, &ar)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 182 | local.args = ar; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 183 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 184 | if (!ar) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 185 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 186 | NEARVAL(arglist); |
| 187 | THROW(EMALFORMED, "Malformed argument list"); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | for (value_t body_ = body; !nilp(body_); body_ = cdr(body_)) |
| 191 | { |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 192 | TRY(walk_and_alloc(env, &local, carref(body_))); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 193 | } |
| 194 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 195 | | setup (local.num_stack_entries); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 196 | |
| 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 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 202 | bool tail = nilp(cdr(body)); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 203 | TRY(compile_expression(env, &local, car(body), tail, Dst)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | | cleanup; |
| 207 | |
| 208 | if (local_out) |
| 209 | *local_out = local; |
| 210 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 211 | if (args_out) |
| 212 | *args_out = ar; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 213 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 214 | *state = d; |
| 215 | |
| 216 | OKAY(); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 217 | } |
| 218 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 219 | struct error compile_tl(value_t val, struct environment *env, char *fname) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 220 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 221 | E_INIT(); |
| 222 | |
| 223 | NEARVAL(val); |
| 224 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 225 | if (!listp(val)) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 226 | { |
| 227 | THROW(EEXPECTED, "Top level form must be a list"); |
| 228 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 229 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 230 | value_t form = car(val); |
| 231 | value_t args = cdr(val); |
| 232 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 233 | if (symstreq(form, "defun") || symstreq(form, "defmacro")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 234 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 235 | enum namespace namespace = NS_FUNCTION; |
| 236 | |
| 237 | if (symstreq(form, "defmacro")) |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 238 | namespace = NS_MACRO; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 239 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 240 | struct local local; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 241 | struct args *a; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 242 | char *name = (char *)(car(args) ^ SYMBOL_TAG); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 243 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 244 | dasm_State *d; |
| 245 | TRY(compile_function(cdr(args), namespace, env, &local, |
| 246 | NULL, &a, name, fname, &d)); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 247 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 248 | add_function(env, name, link_program(&d), a, namespace); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 249 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 250 | dasm_free(&d); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 251 | del_local(&local); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 252 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 253 | else if (symstreq(form, "progn")) |
| 254 | { |
| 255 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 256 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 257 | TRY(compile_tl(car(val), env, fname)); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 260 | else if (symstreq(form, "load")) |
| 261 | { |
| 262 | if (length(args) != 1) |
| 263 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 264 | NEARVAL(args); |
| 265 | THROW(EARGS, "load expects exactly 1 argument, %d given", |
| 266 | length(args)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 267 | } |
| 268 | load_relative(env, fname, car(args)); |
| 269 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 270 | |
| 271 | OKAY(); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 272 | } |
| 273 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 274 | struct error walk_and_alloc(struct environment *env, struct local *local, value_t *bp) |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 275 | { |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 276 | E_INIT(); |
| 277 | |
| 278 | value_t body = *bp; |
| 279 | |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 280 | // TODO: handle macros |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 281 | if (!listp(body)) |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 282 | OKAY(); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 283 | |
| 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); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 291 | for (; !nilp(expr); expr = cdr(expr)) |
| 292 | { |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 293 | walk_and_alloc(env, local, carref(expr)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 294 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 295 | |
| 296 | local_free(local, slot); |
| 297 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 298 | 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. |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 302 | OKAY(); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 303 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 304 | else |
| 305 | { |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 306 | // 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) |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 316 | { |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 317 | 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 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 332 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 333 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 334 | |
| 335 | OKAY(); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 336 | } |
| 337 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 338 | bool load(struct environment *env, char *path) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 339 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 340 | if (!file_exists(path)) |
| 341 | return false; |
| 342 | |
| 343 | add_load(env, path); |
| 344 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 345 | unsigned char pool = make_pool(); |
| 346 | unsigned char pop = push_pool(pool); |
| 347 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 348 | struct istream *is = new_fistream(path, false); |
| 349 | if (!is) |
| 350 | return false; |
| 351 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 352 | value_t val; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 353 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 354 | struct error read_error; |
| 355 | |
| 356 | while (IS_OKAY((read_error = read1(is, &val)))) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 357 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 358 | if (!IS_OKAY(compile_tl(val, env, path))) |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 359 | { |
| 360 | goto failure; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (!read_error.safe_state) |
| 365 | { |
| 366 | goto failure; |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 367 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 368 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 369 | del_fistream(is); |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 370 | pop_pool(pop); |
| 371 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 372 | return true; |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 373 | |
| 374 | failure: |
| 375 | del_fistream(is); |
| 376 | pop_pool(pool); |
| 377 | |
| 378 | return false; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 379 | } |
| 380 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 381 | value_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 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 398 | struct error compile_file(char *filename, struct environment **e) |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 399 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 400 | E_INIT(); |
| 401 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 402 | value_t val; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 403 | struct environment *env = malloc(sizeof(struct environment)); |
| 404 | env->first = NULL; |
| 405 | env->first_loaded = NULL; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 406 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 407 | add_load(env, filename); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 408 | TRY(load_std(env)); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 409 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 410 | bool ok_ = load(env, filename); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 411 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 412 | if (!ok_) |
| 413 | { |
| 414 | free(env); |
| 415 | THROWSAFE(ENOTFOUND); |
| 416 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 417 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 418 | *e = env; |
| 419 | |
| 420 | OKAY(); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 421 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 422 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 423 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 424 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 425 | 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 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 434 | struct error compile_backquote(struct environment *env, struct local *local, |
| 435 | value_t val, dasm_State **Dst) |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 436 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 437 | E_INIT(); |
| 438 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 439 | if (!listp(val)) |
| 440 | { |
| 441 | | mov eax, (val); |
| 442 | } |
| 443 | else |
| 444 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 445 | value_t fsym = car(val), args = cdr(val); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 446 | int nargs = length(args), |
| 447 | n = length(val); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 448 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 449 | NEARVAL(val); |
| 450 | |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 451 | if (symstreq(fsym, "unquote")) |
| 452 | { |
| 453 | if (nargs != 1) |
| 454 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 455 | THROW(EARGS, "unquote (or ,) takes exactly 1 argument"); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 456 | } |
| 457 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 458 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 459 | } |
| 460 | else |
| 461 | { |
| 462 | | push nil; |
| 463 | |
| 464 | for (int i = n - 1; i >= 0; i--) |
| 465 | { |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 466 | value_t v = elt(val, i); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 467 | |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 468 | 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 | } |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 500 | } |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 501 | | pop eax; |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 502 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 503 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 504 | |
| 505 | OKAY(); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 506 | } |
| 507 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 508 | value_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 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 516 | 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 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 525 | |
| 526 | del_local(&local); |
| 527 | |
| 528 | value_t (*f)() = link_program(&d); |
| 529 | return f(); |
| 530 | } |
| 531 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 532 | struct error compile_variable(struct variable *v, dasm_State *Dst) |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 533 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 534 | E_INIT(); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 535 | switch (v->type) |
| 536 | { |
| 537 | case V_ARGUMENT: |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 538 | | mov eax, dword[ebp + (value_size * (v->number + 2))]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 539 | break; |
| 540 | case V_BOUND: |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 541 | | mov eax, dword[ebp - ((v->number + 1) * value_size)]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 542 | break; |
| 543 | case V_FREE: |
| 544 | // edi is the closure context pointer |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 545 | | mov eax, dword[edi + (v->number * value_size)]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 546 | break; |
| 547 | default: |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 548 | THROW(EUNIMPL, "Sorry, can only access V_ARGUMENT, V_BOUND, and V_FREE vars"); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 549 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 550 | OKAY(); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 551 | } |
| 552 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 553 | struct error compile_expression(struct environment *env, struct local *local, |
| 554 | value_t val, bool tail, dasm_State **Dst) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 555 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 556 | E_INIT(); |
| 557 | |
| 558 | NEARVAL(val); |
| 559 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 560 | if (symstreq(val, "nil") || nilp(val)) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 561 | { |
| 562 | | mov eax, (nil); |
| 563 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 564 | else if (symstreq(val, "t")) |
| 565 | { |
| 566 | | mov eax, (t); |
| 567 | } |
| 568 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 569 | { |
| 570 | | mov eax, val; |
| 571 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 572 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 573 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 574 | value_t fsym = car(val); |
| 575 | value_t args = cdr(val); |
| 576 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 577 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 578 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 579 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 580 | THROW(EEXPECTED, "Function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 581 | } |
| 582 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 583 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 584 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 585 | if (nargs < 2 || nargs > 3) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 586 | { |
| 587 | THROW(EARGS, "Must give at least 2 arguments to if"); |
| 588 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 589 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 590 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 591 | int false_label = nextpc(local, Dst), |
| 592 | after_label = nextpc(local, Dst); |
| 593 | |
| 594 | // result is in eax |
| 595 | | cmp eax, (nil); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 596 | | je =>false_label; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 597 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 598 | TRY(compile_expression(env, local, elt(args, 1), tail, Dst)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 599 | | jmp =>after_label; |
| 600 | |=>false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 601 | if (nargs == 3) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 602 | TRY(compile_expression(env, local, elt(args, 2), tail, Dst)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 603 | |=>after_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 604 | } |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 605 | 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 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 612 | THROW(EARGS, "and & or require at least 1 argument."); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | int after = nextpc(local, Dst); |
| 616 | |
| 617 | for (; !nilp(args); args = cdr(args)) |
| 618 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 619 | NEARVAL(args); |
| 620 | |
| 621 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 622 | if (!nilp(cdr(args))) |
| 623 | { |
| 624 | | cmp eax, nil; |
| 625 | if (or) |
| 626 | { |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 627 | | jne =>after; |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 628 | } |
| 629 | else |
| 630 | { |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 631 | | je =>after; |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | |=>after:; |
| 637 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 638 | else if (symstreq(fsym, "progn")) |
| 639 | { |
| 640 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 641 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 642 | NEARVAL(args); |
| 643 | |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 644 | bool t = tail && nilp(cdr(val)); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 645 | TRY(compile_expression(env, local, car(val), t, Dst)); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 648 | else if (symstreq(fsym, "let1")) |
| 649 | { |
| 650 | if (nargs < 2) |
| 651 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 652 | THROW(EARGS, "Must give at least 2 arguments to let1"); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 653 | } |
| 654 | value_t binding = car(args); |
| 655 | value_t rest = cdr(args); |
| 656 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 657 | NEARVAL(binding); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 658 | if (length(binding) != 2) |
| 659 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 660 | THROW(EARGS, "Binding list in let1 must contain exactly two entries"); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 661 | } |
| 662 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 663 | NEARVAL(rest); |
| 664 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 665 | value_t name = car(binding); |
| 666 | value_t value = car(cdr(binding)); |
| 667 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 668 | TRY(compile_expression(env, local, value, false, Dst)); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 669 | |
| 670 | int i = local_alloc(local); |
| 671 | |
| 672 | add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i); |
| 673 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 674 | | mov dword[ebp - ((i + 1) * value_size)], eax; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 675 | |
| 676 | for (; !nilp(rest); rest = cdr(rest)) |
| 677 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 678 | bool t = tail && nilp(cdr(rest)); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 679 | NEARVAL(rest); |
| 680 | TRY(compile_expression(env, local, car(rest), t, Dst)); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | local_free(local, i); |
| 684 | } |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 685 | else if (symstreq(fsym, "gc")) |
| 686 | { |
| 687 | if (nargs) |
| 688 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 689 | THROW(EARGS, "gc takes no arguments"); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | | run_gc; |
| 693 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 694 | else if (symstreq(fsym, "quote")) |
| 695 | { |
| 696 | if (nargs != 1) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 697 | THROW(EARGS, "quote should take exactly 1 argument"); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 698 | |
| 699 | // Simple! |
| 700 | | mov eax, (car(args)); |
| 701 | } |
| 702 | else if (symstreq(fsym, "backquote")) |
| 703 | { |
| 704 | if (nargs != 1) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 705 | THROW(EARGS, "backquote should take exactly 1 argument"); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 706 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 707 | TRY(compile_backquote(env, local, car(args), Dst)); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 708 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 709 | else if (symstreq(fsym, "function")) |
| 710 | { |
| 711 | if (nargs != 1) |
| 712 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 713 | THROW(EARGS, "function should take exactly 1 argument"); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 714 | } |
| 715 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 716 | NEARVAL(args); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 717 | if (!symbolp(car(args))) |
| 718 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 719 | THROW(EINVALID, "argument to function should be a symbol resolvable at " |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 720 | "compile time"); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 721 | } |
| 722 | |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 723 | char *name = (char *)(car(args) ^ SYMBOL_TAG); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 724 | |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 725 | 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 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 738 | THROW(EINVALID, "Function `%s' does not exist", (char *)(car(args) ^ SYMBOL_TAG)); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 739 | } |
| 740 | value_t closure = create_closure(f->code_ptr, f->args, 0); |
| 741 | | mov eax, (closure); |
| 742 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 743 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 744 | else if (symstreq(fsym, "list")) |
| 745 | { |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 746 | | push (nil); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 747 | |
| 748 | for (int i = nargs - 1; i >= 0; i--) |
| 749 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 750 | TRY(compile_expression(env, local, elt(args, i), false, Dst)); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 751 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 752 | | push eax; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 753 | | call_extern cons; |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 754 | | add esp, (2 * value_size); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 755 | | push eax; |
| 756 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 757 | | pop eax; |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 758 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 759 | 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; |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 764 | dasm_State *d; |
| 765 | TRY(compile_function( |
| 766 | args, NS_ANONYMOUS, env, &new_local, local, &nargs_out, |
| 767 | "recurse", local->current_file_path, &d)); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 768 | |
| 769 | // Link the function |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 770 | void *func_ptr = link_program(&d); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 771 | |
| 772 | // Create a closure object with the correct number of captures at |
| 773 | // runtime |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 774 | | push (new_local.num_closure_slots); |
| 775 | | push (nargs_out); |
| 776 | | push (func_ptr); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 777 | | call_extern create_closure; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 778 | | 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 |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 791 | TRY(compile_variable(find_variable(local, var->name), Dst)); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 792 | | push eax; |
| 793 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 794 | // The capture offset |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 795 | | push (var->number); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 796 | | call_extern set_closure_capture_variable; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 797 | // 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); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 807 | del_local(&new_local); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 808 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 809 | else if (symstreq(fsym, "eval")) |
| 810 | { |
| 811 | if (nargs != 1) |
| 812 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 813 | THROW(EARGS, "eval takes exactly 1 argument"); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 814 | } |
| 815 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 816 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 817 | | push eax; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 818 | | push (env); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 819 | | call_extern eval; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 820 | } |
| 821 | else if (symstreq(fsym, "load")) |
| 822 | { |
| 823 | if (nargs != 1) |
| 824 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 825 | THROW(EARGS, "load takes exactly 1 argument, %d given", nargs); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 826 | } |
| 827 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 828 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 829 | | push eax; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 830 | | push (local->current_file_path); |
| 831 | | push (env); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 832 | | call_extern load_relative; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 833 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 834 | else |
| 835 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 836 | char *name = (char *)(fsym ^ SYMBOL_TAG); |
| 837 | struct function *func = find_function(env, name); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 838 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 839 | bool is_recursive = false; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 840 | struct args *nargs_needed = NULL; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 841 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 842 | // The number of arguments actually passed on the stack, |
| 843 | // i.e. all varargs are 1. |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 844 | int real_nargs; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 845 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 846 | if (local->current_function_name && |
| 847 | symstreq(fsym, local->current_function_name)) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 848 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 849 | is_recursive = true; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 850 | nargs_needed = local->args; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 851 | } |
| 852 | else |
| 853 | { |
| 854 | if (func == NULL) |
| 855 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 856 | THROW(EINVALID, "Function %s undefined", name); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 857 | } |
| 858 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 859 | nargs_needed = func->args; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 860 | } |
| 861 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 862 | if (!are_args_acceptable(nargs_needed, nargs)) |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 863 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 864 | 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); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 868 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 869 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 870 | int total_taken = nargs_needed->num_optional + |
| 871 | nargs_needed->num_required; |
| 872 | |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 873 | real_nargs = total_taken + (nargs_needed->variadic ? 1 : 0); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 874 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 875 | if (is_recursive || func->namespace == NS_FUNCTION) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 876 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 877 | int nargs = length(args); |
| 878 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 879 | int line = cons_line(val); |
| 880 | char *file = cons_file(val); |
| 881 | |
| 882 | if (nargs_needed->variadic) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 883 | { |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 884 | | 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 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 893 | TRY(compile_expression(env, local, elt(args, i), false, Dst)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 894 | | push eax; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 895 | | call_extern cons; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 896 | | add esp, 8; |
| 897 | | push eax; |
| 898 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 899 | } |
| 900 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 901 | for (int i = nargs_needed->num_optional - 1; |
| 902 | i >= nargs - nargs_needed->num_required; i--) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 903 | { |
| 904 | // Push the default optional values |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 905 | | push (nargs_needed->optional_arguments[i].value); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 906 | } |
| 907 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 908 | int min = MIN(nargs, total_taken); |
| 909 | |
| 910 | for (int i = min - 1; i >= 0; i--) |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 911 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 912 | TRY(compile_expression(env, local, elt(args, i), false, Dst)); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 913 | | push eax; |
| 914 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 915 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 916 | if (is_recursive) |
| 917 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 918 | 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 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 938 | } |
| 939 | else |
| 940 | { |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 941 | // | mov ebx, (func->code_addr); |
| 942 | | call_extern func->code_addr; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 943 | } |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 944 | | add esp, (real_nargs * value_size); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 945 | // result in eax |
| 946 | } |
| 947 | else if (func->namespace == NS_MACRO) |
| 948 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 949 | // Make sure that the stuff allocated by the macro isn't in a |
| 950 | // pool |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 951 | unsigned char pool = push_pool(0); |
| 952 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 953 | value_t expanded_to = call_list(func, args); |
| 954 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 955 | pop_pool(pool); |
| 956 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 957 | TRY(compile_expression(env, local, expanded_to, false, Dst)); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 958 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 959 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 960 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 961 | else if (symbolp(val)) |
| 962 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 963 | if (symstreq(val, "+current-file+")) |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 964 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 965 | value_t file_name_val = strval(local->current_file_path); |
| 966 | |
| 967 | | mov eax, (file_name_val); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 968 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 969 | else |
| 970 | { |
| 971 | struct variable *v = |
| 972 | find_variable(local, (char *)(val ^ SYMBOL_TAG)); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 973 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 974 | if (!v) |
| 975 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 976 | THROW(EINVALID, "Variable `%s' unbound", (char *)(val ^ SYMBOL_TAG)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 977 | } |
| 978 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 979 | TRY(compile_variable(v, Dst)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 980 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 981 | } |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 982 | else if (closurep(val)) |
| 983 | { |
| 984 | | mov eax, val; |
| 985 | } |
| 986 | else |
| 987 | { |
| 988 | printval(val, 1); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 989 | THROW(EUNIMPL, "Don't know how to compile this, sorry."); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 990 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 991 | |
| 992 | OKAY(); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 993 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 994 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 995 | struct 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 | |
| 1009 | void 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 | |
| 1019 | struct 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) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1024 | { |
| 1025 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 1026 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 1027 | if (!v) |
| 1028 | { |
| 1029 | if (local->parent) |
| 1030 | { |
| 1031 | v = find_variable(local->parent, name); |
| 1032 | |
| 1033 | if (v) |
| 1034 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1035 | // We found this in a parent scope, add it as a V_FREE variable |
| 1036 | // to skip the search. |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1037 | v = add_variable(local, V_FREE, name, |
| 1038 | local->num_closure_slots++); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 1042 | return v; |
| 1043 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1044 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 1045 | extern value_t _call_list(void *addr, value_t list, value_t *edi); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1046 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1047 | value_t call_list_args(void *code_ptr, struct args *args, value_t list, |
| 1048 | void *data) |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1049 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1050 | list = deep_copy(list); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 1051 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1052 | int nargs = length(list); |
| 1053 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 1054 | value_t *val = &list; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1055 | |
| 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 | |
| 1094 | value_t call_list(struct function *fun, value_t list) |
| 1095 | { |
| 1096 | return call_list_args(fun->code_ptr, fun->args, list, NULL); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | value_t call_list_closure(struct closure *c, value_t list) |
| 1100 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1101 | return call_list_args(c->function, c->args, list, c->data); |
| 1102 | } |
| 1103 | |
| 1104 | struct 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 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1114 | struct args *add_optional_arg(struct args *args, value_t name, value_t value) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1115 | { |
| 1116 | int i = args->num_optional++; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1117 | args = |
| 1118 | realloc(args, sizeof(struct args) + sizeof(struct optional_argument) * |
| 1119 | args->num_optional); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1120 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1121 | args->optional_arguments[i] = (struct optional_argument){ |
| 1122 | .value = value, |
| 1123 | .name = name, |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1124 | }; |
| 1125 | |
| 1126 | return args; |
| 1127 | } |
| 1128 | |
| 1129 | bool 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 && |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1138 | number <= args->num_required + args->num_optional; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1142 | struct error list_to_args(struct environment *env, value_t list, |
| 1143 | struct local *local, struct args **a) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1144 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1145 | E_INIT(); |
| 1146 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1147 | 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); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1154 | NEARVAL(i); |
| 1155 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1156 | 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 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1165 | THROW(EEXPECTED, "You must provide a symbol after & in an argument list " |
| 1166 | "to bind the\n" |
| 1167 | "variadic arguments to."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | args->variadic = true; |
| 1171 | |
| 1172 | add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG), |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1173 | args->num_optional + args->num_required); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1174 | |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
| 1178 | if (!in_optional) |
| 1179 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1180 | add_variable(local, V_ARGUMENT, (char *)(val ^ SYMBOL_TAG), |
| 1181 | args->num_required++); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1182 | } |
| 1183 | else |
| 1184 | { |
| 1185 | char *name = (char *)(val ^ SYMBOL_TAG); |
| 1186 | if (name[0] == '&') |
| 1187 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1188 | 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."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1193 | } |
| 1194 | else |
| 1195 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1196 | THROW(EINVALID, "Cannot define a non-optional argument after an " |
| 1197 | "optional one."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | else if (listp(val)) |
| 1202 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1203 | NEARVAL(val); |
| 1204 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1205 | in_optional = true; |
| 1206 | int len = length(val); |
| 1207 | |
| 1208 | if (len != 2) |
| 1209 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1210 | THROW(EINVALID, "A list defining an optional value must be structured like " |
| 1211 | "(name expr)\n" |
| 1212 | "with exactly two arguments."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1213 | } |
| 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 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1220 | dasm_State *d; |
| 1221 | TRY(compile_function(function, NS_ANONYMOUS, env, NULL, NULL, NULL, |
| 1222 | NULL, local->current_file_path, &d)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1223 | |
| 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 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1230 | add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG), |
| 1231 | args->num_required + args->num_optional - 1); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1235 | *a = args; |
| 1236 | OKAY(); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | void display_args(struct args *args) |
| 1240 | { |
| 1241 | printf("Args object taking %d require arguments and %d optionals:\n", |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1242 | args->num_required, args->num_optional); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1243 | |
| 1244 | for (int i = 0; i < args->num_optional; i++) |
| 1245 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1246 | printf(" %d\t%s\n", i, |
| 1247 | (char *)(args->optional_arguments[i].name ^ SYMBOL_TAG)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1248 | printval(args->optional_arguments[i].value, 2); |
| 1249 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1250 | } |