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