swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 1 | #include "lisp.h" |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 2 | #include "error.h" |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 3 | #include "plat/plat.h" |
| 4 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 5 | #include <ctype.h> |
| 6 | #include <stdbool.h> |
| 7 | #include <stdio.h> |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 10 | #include <stdarg.h> |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 11 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 12 | struct alloc *first_a = NULL, *last_a = NULL; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 13 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 14 | value_t nil = 0b00101111; // magic ;) |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 15 | value_t t = 1 << 3; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 16 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 17 | unsigned char max_pool = 0, current_pool = 0; |
| 18 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 19 | value_t intval(int i) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 20 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 21 | i <<= 2; |
| 22 | i |= INT_TAG; |
| 23 | return i; |
| 24 | } |
| 25 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 26 | void add_this_alloc(struct alloc *a, int tag) |
| 27 | { |
| 28 | a->type_tag = tag; |
| 29 | a->pool = current_pool; |
| 30 | |
| 31 | if (last_a) |
| 32 | { |
| 33 | a->prev = last_a; |
| 34 | last_a->next = a; |
| 35 | a->next = NULL; |
| 36 | last_a = a; |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | a->prev = a->next = NULL; |
| 41 | first_a = last_a = a; |
| 42 | } |
| 43 | } |
| 44 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 45 | value_t cons(value_t car, value_t cdr) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 46 | { |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 47 | struct cons_alloc *item = malloc_aligned(sizeof(struct cons_alloc)); |
| 48 | struct cons *c = &item->cons; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 49 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 50 | c->car = car; |
| 51 | c->cdr = cdr; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 52 | c->line = 0; |
| 53 | c->name = NULL; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 54 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 55 | value_t v = (value_t)c; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 56 | v |= CONS_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 57 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 58 | add_this_alloc(&item->alloc, CONS_TAG); |
| 59 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 60 | return v; |
| 61 | } |
| 62 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 63 | void skipws(struct istream *is) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 64 | { |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 65 | start: |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 66 | while (isspace(is->peek(is))) |
| 67 | is->get(is); |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 68 | |
| 69 | if (is->peek(is) == ';') |
| 70 | { |
| 71 | while (is->get(is) != '\n') |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 72 | { |
| 73 | } |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 74 | |
| 75 | // Only time I ever use labels is for stuff like this. Compiler would |
| 76 | // probably optimize this if I used recursion but I don't want to |
| 77 | // bother. |
| 78 | goto start; |
| 79 | } |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 80 | } |
| 81 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 82 | bool isallowedchar(char c) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 83 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 84 | return (c >= '#' && c <= '\'') || (c >= '*' && c <= '/') || |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 85 | (c >= '<' && c <= '@'); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 86 | } |
| 87 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 88 | bool issymstart(char c) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 89 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 90 | return isalpha(c) || isallowedchar(c); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 91 | } |
| 92 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 93 | bool issym(char c) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 94 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 95 | return isalpha(c) || isallowedchar(c) || isdigit(c); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 96 | } |
| 97 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 98 | struct error readsym(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 99 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 100 | E_INIT(); |
| 101 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 102 | skipws(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 103 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 104 | if (!issymstart(is->peek(is))) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 105 | THROWSAFE(EEXPECTED); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 106 | |
| 107 | int size = 8; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 108 | struct alloc *a = malloc_aligned(size + sizeof(struct alloc)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 109 | |
| 110 | char *s = (char *)(a + 1); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 111 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 112 | s[0] = is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 113 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 114 | for (int i = 1;; i++) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 115 | { |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 116 | if (i >= size) |
| 117 | { |
| 118 | size *= 2; |
| 119 | a = realloc_aligned(a, size + sizeof(struct alloc)); |
| 120 | s = (char *)(a + 1); |
| 121 | } |
| 122 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 123 | if (issym(is->peek(is))) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 124 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 125 | s[i] = is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 126 | } |
| 127 | else |
| 128 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 129 | s[i] = 0; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 130 | *val = (value_t)s; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 131 | *val |= SYMBOL_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 132 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 133 | add_this_alloc(a, SYMBOL_TAG); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 134 | |
| 135 | OKAY(); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 140 | struct error readstr(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 141 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 142 | E_INIT(); |
| 143 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 144 | skipws(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 145 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 146 | if (is->peek(is) != '"') |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 147 | THROWSAFE(EEXPECTED); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 148 | |
| 149 | bool escape = false; |
| 150 | int size = 8; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 151 | |
| 152 | struct alloc *a = malloc_aligned(size + sizeof(struct alloc)); |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 153 | |
| 154 | char *s = (char *)(a + 1); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 155 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 156 | (void)is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 157 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 158 | for (int i = 0;; i++) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 159 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 160 | if (is->peek(is) != '"') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 161 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 162 | if (i >= size) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 163 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 164 | size *= 2; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 165 | a = realloc_aligned(a, size + sizeof(struct alloc)); |
| 166 | s = (char *)(a + 1); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 167 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 168 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 169 | char c = is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 170 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 171 | if (escape && c == 'n') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 172 | c = '\n'; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 173 | else if (escape && c == '\\') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 174 | c = '\\'; |
| 175 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 176 | if (c == '\\' && !escape) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 177 | { |
| 178 | escape = true; |
| 179 | i--; // will be incremented again, UGLY. |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | escape = false; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 184 | s[i] = c; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | else |
| 188 | { |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 189 | s[i] = '\0'; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 190 | is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 191 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 192 | *val = (value_t)s; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 193 | *val |= STRING_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 194 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 195 | add_this_alloc(a, STRING_TAG); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 196 | |
| 197 | OKAY(); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 202 | void printval_ol(value_t v) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 203 | { |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 204 | // for (int i = 0; i < depth; i++) |
| 205 | // printf(" "); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 206 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 207 | if (symbolp(v)) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 208 | { |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 209 | printf("%s", (char *)(v ^ SYMBOL_TAG)); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 210 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 211 | else if (stringp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 212 | { |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 213 | printf("\"%s\"", (char *)(v ^ STRING_TAG)); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 214 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 215 | else if (integerp(v)) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 216 | { |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 217 | printf("%d", v >> 2); |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 218 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 219 | else if (consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 220 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 221 | if (listp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 222 | { |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 223 | printf("("); |
| 224 | printval_ol(car(v)); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 225 | |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 226 | for (value_t n = cdr(v); !nilp(n); n = cdr(n)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 227 | { |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 228 | printf(" "); |
| 229 | printval_ol(car(n)); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 230 | } |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 231 | |
| 232 | printf(")"); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 233 | } |
| 234 | else |
| 235 | { |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 236 | printf("("); |
| 237 | printval_ol(car(v)); |
| 238 | printf(" . "); |
| 239 | printval_ol(cdr(v)); |
| 240 | printf(")"); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 241 | } |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 242 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 243 | else if (nilp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 244 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 245 | printf("nil\n"); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 246 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 247 | else if (closurep(v)) |
| 248 | { |
| 249 | struct closure *c = (void *)(v ^ CLOSURE_TAG); |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 250 | printf("<closure %p (%d) %d>", |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 251 | c->function, c->args->num_required, c->num_captured); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 252 | } |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 253 | else |
| 254 | { |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 255 | printf("<unknown %d>", v); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
swissChili | e0d4b90 | 2022-07-30 17:32:01 -0700 | [diff] [blame] | 259 | void printval(value_t v, int depth) |
| 260 | { |
| 261 | UNUSED(depth); |
| 262 | printval_ol(v); |
| 263 | printf("\n"); |
| 264 | } |
| 265 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 266 | struct error readlist(struct istream *is, value_t *val) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 267 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 268 | E_INIT(); |
| 269 | NEARIS(is); |
| 270 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 271 | skipws(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 272 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 273 | if (is->peek(is) != '(') |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 274 | THROWSAFE(EEXPECTED); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 275 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 276 | is->get(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 277 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 278 | *val = readn(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 279 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 280 | skipws(is); |
| 281 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 282 | if (is->peek(is) != ')') |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 283 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 284 | NEARIS(is); |
| 285 | THROW(EEXPECTED, "Unterminated list"); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 286 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 287 | is->get(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 288 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 289 | OKAY(); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 290 | } |
| 291 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 292 | struct error readint(struct istream *is, value_t *val) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 293 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 294 | E_INIT(); |
| 295 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 296 | skipws(is); |
| 297 | |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 298 | int number = 0; |
| 299 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 300 | if (!isdigit(is->peek(is))) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 301 | THROWSAFE(EEXPECTED); |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 302 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 303 | while (isdigit(is->peek(is))) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 304 | { |
| 305 | number *= 10; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 306 | number += is->get(is) - '0'; |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 307 | } |
| 308 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 309 | *val = intval(number); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 310 | OKAY(); |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 311 | } |
| 312 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 313 | struct error readquote(struct istream *is, value_t *val) |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 314 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 315 | E_INIT(); |
| 316 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 317 | skipws(is); |
| 318 | |
| 319 | char c = is->peek(is); |
| 320 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 321 | if (c == '\'' || c == '`' || c == ',' || c == '#') |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 322 | { |
| 323 | is->get(is); |
| 324 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 325 | if (c == ',' && is->peek(is) == '@') |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 326 | { |
| 327 | // This is actually a splice |
| 328 | is->get(is); |
| 329 | c = '@'; |
| 330 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 331 | else if (c == '#' && is->peek(is) == '\'') |
| 332 | { |
| 333 | is->get(is); |
| 334 | } |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 335 | |
| 336 | // Read the next form and wrap it in the appropriate function |
| 337 | |
| 338 | value_t wrapped; |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 339 | NEARIS(is); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 340 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 341 | struct error read_error = read1(is, &wrapped); |
| 342 | TRY_ELSE(read_error, EEXPECTED, "Expected a form after reader macro char %c", c); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 343 | |
| 344 | value_t symbol = nil; |
| 345 | |
| 346 | switch (c) |
| 347 | { |
| 348 | case '\'': |
| 349 | symbol = symval("quote"); |
| 350 | break; |
| 351 | case '`': |
| 352 | symbol = symval("backquote"); |
| 353 | break; |
| 354 | case ',': |
| 355 | symbol = symval("unquote"); |
| 356 | break; |
| 357 | case '@': |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 358 | symbol = symval("unquote-splice"); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 359 | break; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 360 | case '#': |
| 361 | symbol = symval("function"); |
| 362 | break; |
| 363 | default: |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 364 | NEARIS(is); |
| 365 | THROW(EINVALID, "Invalid reader macro char %c", c); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | *val = cons(symbol, cons(wrapped, nil)); |
| 369 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 370 | OKAY(); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 371 | } |
| 372 | else |
| 373 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 374 | THROWSAFE(EEXPECTED); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 378 | struct error read1(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 379 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 380 | E_INIT(); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 381 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 382 | NEARIS(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 383 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 384 | OKAY_IF(readquote(is, val)); |
| 385 | OKAY_IF(readsym(is, val)); |
| 386 | OKAY_IF(readstr(is, val)); |
| 387 | OKAY_IF(readint(is, val)); |
| 388 | OKAY_IF(readlist(is, val)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 389 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 390 | THROWSAFE(EEXPECTED); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 391 | } |
| 392 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 393 | void set_cons_info(value_t cons, int line, char *name) |
| 394 | { |
| 395 | if (!consp(cons)) |
| 396 | return; |
| 397 | |
| 398 | struct cons *ca = (void *)(cons ^ CONS_TAG); |
| 399 | |
| 400 | ca->line = line; |
| 401 | ca->name = name; |
| 402 | } |
| 403 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 404 | value_t readn(struct istream *is) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 405 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 406 | value_t first = nil; |
| 407 | value_t *last = &first; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 408 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 409 | value_t read_val; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 410 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 411 | while (IS_OKAY(read1(is, &read_val))) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 412 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 413 | int line; |
| 414 | char *file; |
| 415 | |
| 416 | is->getpos(is, &line, &file); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 417 | *last = cons(read_val, nil); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 418 | set_cons_info(*last, line, file); |
| 419 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 420 | last = cdrref(*last); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | return first; |
| 424 | } |
| 425 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 426 | bool startswith(struct istream *s, char *pattern) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 427 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 428 | char *check = strdup(pattern); |
| 429 | s->read(s, check, strlen(pattern)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 430 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 431 | bool res = strcmp(check, pattern) == 0; |
| 432 | free(check); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 433 | |
| 434 | return res; |
| 435 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 436 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 437 | static value_t strptrval(char *str, int tag) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 438 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 439 | value_t v; |
| 440 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 441 | struct alloc *al = malloc_aligned(strlen(str) + 1 + sizeof(struct alloc)); |
| 442 | add_this_alloc(al, SYMBOL_TAG); |
| 443 | |
| 444 | char *a = (char *)(al + 1); |
| 445 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 446 | strcpy(a, str); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 447 | v = (value_t)a; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 448 | v |= tag; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 449 | |
| 450 | return v; |
| 451 | } |
| 452 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 453 | value_t strval(char *str) |
| 454 | { |
| 455 | return strptrval(str, STRING_TAG); |
| 456 | } |
| 457 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 458 | value_t symval(char *str) |
| 459 | { |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 460 | return strptrval(str, SYMBOL_TAG); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 461 | } |
| 462 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 463 | bool integerp(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 464 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 465 | return (v & INT_MASK) == INT_TAG; |
| 466 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 467 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 468 | bool symbolp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 469 | { |
| 470 | return (v & HEAP_MASK) == SYMBOL_TAG; |
| 471 | } |
| 472 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 473 | bool stringp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 474 | { |
| 475 | return (v & HEAP_MASK) == STRING_TAG; |
| 476 | } |
| 477 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 478 | bool consp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 479 | { |
| 480 | return (v & HEAP_MASK) == CONS_TAG; |
| 481 | } |
| 482 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 483 | bool heapp(value_t v) |
| 484 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 485 | return consp(v) || stringp(v) || symbolp(v) || closurep(v); |
| 486 | } |
| 487 | |
| 488 | bool closurep(value_t v) |
| 489 | { |
| 490 | return (v & HEAP_MASK) == CLOSURE_TAG; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 491 | } |
| 492 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 493 | bool listp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 494 | { |
| 495 | value_t next = v; |
| 496 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 497 | while (consp(next)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 498 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 499 | next = cdr(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 500 | } |
| 501 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 502 | return nilp(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 503 | } |
| 504 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 505 | value_t car(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 506 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 507 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 508 | return nil; |
| 509 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 510 | return *carref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 511 | } |
| 512 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 513 | value_t cdr(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 514 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 515 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 516 | return nil; |
| 517 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 518 | return *cdrref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 519 | } |
| 520 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 521 | value_t *carref(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 522 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 523 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 524 | return NULL; |
| 525 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 526 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 527 | return &c->car; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 528 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 529 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 530 | value_t *cdrref(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 531 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 532 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 533 | return NULL; |
| 534 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 535 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 536 | return &c->cdr; |
| 537 | } |
| 538 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 539 | bool nilp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 540 | { |
| 541 | return v == nil; |
| 542 | } |
| 543 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 544 | int length(value_t v) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 545 | { |
| 546 | int i = 0; |
| 547 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 548 | for (; !nilp(v); v = cdr(v)) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 549 | i++; |
| 550 | |
| 551 | return i; |
| 552 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 553 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 554 | value_t elt(value_t v, int index) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 555 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 556 | for (int i = 0; i < index; i++) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 557 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 558 | v = cdr(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 559 | } |
| 560 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 561 | return car(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 562 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 563 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 564 | bool symstreq(value_t sym, char *str) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 565 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 566 | if ((sym & HEAP_MASK) != SYMBOL_TAG) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 567 | return false; |
| 568 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 569 | return strcmp((char *)(sym ^ SYMBOL_TAG), str) == 0; |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 570 | } |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 571 | |
| 572 | unsigned char make_pool() |
| 573 | { |
| 574 | return ++max_pool; |
| 575 | } |
| 576 | |
| 577 | unsigned char push_pool(unsigned char pool) |
| 578 | { |
| 579 | unsigned char old = current_pool; |
| 580 | current_pool = pool; |
| 581 | return old; |
| 582 | } |
| 583 | |
| 584 | void pop_pool(unsigned char pool) |
| 585 | { |
| 586 | current_pool = pool; |
| 587 | } |
| 588 | |
| 589 | bool pool_alive(unsigned char pool) |
| 590 | { |
| 591 | return pool != 0; |
| 592 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 593 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 594 | void add_to_pool(value_t form) |
| 595 | { |
| 596 | if (!heapp(form)) |
| 597 | return; |
| 598 | |
| 599 | struct alloc *a = (void *)(form & ~0b111); |
| 600 | a[-1].pool = current_pool; |
| 601 | } |
| 602 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 603 | int cons_line(value_t val) |
| 604 | { |
| 605 | if (!consp(val)) |
| 606 | return 0; |
| 607 | |
| 608 | struct cons *c = (void *)(val ^ CONS_TAG); |
| 609 | |
| 610 | return c->line; |
| 611 | } |
| 612 | |
| 613 | char *cons_file(value_t val) |
| 614 | { |
| 615 | if (!consp(val)) |
| 616 | return NULL; |
| 617 | |
| 618 | struct cons *c = (void *)(val ^ CONS_TAG); |
| 619 | |
| 620 | return c->name; |
| 621 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 622 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 623 | value_t create_closure(void *code, struct args *args, int ncaptures) |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 624 | { |
| 625 | struct closure_alloc *ca = malloc_aligned(sizeof(struct closure_alloc) + |
| 626 | ncaptures * sizeof(value_t)); |
| 627 | |
| 628 | ca->closure.function = code; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 629 | ca->closure.args = args; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 630 | ca->closure.num_captured = ncaptures; |
| 631 | |
| 632 | add_this_alloc(&ca->alloc, CLOSURE_TAG); |
| 633 | |
| 634 | return (value_t)(&ca->closure) | CLOSURE_TAG; |
| 635 | } |
| 636 | |
| 637 | void set_closure_capture_variable(int index, value_t value, value_t closure) |
| 638 | { |
| 639 | if (!closurep(closure)) |
| 640 | return; |
| 641 | |
| 642 | struct closure *c = (void *)(closure ^ CLOSURE_TAG); |
| 643 | |
| 644 | c->data[index] = value; |
| 645 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 646 | |
| 647 | value_t cxdr(value_t v, int index) |
| 648 | { |
| 649 | if (!listp(v) || index >= length(v)) |
| 650 | return nil; |
| 651 | |
| 652 | for (int i = 0; i < index; i++) |
| 653 | { |
| 654 | v = cdr(v); |
| 655 | } |
| 656 | |
| 657 | return v; |
| 658 | } |
| 659 | |
| 660 | value_t *cxdrref(value_t *v, int index) |
| 661 | { |
| 662 | if (!listp(*v) || index >= length(*v)) |
| 663 | return NULL; |
| 664 | |
| 665 | value_t *p = v; |
| 666 | |
| 667 | for (int i = 0; i < index; i++) |
| 668 | { |
| 669 | p = cdrref(*p); |
| 670 | } |
| 671 | |
| 672 | return p; |
| 673 | } |
| 674 | |
| 675 | value_t deep_copy(value_t val) |
| 676 | { |
| 677 | if (integerp(val) || val == nil || val == t) |
| 678 | { |
| 679 | return val; |
| 680 | } |
| 681 | else if (symbolp(val)) |
| 682 | { |
| 683 | return symval((char *)(val ^ SYMBOL_TAG)); |
| 684 | } |
| 685 | else if (stringp(val)) |
| 686 | { |
| 687 | return strval((char *)(val ^ STRING_TAG)); |
| 688 | } |
| 689 | else if (consp(val)) |
| 690 | { |
| 691 | return cons(deep_copy(car(val)), deep_copy(cdr(val))); |
| 692 | } |
| 693 | else if (closurep(val)) |
| 694 | { |
| 695 | struct closure *c = (void *)(val ^ CLOSURE_TAG); |
| 696 | value_t new = create_closure(c->function, c->args, c->num_captured); |
| 697 | struct closure *new_c = (void *)(new ^ CLOSURE_TAG); |
| 698 | |
| 699 | for (int i = 0; i < c->num_captured; i++) |
| 700 | { |
| 701 | new_c->data[i] = deep_copy(c->data[i]); |
| 702 | } |
| 703 | |
| 704 | return new; |
| 705 | } |
| 706 | else |
| 707 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 708 | fprintf(stderr, "Don't know how to deep copy this, sorry... please report this bug :)"); |
| 709 | return nil; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 710 | } |
| 711 | } |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 712 | |
| 713 | value_t *nilptr(value_t val) |
| 714 | { |
| 715 | if (!listp(val)) |
| 716 | return NULL; |
| 717 | |
| 718 | if (nilp(val)) |
| 719 | return NULL; |
| 720 | |
| 721 | value_t *p; |
| 722 | |
| 723 | for (p = cdrref(val); !nilp(*p); p = cdrref(*p)) |
| 724 | { |
| 725 | } |
| 726 | |
| 727 | return p; |
| 728 | } |
| 729 | |
| 730 | value_t merge2(value_t front, value_t back) |
| 731 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 732 | if (!listp(front) && listp(back)) |
| 733 | return cons(front, back); |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 734 | |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 735 | if (listp(front) && !listp(back)) |
| 736 | back = cons(back, nil); |
| 737 | |
| 738 | *nilptr(front) = back; |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 739 | |
| 740 | return front; |
| 741 | } |