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