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