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 | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 18 | struct args *new_args(); |
| 19 | struct args *add_optional_arg(struct args *args, value_t name, |
| 20 | value_t expression); |
| 21 | |
| 22 | /** |
| 23 | * @returns if `number` is an acceptable number of arguments for `args`. |
| 24 | */ |
| 25 | bool are_args_acceptable(struct args *args, int number); |
| 26 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 27 | struct function |
| 28 | { |
| 29 | char *name; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 30 | struct args *args; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 31 | enum namespace namespace; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 32 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 33 | union { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 34 | value_t (*def0)(); |
| 35 | value_t (*def1)(value_t); |
| 36 | value_t (*def2)(value_t, value_t); |
| 37 | value_t (*def3)(value_t, value_t, value_t); |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 38 | void *code_ptr; |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 39 | uintptr_t code_addr; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | struct function *prev; |
| 43 | }; |
| 44 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 45 | struct loaded_file |
| 46 | { |
| 47 | char *resolved_path; |
| 48 | struct loaded_file *previous; |
| 49 | }; |
| 50 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 51 | struct environment |
| 52 | { |
| 53 | struct function *first; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 54 | struct loaded_file *first_loaded; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 57 | enum var_type |
| 58 | { |
| 59 | V_BOUND, // Bound local variable |
| 60 | V_ARGUMENT, // Bound function argument |
| 61 | V_GLOBAL, // Global variable |
| 62 | V_FREE // Free (lexical) variable |
| 63 | }; |
| 64 | |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 65 | struct variable |
| 66 | { |
| 67 | char *name; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 68 | uintptr_t number; |
| 69 | enum var_type type; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 70 | struct variable *prev; |
| 71 | }; |
| 72 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 73 | /// Local environment |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 74 | struct local |
| 75 | { |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 76 | /// Parent environment, NULL if none (root). |
| 77 | struct local *parent; |
| 78 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 79 | /// Name that the current function should be referred to by, e.g. `recurse` |
| 80 | /// for a lambda. |
| 81 | char *current_function_name; |
| 82 | |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 83 | /// Path to the current file |
| 84 | char *current_file_path; |
| 85 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 86 | int num_vars; |
| 87 | struct args *args; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 88 | /// Most recently defined variable |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 89 | struct variable *first; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 90 | int npc; |
| 91 | int nextpc; |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 92 | bool *stack_slots; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 93 | /// Number of slots allocated in `stack_slots` |
| 94 | int num_stack_slots; |
| 95 | /// Number of entries used in `stack_slots` |
| 96 | int num_stack_entries; |
| 97 | /// Number of closure slots total (allocated as V_FREE variables) |
| 98 | int num_closure_slots; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Parse a list of arguments to an args object and add them as V_ARGUMENT |
| 103 | * variables to `local`. The list should be in the same format as is accepted by |
| 104 | * `defun`, `defmacro`, `lambda`, etc. |
| 105 | * @returns NULL if the list is malformed. |
| 106 | */ |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 107 | struct error list_to_args(struct environment *env, value_t list, |
| 108 | struct local *local, struct args **args); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Print out `args` to stdout. Useful for debugging. |
| 112 | */ |
| 113 | void display_args(struct args *args); |
| 114 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 115 | struct error compile_expression(struct environment *env, struct local *local, |
| 116 | value_t val, bool tail, dasm_State **Dst) WARN_UNUSED; |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 119 | * Compile a function |
| 120 | * @param args The function args and body, e.g. `((b c) d)` |
| 121 | * @param namespace The function namespace. |
| 122 | * @param env The environment. |
| 123 | * @param local_out The local environment generated for this function will be |
| 124 | * returned here. NULL if you do not care about it being returned (you probably |
| 125 | * should since you need to free the stack slot allocation map). |
| 126 | * @param local_parent Parent local environment, only needed for closures. NULL |
| 127 | * if no parent. |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 128 | * @param args An object representing the arguments this function accepts will |
| 129 | * be returned here. Set this to NULL if you don't care. |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 130 | * @returns The compiled function state. You should probably give this to |
| 131 | * `add_function` or something similar. |
| 132 | */ |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 133 | struct error compile_function(value_t args, enum namespace namespace, |
| 134 | struct environment *env, |
| 135 | struct local *local_out, |
| 136 | struct local *local_parent, |
| 137 | struct args **ar, char *name, char *path, |
| 138 | dasm_State **s) WARN_UNUSED; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 139 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 140 | struct error compile_variable(struct variable *v, dasm_State *Dst) WARN_UNUSED; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 143 | * Compile a backquoted expression |
| 144 | */ |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 145 | struct error compile_backquote(struct environment *env, struct local *local, |
| 146 | value_t val, dasm_State **Dst) WARN_UNUSED; |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 147 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 148 | int nextpc(struct local *local, dasm_State **Dst); |
swissChili | 67bdf28 | 2021-06-06 18:46:08 -0700 | [diff] [blame] | 149 | |
| 150 | // Local utilities |
| 151 | unsigned int local_alloc(struct local *local); |
| 152 | void local_free(struct local *local, unsigned int slot); |
| 153 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 154 | /** |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 155 | * Deletes the memory allocated in `local`. Does not actually call `free()` on |
| 156 | * `local` itself, but frees the variables and stack slots associated with it. |
| 157 | */ |
| 158 | void del_local(struct local *local); |
| 159 | |
| 160 | /** |
| 161 | * Deletes the memory allocated in `env`. Does not actually call `free()` on |
| 162 | * `env` itself. |
| 163 | */ |
| 164 | void del_env(struct environment *env); |
| 165 | |
| 166 | /** |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 167 | * Walk `body` and reserve space in `local` for any variable declarations. |
| 168 | */ |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 169 | struct error walk_and_alloc(struct environment *env, struct local *local, value_t *body, bool quoted); |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * Compile a top level definition |
| 173 | * @param fname The path to the current file. |
| 174 | * @param val The expression to compile. |
| 175 | */ |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 176 | struct error compile_tl(value_t val, struct environment *env, char *fname) WARN_UNUSED; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Compile a file in a new environment. |
| 180 | * @param filename The path to the file. |
| 181 | * @param ok Set to `true` if loading succeeds, `false` otherwise. If `ok` is |
| 182 | * NULL it is ignored. |
| 183 | * @returns The environment for the compiled file, or an empty environment if |
| 184 | * `ok` was set to `false` (i.e. the file could not be compiled). |
| 185 | */ |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 186 | struct error compile_file(char *filename, struct environment **env) WARN_UNUSED; |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 187 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 188 | struct function *find_function(struct environment *env, char *name); |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 189 | struct variable *add_variable(struct local *local, enum var_type type, |
| 190 | char *name, int number); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Find a variable in `local` with name `name`. |
| 194 | * @returns The variable, NULL if not found. |
| 195 | */ |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 196 | struct variable *find_variable(struct local *local, char *name); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 197 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 198 | void destroy_local(struct local *local); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * Like `apply` in lisp, calls func with list args and returns the result. |
| 202 | */ |
| 203 | value_t call_list(struct function *func, value_t list); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 204 | value_t call_list_closure(struct closure *c, value_t list); |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Load a lisp file into the current environment. |
| 208 | * @returns `true` if succesful, `false` otherwise. |
| 209 | */ |
| 210 | bool load(struct environment *env, char *path); |
| 211 | |
| 212 | /** |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 213 | * Load a file relative to another file. |
| 214 | * @param to The file to load relative to. |
| 215 | * @param name The name or relative path of the file to load. |
| 216 | * @param env The environment to load in. |
| 217 | */ |
| 218 | value_t load_relative(struct environment *env, char *to, value_t name); |
| 219 | |
| 220 | /** |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 221 | * Mark a file `path` as loaded in the environment. `path` will be expanded with |
| 222 | * `readlink`. You can pass a temporary string here, memory will be allocated by |
| 223 | * this function as needed. |
| 224 | */ |
| 225 | void add_load(struct environment *env, char *path); |