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 | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 42 | struct closure |
| 43 | { |
| 44 | int num_args; |
| 45 | void *function; |
| 46 | |
| 47 | /// This will be passed in edi. |
| 48 | value_t data[]; |
| 49 | }; |
| 50 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 51 | /// Default pool (no pool) |
| 52 | #define NO_POOL 0 |
| 53 | |
| 54 | /** |
| 55 | * The max used pool number, don't touch this. |
| 56 | */ |
| 57 | extern unsigned char max_pool; |
| 58 | |
| 59 | /** |
| 60 | * Current allocation pool, default 0 (no pool) |
| 61 | */ |
| 62 | extern unsigned char current_pool; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 63 | |
| 64 | // It is integral that this be 16 bytes long so that whatever follows it is |
| 65 | // still aligned to 4 bits. |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 66 | struct alloc |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 67 | { |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 68 | /** |
| 69 | * One of the type tags, eg CONS_TAG, etc |
| 70 | */ |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 71 | unsigned int type_tag; // 4 |
| 72 | struct alloc *prev, *next; // + 8 |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Zero if this is not part of a release pool, pool number otherwise. |
| 75 | */ |
| 76 | unsigned char pool; // + 1 |
| 77 | /** |
| 78 | * Reserved for the GC. |
| 79 | */ |
| 80 | unsigned int mark : 24; // + 2 = 16 |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 81 | |
| 82 | // Whatever else |
| 83 | }; |
| 84 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 85 | extern struct alloc *first_a, *last_a; |
| 86 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 87 | struct cons_alloc |
| 88 | { |
| 89 | struct alloc alloc; |
| 90 | struct cons cons; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 93 | struct closure_alloc |
| 94 | { |
| 95 | struct alloc alloc; |
| 96 | struct closure closure; |
| 97 | }; |
| 98 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Create a new allocation pool. |
| 101 | */ |
| 102 | unsigned char make_pool(); |
| 103 | |
| 104 | /** |
| 105 | * Set the allocation pull |
| 106 | * @returns the old pool, you should reset this later with pop_pool. |
| 107 | */ |
| 108 | unsigned char push_pool(unsigned char pool); |
| 109 | |
| 110 | /** |
| 111 | * Set the allocation pool and throw away the old value. |
| 112 | */ |
| 113 | void pop_pool(unsigned char pool); |
| 114 | |
| 115 | /** |
| 116 | * @returns true if pool is still alive (in scope). |
| 117 | */ |
| 118 | bool pool_alive(unsigned char pool); |
| 119 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 120 | bool startswith(struct istream *s, char *pattern); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 121 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 122 | bool readsym(struct istream *is, value_t *val); |
| 123 | bool readstr(struct istream *is, value_t *val); |
| 124 | bool readlist(struct istream *is, value_t *val); |
| 125 | bool readint(struct istream *is, value_t *val); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 126 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Read a quoted form, including `'` (quote) `\`` (backquote) and `,` (unquote) |
| 129 | * @returns true if read successfully, and sets `val`. |
| 130 | */ |
| 131 | bool readquote(struct istream *is, value_t *val); |
| 132 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 133 | value_t intval(int i); |
| 134 | value_t strval(char *str); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 135 | value_t symval(char *str); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 136 | value_t cons(value_t car, value_t cdr); |
| 137 | bool read1(struct istream *is, value_t *val); |
| 138 | value_t read(struct istream *is); |
| 139 | value_t readn(struct istream *is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 140 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 141 | value_t car(value_t v); |
| 142 | value_t cdr(value_t v); |
| 143 | value_t *carref(value_t v); |
| 144 | value_t *cdrref(value_t v); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 145 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 146 | int cons_line(value_t val); |
| 147 | char *cons_file(value_t val); |
| 148 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 149 | bool integerp(value_t v); |
| 150 | bool symbolp(value_t v); |
| 151 | bool stringp(value_t v); |
| 152 | bool consp(value_t v); |
| 153 | bool listp(value_t v); |
| 154 | bool nilp(value_t v); |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 155 | bool heapp(value_t v); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 156 | int length(value_t v); |
| 157 | value_t elt(value_t v, int index); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 158 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 159 | void printval(value_t v, int depth); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 160 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 161 | void err(const char *msg); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 162 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 163 | bool symstreq(value_t sym, char *str); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 164 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 165 | extern value_t nil; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 166 | extern value_t t; |