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 | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 328 | TRY_ELSE(read1(is, &wrapped), EEXPECTED, "Expected a form after reader macro char %c", c); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 329 | |
| 330 | value_t symbol = nil; |
| 331 | |
| 332 | switch (c) |
| 333 | { |
| 334 | case '\'': |
| 335 | symbol = symval("quote"); |
| 336 | break; |
| 337 | case '`': |
| 338 | symbol = symval("backquote"); |
| 339 | break; |
| 340 | case ',': |
| 341 | symbol = symval("unquote"); |
| 342 | break; |
| 343 | case '@': |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 344 | symbol = symval("unquote-splice"); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 345 | break; |
swissChili | 7434842 | 2021-07-04 13:23:24 -0700 | [diff] [blame] | 346 | case '#': |
| 347 | symbol = symval("function"); |
| 348 | break; |
| 349 | default: |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 350 | NEARIS(is); |
| 351 | THROW(EINVALID, "Invalid reader macro char %c", c); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | *val = cons(symbol, cons(wrapped, nil)); |
| 355 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 356 | OKAY(); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 357 | } |
| 358 | else |
| 359 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 360 | THROWSAFE(EEXPECTED); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 364 | struct error read1(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 365 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 366 | E_INIT(); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 367 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 368 | NEARIS(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 369 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 370 | OKAY_IF(readquote(is, val)); |
| 371 | OKAY_IF(readsym(is, val)); |
| 372 | OKAY_IF(readstr(is, val)); |
| 373 | OKAY_IF(readint(is, val)); |
| 374 | OKAY_IF(readlist(is, val)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 375 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 376 | THROWSAFE(EEXPECTED); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 377 | } |
| 378 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 379 | void set_cons_info(value_t cons, int line, char *name) |
| 380 | { |
| 381 | if (!consp(cons)) |
| 382 | return; |
| 383 | |
| 384 | struct cons *ca = (void *)(cons ^ CONS_TAG); |
| 385 | |
| 386 | ca->line = line; |
| 387 | ca->name = name; |
| 388 | } |
| 389 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 390 | value_t readn(struct istream *is) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 391 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 392 | value_t first = nil; |
| 393 | value_t *last = &first; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 394 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 395 | value_t read_val; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 396 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 397 | while (IS_OKAY(read1(is, &read_val))) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 398 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 399 | int line; |
| 400 | char *file; |
| 401 | |
| 402 | is->getpos(is, &line, &file); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 403 | *last = cons(read_val, nil); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 404 | set_cons_info(*last, line, file); |
| 405 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 406 | last = cdrref(*last); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return first; |
| 410 | } |
| 411 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 412 | bool startswith(struct istream *s, char *pattern) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 413 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 414 | char *check = strdup(pattern); |
| 415 | s->read(s, check, strlen(pattern)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 416 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 417 | bool res = strcmp(check, pattern) == 0; |
| 418 | free(check); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 419 | |
| 420 | return res; |
| 421 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 422 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 423 | static value_t strptrval(char *str, int tag) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 424 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 425 | value_t v; |
| 426 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 427 | struct alloc *al = malloc_aligned(strlen(str) + 1 + sizeof(struct alloc)); |
| 428 | add_this_alloc(al, SYMBOL_TAG); |
| 429 | |
| 430 | char *a = (char *)(al + 1); |
| 431 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 432 | strcpy(a, str); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 433 | v = (value_t)a; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 434 | v |= tag; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 435 | |
| 436 | return v; |
| 437 | } |
| 438 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 439 | value_t strval(char *str) |
| 440 | { |
| 441 | return strptrval(str, STRING_TAG); |
| 442 | } |
| 443 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 444 | value_t symval(char *str) |
| 445 | { |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 446 | return strptrval(str, SYMBOL_TAG); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 447 | } |
| 448 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 449 | bool integerp(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 450 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 451 | return (v & INT_MASK) == INT_TAG; |
| 452 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 453 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 454 | bool symbolp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 455 | { |
| 456 | return (v & HEAP_MASK) == SYMBOL_TAG; |
| 457 | } |
| 458 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 459 | bool stringp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 460 | { |
| 461 | return (v & HEAP_MASK) == STRING_TAG; |
| 462 | } |
| 463 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 464 | bool consp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 465 | { |
| 466 | return (v & HEAP_MASK) == CONS_TAG; |
| 467 | } |
| 468 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 469 | bool heapp(value_t v) |
| 470 | { |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 471 | return consp(v) || stringp(v) || symbolp(v) || closurep(v); |
| 472 | } |
| 473 | |
| 474 | bool closurep(value_t v) |
| 475 | { |
| 476 | return (v & HEAP_MASK) == CLOSURE_TAG; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 477 | } |
| 478 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 479 | bool listp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 480 | { |
| 481 | value_t next = v; |
| 482 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 483 | while (consp(next)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 484 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 485 | next = cdr(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 486 | } |
| 487 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 488 | return nilp(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 489 | } |
| 490 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 491 | value_t car(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 492 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 493 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 494 | return nil; |
| 495 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 496 | return *carref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 497 | } |
| 498 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 499 | value_t cdr(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 500 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 501 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 502 | return nil; |
| 503 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 504 | return *cdrref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 505 | } |
| 506 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 507 | value_t *carref(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 508 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 509 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 510 | return NULL; |
| 511 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 512 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 513 | return &c->car; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 514 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 515 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 516 | value_t *cdrref(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 517 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 518 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 519 | return NULL; |
| 520 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 521 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 522 | return &c->cdr; |
| 523 | } |
| 524 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 525 | bool nilp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 526 | { |
| 527 | return v == nil; |
| 528 | } |
| 529 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 530 | int length(value_t v) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 531 | { |
| 532 | int i = 0; |
| 533 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 534 | for (; !nilp(v); v = cdr(v)) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 535 | i++; |
| 536 | |
| 537 | return i; |
| 538 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 539 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 540 | value_t elt(value_t v, int index) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 541 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 542 | for (int i = 0; i < index; i++) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 543 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 544 | v = cdr(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 545 | } |
| 546 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 547 | return car(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 548 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 549 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 550 | bool symstreq(value_t sym, char *str) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 551 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 552 | if ((sym & HEAP_MASK) != SYMBOL_TAG) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 553 | return false; |
| 554 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 555 | return strcmp((char *)(sym ^ SYMBOL_TAG), str) == 0; |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 556 | } |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 557 | |
| 558 | unsigned char make_pool() |
| 559 | { |
| 560 | return ++max_pool; |
| 561 | } |
| 562 | |
| 563 | unsigned char push_pool(unsigned char pool) |
| 564 | { |
| 565 | unsigned char old = current_pool; |
| 566 | current_pool = pool; |
| 567 | return old; |
| 568 | } |
| 569 | |
| 570 | void pop_pool(unsigned char pool) |
| 571 | { |
| 572 | current_pool = pool; |
| 573 | } |
| 574 | |
| 575 | bool pool_alive(unsigned char pool) |
| 576 | { |
| 577 | return pool != 0; |
| 578 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame] | 579 | |
| 580 | int cons_line(value_t val) |
| 581 | { |
| 582 | if (!consp(val)) |
| 583 | return 0; |
| 584 | |
| 585 | struct cons *c = (void *)(val ^ CONS_TAG); |
| 586 | |
| 587 | return c->line; |
| 588 | } |
| 589 | |
| 590 | char *cons_file(value_t val) |
| 591 | { |
| 592 | if (!consp(val)) |
| 593 | return NULL; |
| 594 | |
| 595 | struct cons *c = (void *)(val ^ CONS_TAG); |
| 596 | |
| 597 | return c->name; |
| 598 | } |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 599 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 600 | value_t create_closure(void *code, struct args *args, int ncaptures) |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 601 | { |
| 602 | struct closure_alloc *ca = malloc_aligned(sizeof(struct closure_alloc) + |
| 603 | ncaptures * sizeof(value_t)); |
| 604 | |
| 605 | ca->closure.function = code; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 606 | ca->closure.args = args; |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 607 | ca->closure.num_captured = ncaptures; |
| 608 | |
| 609 | add_this_alloc(&ca->alloc, CLOSURE_TAG); |
| 610 | |
| 611 | return (value_t)(&ca->closure) | CLOSURE_TAG; |
| 612 | } |
| 613 | |
| 614 | void set_closure_capture_variable(int index, value_t value, value_t closure) |
| 615 | { |
| 616 | if (!closurep(closure)) |
| 617 | return; |
| 618 | |
| 619 | struct closure *c = (void *)(closure ^ CLOSURE_TAG); |
| 620 | |
| 621 | c->data[index] = value; |
| 622 | } |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 623 | |
| 624 | value_t cxdr(value_t v, int index) |
| 625 | { |
| 626 | if (!listp(v) || index >= length(v)) |
| 627 | return nil; |
| 628 | |
| 629 | for (int i = 0; i < index; i++) |
| 630 | { |
| 631 | v = cdr(v); |
| 632 | } |
| 633 | |
| 634 | return v; |
| 635 | } |
| 636 | |
| 637 | value_t *cxdrref(value_t *v, int index) |
| 638 | { |
| 639 | if (!listp(*v) || index >= length(*v)) |
| 640 | return NULL; |
| 641 | |
| 642 | value_t *p = v; |
| 643 | |
| 644 | for (int i = 0; i < index; i++) |
| 645 | { |
| 646 | p = cdrref(*p); |
| 647 | } |
| 648 | |
| 649 | return p; |
| 650 | } |
| 651 | |
| 652 | value_t deep_copy(value_t val) |
| 653 | { |
| 654 | if (integerp(val) || val == nil || val == t) |
| 655 | { |
| 656 | return val; |
| 657 | } |
| 658 | else if (symbolp(val)) |
| 659 | { |
| 660 | return symval((char *)(val ^ SYMBOL_TAG)); |
| 661 | } |
| 662 | else if (stringp(val)) |
| 663 | { |
| 664 | return strval((char *)(val ^ STRING_TAG)); |
| 665 | } |
| 666 | else if (consp(val)) |
| 667 | { |
| 668 | return cons(deep_copy(car(val)), deep_copy(cdr(val))); |
| 669 | } |
| 670 | else if (closurep(val)) |
| 671 | { |
| 672 | struct closure *c = (void *)(val ^ CLOSURE_TAG); |
| 673 | value_t new = create_closure(c->function, c->args, c->num_captured); |
| 674 | struct closure *new_c = (void *)(new ^ CLOSURE_TAG); |
| 675 | |
| 676 | for (int i = 0; i < c->num_captured; i++) |
| 677 | { |
| 678 | new_c->data[i] = deep_copy(c->data[i]); |
| 679 | } |
| 680 | |
| 681 | return new; |
| 682 | } |
| 683 | else |
| 684 | { |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 685 | fprintf(stderr, "Don't know how to deep copy this, sorry... please report this bug :)"); |
| 686 | return nil; |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 687 | } |
| 688 | } |