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 | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 202 | void printval(value_t v, int depth) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 203 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -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 | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 209 | printf("'%s\n", (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 | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 213 | printf("\"%s\"\n", (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 | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 217 | printf("%d\n", 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 | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 223 | printf("list:\n"); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 224 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 225 | for (value_t n = v; !nilp(n); n = cdr(n)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 226 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 227 | printval(car(n), depth + 1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | else |
| 231 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 232 | printf("cons:\n"); |
| 233 | printval(car(v), depth + 1); |
| 234 | printval(cdr(v), depth + 1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 235 | } |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 236 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 237 | else if (nilp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 238 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 239 | printf("nil\n"); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 240 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 241 | else if (closurep(v)) |
| 242 | { |
| 243 | struct closure *c = (void *)(v ^ CLOSURE_TAG); |
| 244 | printf("closure %p taking %d argument(s) and capturing %d value(s)\n", |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 245 | c->function, c->args->num_required, c->num_captured); |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 246 | } |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 247 | else |
| 248 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 249 | printf("<unknown %d>\n", v); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 253 | struct error readlist(struct istream *is, value_t *val) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 254 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 255 | E_INIT(); |
| 256 | NEARIS(is); |
| 257 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 258 | skipws(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 259 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 260 | if (is->peek(is) != '(') |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 261 | THROWSAFE(EEXPECTED); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 262 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 263 | is->get(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 264 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 265 | *val = readn(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 266 | |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 267 | skipws(is); |
| 268 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 269 | if (is->peek(is) != ')') |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 270 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 271 | NEARIS(is); |
| 272 | THROW(EEXPECTED, "Unterminated list"); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 273 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 274 | is->get(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 275 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 276 | OKAY(); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 277 | } |
| 278 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 279 | struct error readint(struct istream *is, value_t *val) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 280 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 281 | E_INIT(); |
| 282 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 283 | skipws(is); |
| 284 | |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 285 | int number = 0; |
| 286 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 287 | if (!isdigit(is->peek(is))) |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 288 | THROWSAFE(EEXPECTED); |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 289 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 290 | while (isdigit(is->peek(is))) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 291 | { |
| 292 | number *= 10; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 293 | number += is->get(is) - '0'; |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 294 | } |
| 295 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 296 | *val = intval(number); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 297 | OKAY(); |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 298 | } |
| 299 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 300 | struct error readquote(struct istream *is, value_t *val) |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 301 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 302 | E_INIT(); |
| 303 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 304 | skipws(is); |
| 305 | |
| 306 | char c = is->peek(is); |
| 307 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 308 | if (c == '\'' || c == '`' || c == ',' || c == '#') |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 309 | { |
| 310 | is->get(is); |
| 311 | |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 312 | if (c == ',' && is->peek(is) == '@') |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 313 | { |
| 314 | // This is actually a splice |
| 315 | is->get(is); |
| 316 | c = '@'; |
| 317 | } |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 318 | else if (c == '#' && is->peek(is) == '\'') |
| 319 | { |
| 320 | is->get(is); |
| 321 | } |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 322 | |
| 323 | // Read the next form and wrap it in the appropriate function |
| 324 | |
| 325 | value_t wrapped; |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 326 | NEARIS(is); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 327 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 328 | struct error read_error = read1(is, &wrapped); |
| 329 | 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] | 330 | |
| 331 | value_t symbol = nil; |
| 332 | |
| 333 | switch (c) |
| 334 | { |
| 335 | case '\'': |
| 336 | symbol = symval("quote"); |
| 337 | break; |
| 338 | case '`': |
| 339 | symbol = symval("backquote"); |
| 340 | break; |
| 341 | case ',': |
| 342 | symbol = symval("unquote"); |
| 343 | break; |
| 344 | case '@': |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 345 | symbol = symval("unquote-splice"); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 346 | break; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 347 | case '#': |
| 348 | symbol = symval("function"); |
| 349 | break; |
| 350 | default: |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 351 | NEARIS(is); |
| 352 | THROW(EINVALID, "Invalid reader macro char %c", c); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | *val = cons(symbol, cons(wrapped, nil)); |
| 356 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 357 | OKAY(); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 358 | } |
| 359 | else |
| 360 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 361 | THROWSAFE(EEXPECTED); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 365 | struct error read1(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 366 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 367 | E_INIT(); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 368 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 369 | NEARIS(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 370 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 371 | OKAY_IF(readquote(is, val)); |
| 372 | OKAY_IF(readsym(is, val)); |
| 373 | OKAY_IF(readstr(is, val)); |
| 374 | OKAY_IF(readint(is, val)); |
| 375 | OKAY_IF(readlist(is, val)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 376 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 377 | THROWSAFE(EEXPECTED); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 378 | } |
| 379 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 380 | void set_cons_info(value_t cons, int line, char *name) |
| 381 | { |
| 382 | if (!consp(cons)) |
| 383 | return; |
| 384 | |
| 385 | struct cons *ca = (void *)(cons ^ CONS_TAG); |
| 386 | |
| 387 | ca->line = line; |
| 388 | ca->name = name; |
| 389 | } |
| 390 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 391 | value_t readn(struct istream *is) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 392 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 393 | value_t first = nil; |
| 394 | value_t *last = &first; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 395 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 396 | value_t read_val; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 397 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 398 | while (IS_OKAY(read1(is, &read_val))) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 399 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 400 | int line; |
| 401 | char *file; |
| 402 | |
| 403 | is->getpos(is, &line, &file); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 404 | *last = cons(read_val, nil); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 405 | set_cons_info(*last, line, file); |
| 406 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 407 | last = cdrref(*last); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | return first; |
| 411 | } |
| 412 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 413 | bool startswith(struct istream *s, char *pattern) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 414 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 415 | char *check = strdup(pattern); |
| 416 | s->read(s, check, strlen(pattern)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 417 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 418 | bool res = strcmp(check, pattern) == 0; |
| 419 | free(check); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 420 | |
| 421 | return res; |
| 422 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 423 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 424 | static value_t strptrval(char *str, int tag) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 425 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 426 | value_t v; |
| 427 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 428 | struct alloc *al = malloc_aligned(strlen(str) + 1 + sizeof(struct alloc)); |
| 429 | add_this_alloc(al, SYMBOL_TAG); |
| 430 | |
| 431 | char *a = (char *)(al + 1); |
| 432 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 433 | strcpy(a, str); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 434 | v = (value_t)a; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 435 | v |= tag; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 436 | |
| 437 | return v; |
| 438 | } |
| 439 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 440 | value_t strval(char *str) |
| 441 | { |
| 442 | return strptrval(str, STRING_TAG); |
| 443 | } |
| 444 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 445 | value_t symval(char *str) |
| 446 | { |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 447 | return strptrval(str, SYMBOL_TAG); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 448 | } |
| 449 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 450 | bool integerp(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 451 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 452 | return (v & INT_MASK) == INT_TAG; |
| 453 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 454 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 455 | bool symbolp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 456 | { |
| 457 | return (v & HEAP_MASK) == SYMBOL_TAG; |
| 458 | } |
| 459 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 460 | bool stringp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 461 | { |
| 462 | return (v & HEAP_MASK) == STRING_TAG; |
| 463 | } |
| 464 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 465 | bool consp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 466 | { |
| 467 | return (v & HEAP_MASK) == CONS_TAG; |
| 468 | } |
| 469 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 470 | bool heapp(value_t v) |
| 471 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 472 | return consp(v) || stringp(v) || symbolp(v) || closurep(v); |
| 473 | } |
| 474 | |
| 475 | bool closurep(value_t v) |
| 476 | { |
| 477 | return (v & HEAP_MASK) == CLOSURE_TAG; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 478 | } |
| 479 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 480 | bool listp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 481 | { |
| 482 | value_t next = v; |
| 483 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 484 | while (consp(next)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 485 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 486 | next = cdr(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 487 | } |
| 488 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 489 | return nilp(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 490 | } |
| 491 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 492 | value_t car(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 493 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 494 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 495 | return nil; |
| 496 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 497 | return *carref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 498 | } |
| 499 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 500 | value_t cdr(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 501 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 502 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 503 | return nil; |
| 504 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 505 | return *cdrref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 506 | } |
| 507 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 508 | value_t *carref(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 509 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 510 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 511 | return NULL; |
| 512 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 513 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 514 | return &c->car; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 515 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 516 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 517 | value_t *cdrref(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 518 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 519 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 520 | return NULL; |
| 521 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 522 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 523 | return &c->cdr; |
| 524 | } |
| 525 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 526 | bool nilp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 527 | { |
| 528 | return v == nil; |
| 529 | } |
| 530 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 531 | int length(value_t v) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 532 | { |
| 533 | int i = 0; |
| 534 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 535 | for (; !nilp(v); v = cdr(v)) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 536 | i++; |
| 537 | |
| 538 | return i; |
| 539 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 540 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 541 | value_t elt(value_t v, int index) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 542 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 543 | for (int i = 0; i < index; i++) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 544 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 545 | v = cdr(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 546 | } |
| 547 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 548 | return car(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 549 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 550 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 551 | bool symstreq(value_t sym, char *str) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 552 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 553 | if ((sym & HEAP_MASK) != SYMBOL_TAG) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 554 | return false; |
| 555 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 556 | return strcmp((char *)(sym ^ SYMBOL_TAG), str) == 0; |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 557 | } |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 558 | |
| 559 | unsigned char make_pool() |
| 560 | { |
| 561 | return ++max_pool; |
| 562 | } |
| 563 | |
| 564 | unsigned char push_pool(unsigned char pool) |
| 565 | { |
| 566 | unsigned char old = current_pool; |
| 567 | current_pool = pool; |
| 568 | return old; |
| 569 | } |
| 570 | |
| 571 | void pop_pool(unsigned char pool) |
| 572 | { |
| 573 | current_pool = pool; |
| 574 | } |
| 575 | |
| 576 | bool pool_alive(unsigned char pool) |
| 577 | { |
| 578 | return pool != 0; |
| 579 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 580 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 581 | void add_to_pool(value_t form) |
| 582 | { |
| 583 | if (!heapp(form)) |
| 584 | return; |
| 585 | |
| 586 | struct alloc *a = (void *)(form & ~0b111); |
| 587 | a[-1].pool = current_pool; |
| 588 | } |
| 589 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 590 | int cons_line(value_t val) |
| 591 | { |
| 592 | if (!consp(val)) |
| 593 | return 0; |
| 594 | |
| 595 | struct cons *c = (void *)(val ^ CONS_TAG); |
| 596 | |
| 597 | return c->line; |
| 598 | } |
| 599 | |
| 600 | char *cons_file(value_t val) |
| 601 | { |
| 602 | if (!consp(val)) |
| 603 | return NULL; |
| 604 | |
| 605 | struct cons *c = (void *)(val ^ CONS_TAG); |
| 606 | |
| 607 | return c->name; |
| 608 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 609 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 610 | value_t create_closure(void *code, struct args *args, int ncaptures) |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 611 | { |
| 612 | struct closure_alloc *ca = malloc_aligned(sizeof(struct closure_alloc) + |
| 613 | ncaptures * sizeof(value_t)); |
| 614 | |
| 615 | ca->closure.function = code; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 616 | ca->closure.args = args; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 617 | ca->closure.num_captured = ncaptures; |
| 618 | |
| 619 | add_this_alloc(&ca->alloc, CLOSURE_TAG); |
| 620 | |
| 621 | return (value_t)(&ca->closure) | CLOSURE_TAG; |
| 622 | } |
| 623 | |
| 624 | void set_closure_capture_variable(int index, value_t value, value_t closure) |
| 625 | { |
| 626 | if (!closurep(closure)) |
| 627 | return; |
| 628 | |
| 629 | struct closure *c = (void *)(closure ^ CLOSURE_TAG); |
| 630 | |
| 631 | c->data[index] = value; |
| 632 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 633 | |
| 634 | value_t cxdr(value_t v, int index) |
| 635 | { |
| 636 | if (!listp(v) || index >= length(v)) |
| 637 | return nil; |
| 638 | |
| 639 | for (int i = 0; i < index; i++) |
| 640 | { |
| 641 | v = cdr(v); |
| 642 | } |
| 643 | |
| 644 | return v; |
| 645 | } |
| 646 | |
| 647 | value_t *cxdrref(value_t *v, int index) |
| 648 | { |
| 649 | if (!listp(*v) || index >= length(*v)) |
| 650 | return NULL; |
| 651 | |
| 652 | value_t *p = v; |
| 653 | |
| 654 | for (int i = 0; i < index; i++) |
| 655 | { |
| 656 | p = cdrref(*p); |
| 657 | } |
| 658 | |
| 659 | return p; |
| 660 | } |
| 661 | |
| 662 | value_t deep_copy(value_t val) |
| 663 | { |
| 664 | if (integerp(val) || val == nil || val == t) |
| 665 | { |
| 666 | return val; |
| 667 | } |
| 668 | else if (symbolp(val)) |
| 669 | { |
| 670 | return symval((char *)(val ^ SYMBOL_TAG)); |
| 671 | } |
| 672 | else if (stringp(val)) |
| 673 | { |
| 674 | return strval((char *)(val ^ STRING_TAG)); |
| 675 | } |
| 676 | else if (consp(val)) |
| 677 | { |
| 678 | return cons(deep_copy(car(val)), deep_copy(cdr(val))); |
| 679 | } |
| 680 | else if (closurep(val)) |
| 681 | { |
| 682 | struct closure *c = (void *)(val ^ CLOSURE_TAG); |
| 683 | value_t new = create_closure(c->function, c->args, c->num_captured); |
| 684 | struct closure *new_c = (void *)(new ^ CLOSURE_TAG); |
| 685 | |
| 686 | for (int i = 0; i < c->num_captured; i++) |
| 687 | { |
| 688 | new_c->data[i] = deep_copy(c->data[i]); |
| 689 | } |
| 690 | |
| 691 | return new; |
| 692 | } |
| 693 | else |
| 694 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 695 | fprintf(stderr, "Don't know how to deep copy this, sorry... please report this bug :)"); |
| 696 | return nil; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 697 | } |
| 698 | } |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 699 | |
| 700 | value_t *nilptr(value_t val) |
| 701 | { |
| 702 | if (!listp(val)) |
| 703 | return NULL; |
| 704 | |
| 705 | if (nilp(val)) |
| 706 | return NULL; |
| 707 | |
| 708 | value_t *p; |
| 709 | |
| 710 | for (p = cdrref(val); !nilp(*p); p = cdrref(*p)) |
| 711 | { |
| 712 | } |
| 713 | |
| 714 | return p; |
| 715 | } |
| 716 | |
| 717 | value_t merge2(value_t front, value_t back) |
| 718 | { |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 719 | if (!listp(front) && listp(back)) |
| 720 | return cons(front, back); |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 721 | |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 722 | if (listp(front) && !listp(back)) |
| 723 | back = cons(back, nil); |
| 724 | |
| 725 | *nilptr(front) = back; |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 726 | |
| 727 | return front; |
| 728 | } |