swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 3 | #include "error.h" |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 4 | #include "istream.h" |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 5 | #include <stdbool.h> |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 7 | #include <stdarg.h> |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 8 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 9 | #define INT_MASK 0b11 |
| 10 | #define INT_TAG 0b00 |
| 11 | |
| 12 | #define CHAR_MASK 0xff |
| 13 | #define CHAR_TAG 0b00001111 |
| 14 | |
| 15 | #define BOOL_MASK 0b1111111 |
| 16 | #define BOOL_TAG 0b0011111 |
| 17 | |
| 18 | #define HEAP_MASK 0b111 |
| 19 | |
| 20 | #define CONS_TAG 0b001 |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 21 | #define CLASS_TAG 0b010 |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 22 | #define STRING_TAG 0b011 |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 23 | #define SYMBOL_TAG 0b101 |
| 24 | #define CLOSURE_TAG 0b110 |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 25 | |
| 26 | struct cons; |
| 27 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 28 | /// Represents a Lisp value |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 29 | typedef unsigned int value_t; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 30 | |
| 31 | struct cons |
| 32 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 33 | value_t car, cdr; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 34 | |
| 35 | /// Line of the input file from where this was parsed, 0 if it was created |
| 36 | /// in Lisp. |
| 37 | int line; |
| 38 | |
| 39 | /// Description of where the cons was parsed from, or NULL if generated in |
| 40 | /// code. |
| 41 | char *name; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Represents how many arguments a function takes. |
| 46 | */ |
| 47 | struct args |
| 48 | { |
| 49 | /// The minimum valid number of arguments |
| 50 | int num_required; |
| 51 | |
| 52 | /// The number of optional values |
| 53 | int num_optional; |
| 54 | |
| 55 | /// Does this function accept variadic arguments? If `true`, any arguments |
| 56 | /// after the required and optional arguments will be `cons`-ed to a list |
| 57 | /// and passed as a final argument. |
| 58 | bool variadic; |
| 59 | |
| 60 | /// The default values for the optional arguments, as expressions. These |
| 61 | /// should be evaluated at the call site. They are known not to reference |
| 62 | /// anything that could clash with scope at the call site. |
| 63 | struct optional_argument |
| 64 | { |
| 65 | /// The default value of this argument |
| 66 | value_t value; |
| 67 | |
| 68 | /// The name of this argument as a symbol |
| 69 | value_t name; |
| 70 | } optional_arguments[]; |
| 71 | }; |
| 72 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 73 | struct closure |
| 74 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 75 | /// How many arguments does this closure take |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 76 | struct args *args; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 77 | /// How many free variables does it capture (i.e. length of `data`) |
| 78 | int num_captured; |
| 79 | /// The function pointer itself |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 80 | void *function; |
| 81 | |
| 82 | /// This will be passed in edi. |
| 83 | value_t data[]; |
| 84 | }; |
| 85 | |
swissChili | 9d428a8 | 2022-08-01 20:47:40 -0700 | [diff] [blame] | 86 | struct class |
| 87 | { |
| 88 | // A symbol representing the name of the class this is an instance |
| 89 | // of. |
| 90 | value_t type; |
| 91 | |
| 92 | int num_members; |
| 93 | // C data |
| 94 | void *cdata; |
| 95 | value_t members[]; |
| 96 | }; |
| 97 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 98 | /// Default pool (no pool) |
| 99 | #define NO_POOL 0 |
| 100 | |
| 101 | /** |
| 102 | * The max used pool number, don't touch this. |
| 103 | */ |
| 104 | extern unsigned char max_pool; |
| 105 | |
| 106 | /** |
| 107 | * Current allocation pool, default 0 (no pool) |
| 108 | */ |
| 109 | extern unsigned char current_pool; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 110 | |
| 111 | // It is integral that this be 16 bytes long so that whatever follows it is |
| 112 | // still aligned to 4 bits. |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 113 | struct alloc |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 114 | { |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 115 | /** |
| 116 | * One of the type tags, eg CONS_TAG, etc |
| 117 | */ |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 118 | unsigned int type_tag; // 4 |
| 119 | struct alloc *prev, *next; // + 8 |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Zero if this is not part of a release pool, pool number otherwise. |
| 122 | */ |
| 123 | unsigned char pool; // + 1 |
| 124 | /** |
| 125 | * Reserved for the GC. |
| 126 | */ |
swissChili | a890aed | 2022-07-30 17:13:07 -0700 | [diff] [blame] | 127 | unsigned int mark : 24; // + 3 = 16 |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 128 | |
| 129 | // Whatever else |
| 130 | }; |
| 131 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 132 | extern struct alloc *first_a, *last_a; |
| 133 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 134 | struct cons_alloc |
| 135 | { |
| 136 | struct alloc alloc; |
| 137 | struct cons cons; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 140 | struct closure_alloc |
| 141 | { |
| 142 | struct alloc alloc; |
| 143 | struct closure closure; |
| 144 | }; |
| 145 | |
swissChili | 9d428a8 | 2022-08-01 20:47:40 -0700 | [diff] [blame] | 146 | struct class_alloc |
| 147 | { |
| 148 | struct alloc alloc; |
| 149 | struct class class; |
| 150 | }; |
| 151 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Create a new allocation pool. |
| 154 | */ |
| 155 | unsigned char make_pool(); |
| 156 | |
| 157 | /** |
| 158 | * Set the allocation pull |
| 159 | * @returns the old pool, you should reset this later with pop_pool. |
| 160 | */ |
| 161 | unsigned char push_pool(unsigned char pool); |
| 162 | |
| 163 | /** |
| 164 | * Set the allocation pool and throw away the old value. |
| 165 | */ |
| 166 | void pop_pool(unsigned char pool); |
| 167 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 168 | void add_to_pool(value_t form); |
| 169 | |
swissChili | c0acce4 | 2022-07-31 13:38:17 -0700 | [diff] [blame] | 170 | void del_alloc(struct alloc *alloc); |
| 171 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 172 | /** |
| 173 | * @returns true if pool is still alive (in scope). |
| 174 | */ |
| 175 | bool pool_alive(unsigned char pool); |
| 176 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 177 | bool startswith(struct istream *s, char *pattern); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 178 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 179 | struct error readsym(struct istream *is, value_t *val) WARN_UNUSED; |
| 180 | struct error readstr(struct istream *is, value_t *val) WARN_UNUSED; |
| 181 | struct error readlist(struct istream *is, value_t *val) WARN_UNUSED; |
| 182 | struct error readint(struct istream *is, value_t *val) WARN_UNUSED; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 183 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Read a quoted form, including `'` (quote) `\`` (backquote) and `,` (unquote) |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 186 | */ |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 187 | struct error readquote(struct istream *is, value_t *val) WARN_UNUSED; |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 188 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 189 | value_t intval(int i); |
| 190 | value_t strval(char *str); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 191 | value_t symval(char *str); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 192 | value_t cons(value_t car, value_t cdr); |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 193 | value_t merge2(value_t front, value_t back); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 194 | struct error read1(struct istream *is, value_t *val) WARN_UNUSED; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 195 | value_t read(struct istream *is); |
| 196 | value_t readn(struct istream *is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 197 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 198 | value_t car(value_t v); |
| 199 | value_t cdr(value_t v); |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 200 | /// Return a pointer to the "nil" tail of the list, or NULL if you do |
| 201 | /// something stupid. |
swissChili | 9d428a8 | 2022-08-01 20:47:40 -0700 | [diff] [blame] | 202 | value_t *nilptr(value_t *val); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 203 | value_t *carref(value_t v); |
| 204 | value_t *cdrref(value_t v); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 205 | /// @returns the `index`-th `cdr` |
| 206 | value_t cxdr(value_t v, int index); |
| 207 | /// @returns a reference to the `index`-th `cdr` |
| 208 | value_t *cxdrref(value_t *v, int index); |
| 209 | |
| 210 | value_t deep_copy(value_t val); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 211 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 212 | int cons_line(value_t val); |
| 213 | char *cons_file(value_t val); |
| 214 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 215 | bool integerp(value_t v); |
| 216 | bool symbolp(value_t v); |
| 217 | bool stringp(value_t v); |
| 218 | bool consp(value_t v); |
| 219 | bool listp(value_t v); |
| 220 | bool nilp(value_t v); |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 221 | bool heapp(value_t v); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 222 | bool closurep(value_t v); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 223 | int length(value_t v); |
| 224 | value_t elt(value_t v, int index); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 225 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 226 | void printval(value_t v, int depth); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 227 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 228 | bool symstreq(value_t sym, char *str); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 229 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 230 | value_t create_closure(void *code, struct args *args, int ncaptures); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * Set the `index`th capture variable of `closure`. This should really only be |
| 234 | * called when creating a new closure. |
| 235 | */ |
| 236 | void set_closure_capture_variable(int index, value_t value, value_t closure); |
| 237 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 238 | extern value_t nil; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 239 | extern value_t t; |