swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "lisp.h" |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 4 | #include <dasm_proto.h> |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 5 | #include <stdbool.h> |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 6 | #include <stdint.h> |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 7 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 8 | enum namespace |
| 9 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 10 | /// A function |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 11 | NS_FUNCTION, |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 12 | /// A macro |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 13 | NS_MACRO, |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 14 | /// An anonymous function (a lambda/closure) |
| 15 | NS_ANONYMOUS, |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 16 | }; |
| 17 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 18 | struct function |
| 19 | { |
| 20 | char *name; |
| 21 | int nargs; // number of arguments |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 22 | enum namespace namespace; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 23 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 24 | union |
| 25 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 26 | value_t (*def0)(); |
| 27 | value_t (*def1)(value_t); |
| 28 | value_t (*def2)(value_t, value_t); |
| 29 | value_t (*def3)(value_t, value_t, value_t); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 30 | void *code_ptr; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 31 | uintptr_t code_addr; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | struct function *prev; |
| 35 | }; |
| 36 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 37 | struct loaded_file |
| 38 | { |
| 39 | char *resolved_path; |
| 40 | struct loaded_file *previous; |
| 41 | }; |
| 42 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 43 | struct environment |
| 44 | { |
| 45 | struct function *first; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 46 | struct loaded_file *first_loaded; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 49 | enum var_type |
| 50 | { |
| 51 | V_BOUND, // Bound local variable |
| 52 | V_ARGUMENT, // Bound function argument |
| 53 | V_GLOBAL, // Global variable |
| 54 | V_FREE // Free (lexical) variable |
| 55 | }; |
| 56 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 57 | struct variable |
| 58 | { |
| 59 | char *name; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 60 | uintptr_t number; |
| 61 | enum var_type type; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 62 | struct variable *prev; |
| 63 | }; |
| 64 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 65 | /// Local environment |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 66 | struct local |
| 67 | { |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 68 | /// Parent environment, NULL if none (root). |
| 69 | struct local *parent; |
| 70 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 71 | /// Name that the current function should be referred to by, e.g. `recurse` |
| 72 | /// for a lambda. |
| 73 | char *current_function_name; |
| 74 | |
| 75 | int num_vars, num_args; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 76 | /// Most recently defined variable |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 77 | struct variable *first; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 78 | int npc; |
| 79 | int nextpc; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 80 | bool *stack_slots; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 81 | /// Number of slots allocated in `stack_slots` |
| 82 | int num_stack_slots; |
| 83 | /// Number of entries used in `stack_slots` |
| 84 | int num_stack_entries; |
| 85 | /// Number of closure slots total (allocated as V_FREE variables) |
| 86 | int num_closure_slots; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 89 | void compile_expression(struct environment *env, struct local *local, |
| 90 | value_t val, dasm_State **Dst); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 93 | * Compile a function |
| 94 | * @param args The function args and body, e.g. `((b c) d)` |
| 95 | * @param namespace The function namespace. |
| 96 | * @param env The environment. |
| 97 | * @param local_out The local environment generated for this function will be |
| 98 | * returned here. NULL if you do not care about it being returned (you probably |
| 99 | * should since you need to free the stack slot allocation map). |
| 100 | * @param local_parent Parent local environment, only needed for closures. NULL |
| 101 | * if no parent. |
| 102 | * @param nargs The number of arguments for this function will be returned here. |
| 103 | * NULL if you don't care about it. |
| 104 | * @returns The compiled function state. You should probably give this to |
| 105 | * `add_function` or something similar. |
| 106 | */ |
| 107 | struct dasm_State *compile_function(value_t args, enum namespace namespace, |
| 108 | struct environment *env, struct local *local_out, |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 109 | struct local *local_parent, int *nargs, char *name); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 110 | |
| 111 | void compile_variable(struct variable *v, dasm_State *Dst); |
| 112 | |
| 113 | /** |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 114 | * Compile a backquoted expression |
| 115 | */ |
| 116 | void compile_backquote(struct environment *env, struct local *local, |
| 117 | value_t val, dasm_State **Dst); |
| 118 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 119 | void compile_expr_to_func(struct environment *env, char *name, value_t val, |
| 120 | dasm_State **Dst); |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 121 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 122 | int nextpc(struct local *local, dasm_State **Dst); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 123 | |
| 124 | // Local utilities |
| 125 | unsigned int local_alloc(struct local *local); |
| 126 | void local_free(struct local *local, unsigned int slot); |
| 127 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 128 | /** |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 129 | * Deletes the memory allocated in `local`. Does not actually call `free()` on |
| 130 | * `local` itself, but frees the variables and stack slots associated with it. |
| 131 | */ |
| 132 | void del_local(struct local *local); |
| 133 | |
| 134 | /** |
| 135 | * Deletes the memory allocated in `env`. Does not actually call `free()` on |
| 136 | * `env` itself. |
| 137 | */ |
| 138 | void del_env(struct environment *env); |
| 139 | |
| 140 | /** |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 141 | * Walk `body` and reserve space in `local` for any variable declarations. |
| 142 | */ |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 143 | void walk_and_alloc(struct local *local, value_t body); |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 144 | // Compile top-level declaration |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 145 | void compile_tl(value_t val, struct environment *env); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 146 | |
| 147 | /** |
| 148 | * Compile a file in a new environment. |
| 149 | * @param filename The path to the file. |
| 150 | * @param ok Set to `true` if loading succeeds, `false` otherwise. If `ok` is |
| 151 | * NULL it is ignored. |
| 152 | * @returns The environment for the compiled file, or an empty environment if |
| 153 | * `ok` was set to `false` (i.e. the file could not be compiled). |
| 154 | */ |
| 155 | struct environment compile_file(char *filename, bool *ok); |
| 156 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 157 | struct function *find_function(struct environment *env, char *name); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 158 | struct variable *add_variable(struct local *local, enum var_type type, |
| 159 | char *name, int number); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Find a variable in `local` with name `name`. |
| 163 | * @returns The variable, NULL if not found. |
| 164 | */ |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 165 | struct variable *find_variable(struct local *local, char *name); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 166 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 167 | void destroy_local(struct local *local); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * Like `apply` in lisp, calls func with list args and returns the result. |
| 171 | */ |
| 172 | value_t call_list(struct function *func, value_t list); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 173 | value_t call_list_closure(struct closure *c, value_t list); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame^] | 174 | |
| 175 | /** |
| 176 | * Load a lisp file into the current environment. |
| 177 | * @returns `true` if succesful, `false` otherwise. |
| 178 | */ |
| 179 | bool load(struct environment *env, char *path); |
| 180 | |
| 181 | /** |
| 182 | * Mark a file `path` as loaded in the environment. `path` will be expanded with |
| 183 | * `readlink`. You can pass a temporary string here, memory will be allocated by |
| 184 | * this function as needed. |
| 185 | */ |
| 186 | void add_load(struct environment *env, char *path); |