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 | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 86 | /// Default pool (no pool) |
| 87 | #define NO_POOL 0 |
| 88 | |
| 89 | /** |
| 90 | * The max used pool number, don't touch this. |
| 91 | */ |
| 92 | extern unsigned char max_pool; |
| 93 | |
| 94 | /** |
| 95 | * Current allocation pool, default 0 (no pool) |
| 96 | */ |
| 97 | extern unsigned char current_pool; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 98 | |
| 99 | // It is integral that this be 16 bytes long so that whatever follows it is |
| 100 | // still aligned to 4 bits. |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 101 | struct alloc |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 102 | { |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 103 | /** |
| 104 | * One of the type tags, eg CONS_TAG, etc |
| 105 | */ |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 106 | unsigned int type_tag; // 4 |
| 107 | struct alloc *prev, *next; // + 8 |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Zero if this is not part of a release pool, pool number otherwise. |
| 110 | */ |
| 111 | unsigned char pool; // + 1 |
| 112 | /** |
| 113 | * Reserved for the GC. |
| 114 | */ |
swissChili | a890aed | 2022-07-30 17:13:07 -0700 | [diff] [blame] | 115 | unsigned int mark : 24; // + 3 = 16 |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 116 | |
| 117 | // Whatever else |
| 118 | }; |
| 119 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 120 | extern struct alloc *first_a, *last_a; |
| 121 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 122 | struct cons_alloc |
| 123 | { |
| 124 | struct alloc alloc; |
| 125 | struct cons cons; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 128 | struct closure_alloc |
| 129 | { |
| 130 | struct alloc alloc; |
| 131 | struct closure closure; |
| 132 | }; |
| 133 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Create a new allocation pool. |
| 136 | */ |
| 137 | unsigned char make_pool(); |
| 138 | |
| 139 | /** |
| 140 | * Set the allocation pull |
| 141 | * @returns the old pool, you should reset this later with pop_pool. |
| 142 | */ |
| 143 | unsigned char push_pool(unsigned char pool); |
| 144 | |
| 145 | /** |
| 146 | * Set the allocation pool and throw away the old value. |
| 147 | */ |
| 148 | void pop_pool(unsigned char pool); |
| 149 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 150 | void add_to_pool(value_t form); |
| 151 | |
swissChili | c0acce4 | 2022-07-31 13:38:17 -0700 | [diff] [blame] | 152 | void del_alloc(struct alloc *alloc); |
| 153 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 154 | /** |
| 155 | * @returns true if pool is still alive (in scope). |
| 156 | */ |
| 157 | bool pool_alive(unsigned char pool); |
| 158 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 159 | bool startswith(struct istream *s, char *pattern); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 160 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 161 | struct error readsym(struct istream *is, value_t *val) WARN_UNUSED; |
| 162 | struct error readstr(struct istream *is, value_t *val) WARN_UNUSED; |
| 163 | struct error readlist(struct istream *is, value_t *val) WARN_UNUSED; |
| 164 | struct error readint(struct istream *is, value_t *val) WARN_UNUSED; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 165 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Read a quoted form, including `'` (quote) `\`` (backquote) and `,` (unquote) |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 168 | */ |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 169 | struct error readquote(struct istream *is, value_t *val) WARN_UNUSED; |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 170 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 171 | value_t intval(int i); |
| 172 | value_t strval(char *str); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 173 | value_t symval(char *str); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 174 | value_t cons(value_t car, value_t cdr); |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 175 | value_t merge2(value_t front, value_t back); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 176 | struct error read1(struct istream *is, value_t *val) WARN_UNUSED; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 177 | value_t read(struct istream *is); |
| 178 | value_t readn(struct istream *is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 179 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 180 | value_t car(value_t v); |
| 181 | value_t cdr(value_t v); |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 182 | /// Return a pointer to the "nil" tail of the list, or NULL if you do |
| 183 | /// something stupid. |
| 184 | value_t *nilptr(value_t val); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 185 | value_t *carref(value_t v); |
| 186 | value_t *cdrref(value_t v); |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 187 | /// @returns the `index`-th `cdr` |
| 188 | value_t cxdr(value_t v, int index); |
| 189 | /// @returns a reference to the `index`-th `cdr` |
| 190 | value_t *cxdrref(value_t *v, int index); |
| 191 | |
| 192 | value_t deep_copy(value_t val); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 193 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 194 | int cons_line(value_t val); |
| 195 | char *cons_file(value_t val); |
| 196 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 197 | bool integerp(value_t v); |
| 198 | bool symbolp(value_t v); |
| 199 | bool stringp(value_t v); |
| 200 | bool consp(value_t v); |
| 201 | bool listp(value_t v); |
| 202 | bool nilp(value_t v); |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 203 | bool heapp(value_t v); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 204 | bool closurep(value_t v); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 205 | int length(value_t v); |
| 206 | value_t elt(value_t v, int index); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 207 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 208 | void printval(value_t v, int depth); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 209 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 210 | bool symstreq(value_t sym, char *str); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 211 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 212 | value_t create_closure(void *code, struct args *args, int ncaptures); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * Set the `index`th capture variable of `closure`. This should really only be |
| 216 | * called when creating a new closure. |
| 217 | */ |
| 218 | void set_closure_capture_variable(int index, value_t value, value_t closure); |
| 219 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 220 | extern value_t nil; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 221 | extern value_t t; |