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 | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 142 | E_INIT(); |
| 143 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 144 | dasm_State *d; |
| 145 | dasm_State **Dst = &d; |
| 146 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 147 | |.section code, imports; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 148 | dasm_init(&d, DASM_MAXSECTION); |
| 149 | |
| 150 | |.globals lbl_; |
| 151 | void *labels[lbl__MAX]; |
| 152 | dasm_setupglobal(&d, labels, lbl__MAX); |
| 153 | |
| 154 | |.actionlist lisp_actions; |
| 155 | dasm_setup(&d, lisp_actions); |
| 156 | |
| 157 | struct local local; |
| 158 | local.parent = NULL; |
| 159 | local.first = NULL; |
| 160 | local.num_vars = 0; |
| 161 | local.npc = 8; |
| 162 | local.nextpc = 0; |
| 163 | local.stack_slots = malloc(sizeof(bool) * 4); |
| 164 | memset(local.stack_slots, 0, sizeof(bool) * 4); |
| 165 | local.num_stack_slots = 4; |
| 166 | local.num_stack_entries = 0; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 167 | local.num_closure_slots = 0; |
| 168 | local.parent = local_parent; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 169 | local.current_function_name = name; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 170 | local.current_file_path = path; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 171 | |
| 172 | dasm_growpc(&d, local.npc); |
| 173 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 174 | value_t arglist = car(args); |
| 175 | value_t body = cdr(args); |
| 176 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 177 | // This will add the arguments to local too. |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 178 | struct args *ar; |
| 179 | TRY(list_to_args(env, arglist, &local, &ar)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 180 | local.args = ar; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 181 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 182 | if (!ar) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 183 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 184 | NEARVAL(arglist); |
| 185 | THROW(EMALFORMED, "Malformed argument list"); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | for (value_t body_ = body; !nilp(body_); body_ = cdr(body_)) |
| 189 | { |
| 190 | walk_and_alloc(&local, car(body_)); |
| 191 | } |
| 192 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 193 | | setup (local.num_stack_entries); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 194 | |
| 195 | memset(local.stack_slots, 0, local.num_stack_slots * sizeof(bool)); |
| 196 | local.num_stack_entries = 0; |
| 197 | |
| 198 | for (; !nilp(body); body = cdr(body)) |
| 199 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 200 | bool tail = nilp(cdr(body)); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 201 | TRY(compile_expression(env, &local, car(body), tail, Dst)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | | cleanup; |
| 205 | |
| 206 | if (local_out) |
| 207 | *local_out = local; |
| 208 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 209 | if (args_out) |
| 210 | *args_out = ar; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 211 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 212 | *state = d; |
| 213 | |
| 214 | OKAY(); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 215 | } |
| 216 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 217 | struct error compile_tl(value_t val, struct environment *env, char *fname) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 218 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 219 | E_INIT(); |
| 220 | |
| 221 | NEARVAL(val); |
| 222 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 223 | if (!listp(val)) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 224 | { |
| 225 | THROW(EEXPECTED, "Top level form must be a list"); |
| 226 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 227 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 228 | value_t form = car(val); |
| 229 | value_t args = cdr(val); |
| 230 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 231 | if (symstreq(form, "defun") || symstreq(form, "defmacro")) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 232 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 233 | enum namespace namespace = NS_FUNCTION; |
| 234 | |
| 235 | if (symstreq(form, "defmacro")) |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 236 | namespace = NS_MACRO; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 237 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 238 | struct local local; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 239 | struct args *a; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 240 | char *name = (char *)(car(args) ^ SYMBOL_TAG); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 241 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 242 | dasm_State *d; |
| 243 | TRY(compile_function(cdr(args), namespace, env, &local, |
| 244 | NULL, &a, name, fname, &d)); |
swissChili | a820dea | 2021-05-09 16:46:55 -0700 | [diff] [blame] | 245 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 246 | add_function(env, name, link_program(&d), a, namespace); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 247 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 248 | dasm_free(&d); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 249 | del_local(&local); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 250 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 251 | else if (symstreq(form, "progn")) |
| 252 | { |
| 253 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 254 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 255 | TRY(compile_tl(car(val), env, fname)); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 258 | else if (symstreq(form, "load")) |
| 259 | { |
| 260 | if (length(args) != 1) |
| 261 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 262 | NEARVAL(args); |
| 263 | THROW(EARGS, "load expects exactly 1 argument, %d given", |
| 264 | length(args)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 265 | } |
| 266 | load_relative(env, fname, car(args)); |
| 267 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 268 | |
| 269 | OKAY(); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void walk_and_alloc(struct local *local, value_t body) |
| 273 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 274 | // TODO: handle macros |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 275 | if (!listp(body)) |
| 276 | return; |
| 277 | |
| 278 | value_t args = cdr(body); |
| 279 | |
| 280 | if (symstreq(car(body), "let1")) |
| 281 | { |
| 282 | int slot = local_alloc(local); |
| 283 | |
| 284 | value_t expr = cdr(args); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 285 | for (; !nilp(expr); expr = cdr(expr)) |
| 286 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 287 | walk_and_alloc(local, car(expr)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 288 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 289 | |
| 290 | local_free(local, slot); |
| 291 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 292 | else if (symstreq(car(body), "lambda")) |
| 293 | { |
| 294 | // We don't want to walk the lambda because it's another function. When |
| 295 | // the lambda is compiled it will be walked. |
| 296 | return; |
| 297 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 298 | else |
| 299 | { |
| 300 | for (; !nilp(args); args = cdr(args)) |
| 301 | { |
| 302 | walk_and_alloc(local, car(args)); |
| 303 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 307 | bool load(struct environment *env, char *path) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 308 | { |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 309 | if (!file_exists(path)) |
| 310 | return false; |
| 311 | |
| 312 | add_load(env, path); |
| 313 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 314 | unsigned char pool = make_pool(); |
| 315 | unsigned char pop = push_pool(pool); |
| 316 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 317 | struct istream *is = new_fistream(path, false); |
| 318 | if (!is) |
| 319 | return false; |
| 320 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 321 | value_t val; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 322 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 323 | while (IS_OKAY(read1(is, &val))) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 324 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 325 | if (!IS_OKAY(compile_tl(val, env, path))) |
| 326 | break; |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 327 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 328 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 329 | del_fistream(is); |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 330 | pop_pool(pop); |
| 331 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 332 | return true; |
| 333 | } |
| 334 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 335 | value_t load_relative(struct environment *env, char *to, value_t name) |
| 336 | { |
| 337 | if (!stringp(name)) |
| 338 | return nil; |
| 339 | |
| 340 | char *new_path = (char *)(name ^ STRING_TAG); |
| 341 | char *relative_to = strdup(to); |
| 342 | char full_path[512]; |
| 343 | |
| 344 | snprintf(full_path, 512, "%s/%s", dirname(relative_to), new_path); |
| 345 | |
| 346 | if (load(env, full_path)) |
| 347 | return t; |
| 348 | else |
| 349 | return nil; |
| 350 | } |
| 351 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 352 | struct error compile_file(char *filename, struct environment **e) |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 353 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 354 | E_INIT(); |
| 355 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 356 | value_t val; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 357 | struct environment *env = malloc(sizeof(struct environment)); |
| 358 | env->first = NULL; |
| 359 | env->first_loaded = NULL; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 360 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 361 | add_load(env, filename); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 362 | TRY(load_std(env)); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 363 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 364 | bool ok_ = load(env, filename); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 365 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 366 | if (!ok_) |
| 367 | { |
| 368 | free(env); |
| 369 | THROWSAFE(ENOTFOUND); |
| 370 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 371 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 372 | *e = env; |
| 373 | |
| 374 | OKAY(); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 375 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 376 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 377 | int nextpc(struct local *local, dasm_State **Dst) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 378 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 379 | int n = local->nextpc++; |
| 380 | if (n > local->npc) |
| 381 | { |
| 382 | local->npc += 16; |
| 383 | dasm_growpc(Dst, local->npc); |
| 384 | } |
| 385 | return n; |
| 386 | } |
| 387 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 388 | struct error compile_backquote(struct environment *env, struct local *local, |
| 389 | value_t val, dasm_State **Dst) |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 390 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 391 | E_INIT(); |
| 392 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 393 | if (!listp(val)) |
| 394 | { |
| 395 | | mov eax, (val); |
| 396 | } |
| 397 | else |
| 398 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 399 | value_t fsym = car(val), args = cdr(val); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 400 | int nargs = length(args), |
| 401 | n = length(val); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 402 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 403 | NEARVAL(val); |
| 404 | |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 405 | if (symstreq(fsym, "unquote")) |
| 406 | { |
| 407 | if (nargs != 1) |
| 408 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 409 | THROW(EARGS, "unquote (or ,) takes exactly 1 argument"); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 410 | } |
| 411 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 412 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 413 | } |
| 414 | else |
| 415 | { |
| 416 | | push nil; |
| 417 | |
| 418 | for (int i = n - 1; i >= 0; i--) |
| 419 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 420 | TRY(compile_backquote(env, local, elt(val, i), Dst)); |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 421 | | push eax; |
| 422 | | call_extern cons; |
| 423 | | add esp, 8; |
| 424 | |
| 425 | // Remove unnecessary pop |
| 426 | | push eax; |
| 427 | } |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 428 | | pop eax; |
swissChili | 9d151e6 | 2021-08-04 13:11:45 -0700 | [diff] [blame] | 429 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 430 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 431 | |
| 432 | OKAY(); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 433 | } |
| 434 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 435 | value_t eval(struct environment *env, value_t form) |
| 436 | { |
| 437 | // Eval! |
| 438 | value_t function = cons(nil, cons(form, nil)); |
| 439 | |
| 440 | struct local local; |
| 441 | struct args *args; |
| 442 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 443 | dasm_State *d; |
| 444 | struct error err; |
| 445 | |
| 446 | if (!IS_OKAY((err = compile_function(function, NS_ANONYMOUS, env, &local, NULL, |
| 447 | &args, NULL, "/", &d)))) |
| 448 | { |
| 449 | ereport(err); |
| 450 | return nil; |
| 451 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 452 | |
| 453 | del_local(&local); |
| 454 | |
| 455 | value_t (*f)() = link_program(&d); |
| 456 | return f(); |
| 457 | } |
| 458 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 459 | struct error compile_variable(struct variable *v, dasm_State *Dst) |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 460 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 461 | E_INIT(); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 462 | switch (v->type) |
| 463 | { |
| 464 | case V_ARGUMENT: |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 465 | | mov eax, dword[ebp + (value_size * (v->number + 2))]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 466 | break; |
| 467 | case V_BOUND: |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 468 | | mov eax, dword[ebp - ((v->number + 1) * value_size)]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 469 | break; |
| 470 | case V_FREE: |
| 471 | // edi is the closure context pointer |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 472 | | mov eax, dword[edi + (v->number * value_size)]; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 473 | break; |
| 474 | default: |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 475 | 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] | 476 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 477 | OKAY(); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 478 | } |
| 479 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 480 | struct error compile_expression(struct environment *env, struct local *local, |
| 481 | value_t val, bool tail, dasm_State **Dst) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 482 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 483 | E_INIT(); |
| 484 | |
| 485 | NEARVAL(val); |
| 486 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 487 | if (symstreq(val, "nil") || nilp(val)) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 488 | { |
| 489 | | mov eax, (nil); |
| 490 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 491 | else if (symstreq(val, "t")) |
| 492 | { |
| 493 | | mov eax, (t); |
| 494 | } |
| 495 | else if (integerp(val) || stringp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 496 | { |
| 497 | | mov eax, val; |
| 498 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 499 | else if (listp(val)) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 500 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 501 | value_t fsym = car(val); |
| 502 | value_t args = cdr(val); |
| 503 | int nargs = length(args); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 504 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 505 | if (!symbolp(fsym)) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 506 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 507 | THROW(EEXPECTED, "Function name must be a symbol"); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 508 | } |
| 509 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 510 | if (symstreq(fsym, "if")) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 511 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 512 | if (nargs < 2 || nargs > 3) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 513 | { |
| 514 | THROW(EARGS, "Must give at least 2 arguments to if"); |
| 515 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 516 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 517 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 518 | int false_label = nextpc(local, Dst), |
| 519 | after_label = nextpc(local, Dst); |
| 520 | |
| 521 | // result is in eax |
| 522 | | cmp eax, (nil); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 523 | | je =>false_label; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 524 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 525 | TRY(compile_expression(env, local, elt(args, 1), tail, Dst)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 526 | | jmp =>after_label; |
| 527 | |=>false_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 528 | if (nargs == 3) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 529 | TRY(compile_expression(env, local, elt(args, 2), tail, Dst)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 530 | |=>after_label:; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 531 | } |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 532 | else if (symstreq(fsym, "and") || symstreq(fsym, "or")) |
| 533 | { |
| 534 | bool or = symstreq(fsym, "or"); // false == and |
| 535 | |
| 536 | // Boolean and and or, short circuit like &&/|| |
| 537 | if (nargs < 1) |
| 538 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 539 | THROW(EARGS, "and & or require at least 1 argument."); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | int after = nextpc(local, Dst); |
| 543 | |
| 544 | for (; !nilp(args); args = cdr(args)) |
| 545 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 546 | NEARVAL(args); |
| 547 | |
| 548 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 549 | if (!nilp(cdr(args))) |
| 550 | { |
| 551 | | cmp eax, nil; |
| 552 | if (or) |
| 553 | { |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 554 | | jne =>after; |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 555 | } |
| 556 | else |
| 557 | { |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 558 | | je =>after; |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | |=>after:; |
| 564 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 565 | else if (symstreq(fsym, "progn")) |
| 566 | { |
| 567 | for (value_t val = args; !nilp(val); val = cdr(val)) |
| 568 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 569 | NEARVAL(args); |
| 570 | |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 571 | bool t = tail && nilp(cdr(val)); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 572 | TRY(compile_expression(env, local, car(val), t, Dst)); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 575 | else if (symstreq(fsym, "let1")) |
| 576 | { |
| 577 | if (nargs < 2) |
| 578 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 579 | THROW(EARGS, "Must give at least 2 arguments to let1"); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 580 | } |
| 581 | value_t binding = car(args); |
| 582 | value_t rest = cdr(args); |
| 583 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 584 | NEARVAL(binding); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 585 | if (length(binding) != 2) |
| 586 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 587 | THROW(EARGS, "Binding list in let1 must contain exactly two entries"); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 588 | } |
| 589 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 590 | NEARVAL(rest); |
| 591 | |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 592 | value_t name = car(binding); |
| 593 | value_t value = car(cdr(binding)); |
| 594 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 595 | TRY(compile_expression(env, local, value, false, Dst)); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 596 | |
| 597 | int i = local_alloc(local); |
| 598 | |
| 599 | add_variable(local, V_BOUND, (char *)(name ^ SYMBOL_TAG), i); |
| 600 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 601 | | mov dword[ebp - ((i + 1) * value_size)], eax; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 602 | |
| 603 | for (; !nilp(rest); rest = cdr(rest)) |
| 604 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 605 | bool t = tail && nilp(cdr(rest)); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 606 | NEARVAL(rest); |
| 607 | TRY(compile_expression(env, local, car(rest), t, Dst)); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | local_free(local, i); |
| 611 | } |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 612 | else if (symstreq(fsym, "gc")) |
| 613 | { |
| 614 | if (nargs) |
| 615 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 616 | THROW(EARGS, "gc takes no arguments"); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | | run_gc; |
| 620 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 621 | else if (symstreq(fsym, "quote")) |
| 622 | { |
| 623 | if (nargs != 1) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 624 | THROW(EARGS, "quote should take exactly 1 argument"); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 625 | |
| 626 | // Simple! |
| 627 | | mov eax, (car(args)); |
| 628 | } |
| 629 | else if (symstreq(fsym, "backquote")) |
| 630 | { |
| 631 | if (nargs != 1) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 632 | THROW(EARGS, "backquote should take exactly 1 argument"); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 633 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 634 | TRY(compile_backquote(env, local, car(args), Dst)); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 635 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 636 | else if (symstreq(fsym, "function")) |
| 637 | { |
| 638 | if (nargs != 1) |
| 639 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 640 | THROW(EARGS, "function should take exactly 1 argument"); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 641 | } |
| 642 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 643 | NEARVAL(args); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 644 | if (!symbolp(car(args))) |
| 645 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 646 | THROW(EINVALID, "argument to function should be a symbol resolvable at " |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 647 | "compile time"); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 648 | } |
| 649 | |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 650 | char *name = (char *)(car(args) ^ SYMBOL_TAG); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 651 | |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 652 | if (!strcmp(name, local->current_function_name)) |
| 653 | { |
| 654 | | push 0; |
| 655 | | push local->args; |
| 656 | | push <1; |
| 657 | | call_extern create_closure; |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | struct function *f = find_function(env, name); |
| 662 | |
| 663 | if (!f) |
| 664 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 665 | THROW(EINVALID, "Function `%s' does not exist", (char *)(car(args) ^ SYMBOL_TAG)); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 666 | } |
| 667 | value_t closure = create_closure(f->code_ptr, f->args, 0); |
| 668 | | mov eax, (closure); |
| 669 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 670 | } |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 671 | else if (symstreq(fsym, "list")) |
| 672 | { |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 673 | | push (nil); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 674 | |
| 675 | for (int i = nargs - 1; i >= 0; i--) |
| 676 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 677 | TRY(compile_expression(env, local, elt(args, i), false, Dst)); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 678 | |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 679 | | push eax; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 680 | | call_extern cons; |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 681 | | add esp, (2 * value_size); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 682 | | push eax; |
| 683 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 684 | | pop eax; |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 685 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 686 | else if (symstreq(fsym, "lambda")) |
| 687 | { |
| 688 | // Compile the function with this as the parent scope |
| 689 | struct local new_local; |
| 690 | int nargs_out; |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 691 | dasm_State *d; |
| 692 | TRY(compile_function( |
| 693 | args, NS_ANONYMOUS, env, &new_local, local, &nargs_out, |
| 694 | "recurse", local->current_file_path, &d)); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 695 | |
| 696 | // Link the function |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 697 | void *func_ptr = link_program(&d); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 698 | |
| 699 | // Create a closure object with the correct number of captures at |
| 700 | // runtime |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 701 | | push (new_local.num_closure_slots); |
| 702 | | push (nargs_out); |
| 703 | | push (func_ptr); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 704 | | call_extern create_closure; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 705 | | add esp, 12; |
| 706 | |
| 707 | // Walk the generated local scope for V_FREE variables, since each |
| 708 | // of these exists in our scope (or higher), evaluate it and set it |
| 709 | // as a member of the lambda capture. |
| 710 | |
| 711 | for (struct variable *var = new_local.first; var; var = var->prev) |
| 712 | { |
| 713 | if (var->type == V_FREE) |
| 714 | { |
| 715 | // Closure in eax |
| 716 | | push eax; |
| 717 | // Variable now in eax |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 718 | TRY(compile_variable(find_variable(local, var->name), Dst)); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 719 | | push eax; |
| 720 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 721 | // The capture offset |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 722 | | push (var->number); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 723 | | call_extern set_closure_capture_variable; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 724 | // Skip the value and index |
| 725 | | add esp, 8; |
| 726 | // Pop the closure back in to eax |
| 727 | | pop eax; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // Closure is still in eax |
| 732 | |
| 733 | dasm_free(&d); |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 734 | del_local(&new_local); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 735 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 736 | else if (symstreq(fsym, "eval")) |
| 737 | { |
| 738 | if (nargs != 1) |
| 739 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 740 | THROW(EARGS, "eval takes exactly 1 argument"); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 741 | } |
| 742 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 743 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 744 | | push eax; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 745 | | push (env); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 746 | | call_extern eval; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 747 | } |
| 748 | else if (symstreq(fsym, "load")) |
| 749 | { |
| 750 | if (nargs != 1) |
| 751 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 752 | THROW(EARGS, "load takes exactly 1 argument, %d given", nargs); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 753 | } |
| 754 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 755 | TRY(compile_expression(env, local, car(args), false, Dst)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 756 | | push eax; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 757 | | push (local->current_file_path); |
| 758 | | push (env); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 759 | | call_extern load_relative; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 760 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 761 | else |
| 762 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 763 | char *name = (char *)(fsym ^ SYMBOL_TAG); |
| 764 | struct function *func = find_function(env, name); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 765 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 766 | bool is_recursive = false; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 767 | struct args *nargs_needed = NULL; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 768 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 769 | // The number of arguments actually passed on the stack, |
| 770 | // i.e. all varargs are 1. |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 771 | int real_nargs; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 772 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 773 | if (local->current_function_name && |
| 774 | symstreq(fsym, local->current_function_name)) |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 775 | { |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 776 | is_recursive = true; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 777 | nargs_needed = local->args; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 778 | } |
| 779 | else |
| 780 | { |
| 781 | if (func == NULL) |
| 782 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 783 | THROW(EINVALID, "Function %s undefined", name); |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 784 | } |
| 785 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 786 | nargs_needed = func->args; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 787 | } |
| 788 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 789 | if (!are_args_acceptable(nargs_needed, nargs)) |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 790 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 791 | THROW(EARGS, |
| 792 | "wrong number of args in function call: %s, " |
| 793 | "want %d args but given %d\n", |
| 794 | name, nargs_needed->num_required, nargs); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 795 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 796 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 797 | int total_taken = nargs_needed->num_optional + |
| 798 | nargs_needed->num_required; |
| 799 | |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 800 | real_nargs = total_taken + (nargs_needed->variadic ? 1 : 0); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 801 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 802 | if (is_recursive || func->namespace == NS_FUNCTION) |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 803 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 804 | int nargs = length(args); |
| 805 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 806 | int line = cons_line(val); |
| 807 | char *file = cons_file(val); |
| 808 | |
| 809 | if (nargs_needed->variadic) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 810 | { |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 811 | | push (nil); |
| 812 | } |
| 813 | |
| 814 | if (nargs > total_taken && nargs_needed->variadic) |
| 815 | { |
| 816 | // We are passing varargs, which means we need to make a list |
| 817 | |
| 818 | for (int i = nargs - 1; i >= total_taken; i--) |
| 819 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 820 | TRY(compile_expression(env, local, elt(args, i), false, Dst)); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 821 | | push eax; |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 822 | | call_extern cons; |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 823 | | add esp, 8; |
| 824 | | push eax; |
| 825 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 826 | } |
| 827 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 828 | for (int i = nargs_needed->num_optional - 1; |
| 829 | i >= nargs - nargs_needed->num_required; i--) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 830 | { |
| 831 | // Push the default optional values |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 832 | | push (nargs_needed->optional_arguments[i].value); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 833 | } |
| 834 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 835 | int min = MIN(nargs, total_taken); |
| 836 | |
| 837 | for (int i = min - 1; i >= 0; i--) |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 838 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 839 | TRY(compile_expression(env, local, elt(args, i), false, Dst)); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 840 | | push eax; |
| 841 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 842 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 843 | if (is_recursive) |
| 844 | { |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 845 | if (tail) |
| 846 | { |
| 847 | // Move all the arguments pushed to the stack |
| 848 | // back up to the argument bit of the stack. |
| 849 | |
| 850 | for (int i = 0; i < real_nargs; i++) |
| 851 | { |
| 852 | | pop eax; |
| 853 | | mov dword[ebp + (value_size * (i + 2))], eax; |
| 854 | } |
| 855 | |
| 856 | // Jmp back to start |
| 857 | | mov esp, ebp; |
| 858 | | pop ebp; |
| 859 | | jmp <1; |
| 860 | } |
| 861 | else |
| 862 | { |
| 863 | | call <1; |
| 864 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 865 | } |
| 866 | else |
| 867 | { |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 868 | // | mov ebx, (func->code_addr); |
| 869 | | call_extern func->code_addr; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 870 | } |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 871 | | add esp, (real_nargs * value_size); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 872 | // result in eax |
| 873 | } |
| 874 | else if (func->namespace == NS_MACRO) |
| 875 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 876 | // Make sure that the stuff allocated by the macro isn't in a |
| 877 | // pool |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 878 | unsigned char pool = push_pool(0); |
| 879 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 880 | value_t expanded_to = call_list(func, args); |
| 881 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 882 | pop_pool(pool); |
| 883 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 884 | TRY(compile_expression(env, local, expanded_to, false, Dst)); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 885 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 886 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 887 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 888 | else if (symbolp(val)) |
| 889 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 890 | if (symstreq(val, "+current-file+")) |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 891 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 892 | value_t file_name_val = strval(local->current_file_path); |
| 893 | |
| 894 | | mov eax, (file_name_val); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 895 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 896 | else |
| 897 | { |
| 898 | struct variable *v = |
| 899 | find_variable(local, (char *)(val ^ SYMBOL_TAG)); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 900 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 901 | if (!v) |
| 902 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 903 | THROW(EINVALID, "Variable `%s' unbound", (char *)(val ^ SYMBOL_TAG)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 904 | } |
| 905 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 906 | TRY(compile_variable(v, Dst)); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 907 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 908 | } |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 909 | else if (closurep(val)) |
| 910 | { |
| 911 | | mov eax, val; |
| 912 | } |
| 913 | else |
| 914 | { |
| 915 | printval(val, 1); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 916 | THROW(EUNIMPL, "Don't know how to compile this, sorry."); |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 917 | } |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 918 | |
| 919 | OKAY(); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 920 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 921 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 922 | struct variable *add_variable(struct local *local, enum var_type type, |
| 923 | char *name, int number) |
| 924 | { |
| 925 | struct variable *var = malloc(sizeof(struct variable)); |
| 926 | var->prev = local->first; |
| 927 | var->type = type; |
| 928 | var->name = name; |
| 929 | var->number = number; |
| 930 | |
| 931 | local->first = var; |
| 932 | |
| 933 | return var; |
| 934 | } |
| 935 | |
| 936 | void destroy_local(struct local *local) |
| 937 | { |
| 938 | for (struct variable *v = local->first; v;) |
| 939 | { |
| 940 | struct variable *t = v; |
| 941 | v = v->prev; |
| 942 | free(t); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | struct variable *find_variable(struct local *local, char *name) |
| 947 | { |
| 948 | struct variable *v = local->first; |
| 949 | |
| 950 | for (; v && strcmp(v->name, name) != 0; v = v->prev) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 951 | { |
| 952 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 953 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 954 | if (!v) |
| 955 | { |
| 956 | if (local->parent) |
| 957 | { |
| 958 | v = find_variable(local->parent, name); |
| 959 | |
| 960 | if (v) |
| 961 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 962 | // We found this in a parent scope, add it as a V_FREE variable |
| 963 | // to skip the search. |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 964 | v = add_variable(local, V_FREE, name, |
| 965 | local->num_closure_slots++); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | } |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 969 | return v; |
| 970 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 971 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 972 | extern value_t _call_list(void *addr, value_t list, value_t *edi); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 973 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 974 | value_t call_list_args(void *code_ptr, struct args *args, value_t list, |
| 975 | void *data) |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 976 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 977 | list = deep_copy(list); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 978 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 979 | int nargs = length(list); |
| 980 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 981 | value_t *val = &list; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 982 | |
| 983 | for (value_t i = list; !nilp(i); i = cdr(i)) |
| 984 | { |
| 985 | val = cdrref(i); |
| 986 | } |
| 987 | |
| 988 | int total_required = args->num_required + args->num_optional; |
| 989 | |
| 990 | if (nargs > total_required) |
| 991 | { |
| 992 | // Take the remainder of the list and put it as the last item in the |
| 993 | // list. |
| 994 | value_t trailing = cxdr(list, total_required); |
| 995 | value_t last_item = cons(trailing, nil); |
| 996 | |
| 997 | *cxdrref(&list, total_required) = last_item; |
| 998 | } |
| 999 | else if (nargs < total_required) |
| 1000 | { |
| 1001 | for (int i = nargs - args->num_required; i < args->num_optional; i++) |
| 1002 | { |
| 1003 | // Append the i-th defualt argument |
| 1004 | value_t appended = cons(args->optional_arguments[i].value, nil); |
| 1005 | *val = appended; |
| 1006 | val = cdrref(appended); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | // We want to call this if we pass the correct # of arguments or less, just |
| 1011 | // not if we have already passed varargs. Appends a nil argument. |
| 1012 | if (nargs <= total_required) |
| 1013 | { |
| 1014 | // Enough real arguments but no variadic arguments. Pass a nil list. |
| 1015 | *val = cons(nil, nil); |
| 1016 | } |
| 1017 | |
| 1018 | return _call_list(code_ptr, list, data); |
| 1019 | } |
| 1020 | |
| 1021 | value_t call_list(struct function *fun, value_t list) |
| 1022 | { |
| 1023 | return call_list_args(fun->code_ptr, fun->args, list, NULL); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | value_t call_list_closure(struct closure *c, value_t list) |
| 1027 | { |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1028 | return call_list_args(c->function, c->args, list, c->data); |
| 1029 | } |
| 1030 | |
| 1031 | struct args *new_args() |
| 1032 | { |
| 1033 | struct args *a = malloc(sizeof(struct args)); |
| 1034 | a->num_optional = 0; |
| 1035 | a->num_required = 0; |
| 1036 | a->variadic = false; |
| 1037 | |
| 1038 | return a; |
| 1039 | } |
| 1040 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1041 | 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] | 1042 | { |
| 1043 | int i = args->num_optional++; |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1044 | args = |
| 1045 | realloc(args, sizeof(struct args) + sizeof(struct optional_argument) * |
| 1046 | args->num_optional); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1047 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1048 | args->optional_arguments[i] = (struct optional_argument){ |
| 1049 | .value = value, |
| 1050 | .name = name, |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1051 | }; |
| 1052 | |
| 1053 | return args; |
| 1054 | } |
| 1055 | |
| 1056 | bool are_args_acceptable(struct args *args, int number) |
| 1057 | { |
| 1058 | if (args->variadic) |
| 1059 | { |
| 1060 | return number >= args->num_required; |
| 1061 | } |
| 1062 | else |
| 1063 | { |
| 1064 | return number >= args->num_required && |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1065 | number <= args->num_required + args->num_optional; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1069 | struct error list_to_args(struct environment *env, value_t list, |
| 1070 | struct local *local, struct args **a) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1071 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1072 | E_INIT(); |
| 1073 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1074 | struct args *args = new_args(); |
| 1075 | |
| 1076 | bool in_optional = false; |
| 1077 | |
| 1078 | for (value_t i = list; !nilp(i); i = cdr(i)) |
| 1079 | { |
| 1080 | value_t val = car(i); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1081 | NEARVAL(i); |
| 1082 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1083 | if (symbolp(val)) |
| 1084 | { |
| 1085 | if (!args->variadic && symstreq(val, "&")) |
| 1086 | { |
| 1087 | i = cdr(i); |
| 1088 | value_t name = car(i); |
| 1089 | |
| 1090 | if (!symbolp(name)) |
| 1091 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1092 | THROW(EEXPECTED, "You must provide a symbol after & in an argument list " |
| 1093 | "to bind the\n" |
| 1094 | "variadic arguments to."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | args->variadic = true; |
| 1098 | |
| 1099 | add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG), |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1100 | args->num_optional + args->num_required); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1101 | |
| 1102 | continue; |
| 1103 | } |
| 1104 | |
| 1105 | if (!in_optional) |
| 1106 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1107 | add_variable(local, V_ARGUMENT, (char *)(val ^ SYMBOL_TAG), |
| 1108 | args->num_required++); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1109 | } |
| 1110 | else |
| 1111 | { |
| 1112 | char *name = (char *)(val ^ SYMBOL_TAG); |
| 1113 | if (name[0] == '&') |
| 1114 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1115 | THROW(EINVALID, "Non-optional argument following optional arguments " |
| 1116 | "starts with a &\n" |
| 1117 | "did you mean to declare a variadic argument? If so " |
| 1118 | "leave a space\n" |
| 1119 | "between the & and name."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1120 | } |
| 1121 | else |
| 1122 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1123 | THROW(EINVALID, "Cannot define a non-optional argument after an " |
| 1124 | "optional one."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | else if (listp(val)) |
| 1129 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1130 | NEARVAL(val); |
| 1131 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1132 | in_optional = true; |
| 1133 | int len = length(val); |
| 1134 | |
| 1135 | if (len != 2) |
| 1136 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1137 | THROW(EINVALID, "A list defining an optional value must be structured like " |
| 1138 | "(name expr)\n" |
| 1139 | "with exactly two arguments."); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | value_t name = car(val); |
| 1143 | value_t expr = car(cdr(val)); |
| 1144 | |
| 1145 | value_t function = cons(nil, cons(expr, nil)); |
| 1146 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1147 | dasm_State *d; |
| 1148 | TRY(compile_function(function, NS_ANONYMOUS, env, NULL, NULL, NULL, |
| 1149 | NULL, local->current_file_path, &d)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1150 | |
| 1151 | // TODO: GC stack top! |
| 1152 | value_t (*compiled)() = link_program(&d); |
| 1153 | |
| 1154 | value_t value = compiled(); |
| 1155 | args = add_optional_arg(args, name, value); |
| 1156 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1157 | add_variable(local, V_ARGUMENT, (char *)(name ^ SYMBOL_TAG), |
| 1158 | args->num_required + args->num_optional - 1); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 1162 | *a = args; |
| 1163 | OKAY(); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | void display_args(struct args *args) |
| 1167 | { |
| 1168 | printf("Args object taking %d require arguments and %d optionals:\n", |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1169 | args->num_required, args->num_optional); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1170 | |
| 1171 | for (int i = 0; i < args->num_optional; i++) |
| 1172 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1173 | printf(" %d\t%s\n", i, |
| 1174 | (char *)(args->optional_arguments[i].name ^ SYMBOL_TAG)); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 1175 | printval(args->optional_arguments[i].value, 2); |
| 1176 | } |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1177 | } |