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