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