swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 1 | #include "lisp.h" |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 2 | #include "plat/plat.h" |
| 3 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 4 | #include <ctype.h> |
| 5 | #include <stdbool.h> |
| 6 | #include <stdio.h> |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 9 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 10 | struct alloc *first_a = NULL, *last_a = NULL; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 11 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 12 | value_t nil = 0b00101111; // magic ;) |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 13 | value_t t = 1 << 3; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 14 | |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 15 | unsigned char max_pool = 0, current_pool = 0; |
| 16 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 17 | void err(const char *msg) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 18 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 19 | fprintf(stderr, "ERROR: %s\n", msg); |
| 20 | exit(1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 21 | } |
| 22 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 23 | value_t intval(int i) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 24 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 25 | i <<= 2; |
| 26 | i |= INT_TAG; |
| 27 | return i; |
| 28 | } |
| 29 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 30 | void add_this_alloc(struct alloc *a, int tag) |
| 31 | { |
| 32 | a->type_tag = tag; |
| 33 | a->pool = current_pool; |
| 34 | |
| 35 | if (last_a) |
| 36 | { |
| 37 | a->prev = last_a; |
| 38 | last_a->next = a; |
| 39 | a->next = NULL; |
| 40 | last_a = a; |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | a->prev = a->next = NULL; |
| 45 | first_a = last_a = a; |
| 46 | } |
| 47 | } |
| 48 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 49 | value_t cons(value_t car, value_t cdr) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 50 | { |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 51 | struct cons_alloc *item = malloc_aligned(sizeof(struct cons_alloc)); |
| 52 | struct cons *c = &item->cons; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 53 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 54 | c->car = car; |
| 55 | c->cdr = cdr; |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 56 | c->line = 0; |
| 57 | c->name = NULL; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 58 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 59 | value_t v = (value_t)c; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 60 | v |= CONS_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 61 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 62 | add_this_alloc(&item->alloc, CONS_TAG); |
| 63 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 64 | return v; |
| 65 | } |
| 66 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 67 | void skipws(struct istream *is) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 68 | { |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 69 | start: |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 70 | while (isspace(is->peek(is))) |
| 71 | is->get(is); |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 72 | |
| 73 | if (is->peek(is) == ';') |
| 74 | { |
| 75 | while (is->get(is) != '\n') |
| 76 | {} |
| 77 | |
| 78 | // Only time I ever use labels is for stuff like this. Compiler would |
| 79 | // probably optimize this if I used recursion but I don't want to |
| 80 | // bother. |
| 81 | goto start; |
| 82 | } |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 83 | } |
| 84 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 85 | bool isallowedchar(char c) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 86 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 87 | return (c >= '#' && c <= '\'') || (c >= '*' && c <= '/') || |
| 88 | (c >= '>' && c <= '@'); |
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 issymstart(char c) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 92 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 93 | return isalpha(c) || isallowedchar(c); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 94 | } |
| 95 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 96 | bool issym(char c) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 97 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 98 | return isalpha(c) || isallowedchar(c) || isdigit(c); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 99 | } |
| 100 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 101 | bool readsym(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 102 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 103 | skipws(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 104 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 105 | if (!issymstart(is->peek(is))) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 106 | return false; |
| 107 | |
| 108 | int size = 8; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 109 | struct alloc *a = malloc_aligned(size + sizeof(struct alloc)); |
| 110 | add_this_alloc(a, SYMBOL_TAG); |
| 111 | |
| 112 | char *s = (char *)(a + 1); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 113 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 114 | s[0] = is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 115 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 116 | for (int i = 1;; i++) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 117 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 118 | if (issym(is->peek(is))) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 119 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 120 | if (i >= size) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 121 | { |
| 122 | size *= 2; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 123 | a = realloc_aligned(a, size + sizeof(struct alloc)); |
| 124 | s = (char *)(a + 1); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 125 | } |
| 126 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 127 | s[i] = is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 128 | } |
| 129 | else |
| 130 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 131 | s[i] = 0; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 132 | *val = (value_t)s; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 133 | *val |= SYMBOL_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 134 | |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 140 | bool readstr(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 141 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 142 | skipws(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 143 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 144 | if (is->peek(is) != '"') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 145 | return false; |
| 146 | |
| 147 | bool escape = false; |
| 148 | int size = 8; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 149 | |
| 150 | struct alloc *a = malloc_aligned(size + sizeof(struct alloc)); |
| 151 | add_this_alloc(a, STRING_TAG); |
| 152 | |
| 153 | char *s = (char *)(a + 1); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 154 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 155 | (void)is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 156 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 157 | for (int i = 0;; i++) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 158 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 159 | if (is->peek(is) != '"') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 160 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 161 | if (i >= size) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 162 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 163 | size *= 2; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 164 | a = realloc_aligned(a, size + sizeof(struct alloc)); |
| 165 | s = (char *)(a + 1); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 166 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 167 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 168 | char c = is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 169 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 170 | if (escape && c == 'n') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 171 | c = '\n'; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 172 | else if (escape && c == '\\') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 173 | c = '\\'; |
| 174 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 175 | if (c == '\\' && !escape) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 176 | { |
| 177 | escape = true; |
| 178 | i--; // will be incremented again, UGLY. |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | escape = false; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 183 | s[i] = c; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | else |
| 187 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 188 | is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 189 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 190 | *val = (value_t)s; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 191 | *val |= STRING_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 192 | |
| 193 | return true; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 198 | void printval(value_t v, int depth) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 199 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 200 | for (int i = 0; i < depth; i++) |
| 201 | printf(" "); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 202 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 203 | if (symbolp(v)) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 204 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 205 | printf("'%s\n", (char *)(v ^ SYMBOL_TAG)); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 206 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 207 | else if (stringp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 208 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 209 | printf("\"%s\"\n", (char *)(v ^ STRING_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 (integerp(v)) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 212 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 213 | printf("%d\n", v >> 2); |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 214 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 215 | else if (consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 216 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 217 | if (listp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 218 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 219 | printf("list:\n"); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 220 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 221 | for (value_t n = v; !nilp(n); n = cdr(n)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 222 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 223 | printval(car(n), depth + 1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | else |
| 227 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 228 | printf("cons:\n"); |
| 229 | printval(car(v), depth + 1); |
| 230 | printval(cdr(v), depth + 1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 231 | } |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 232 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 233 | else if (nilp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 234 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 235 | printf("nil\n"); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 236 | } |
| 237 | else |
| 238 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 239 | printf("<unknown %d>\n", v); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 243 | bool readlist(struct istream *is, value_t *val) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 244 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 245 | skipws(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 246 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 247 | if (is->peek(is) != '(') |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 248 | return false; |
| 249 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 250 | is->get(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 251 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 252 | *val = readn(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 253 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 254 | if (is->peek(is) != ')') |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 255 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 256 | is->showpos(is, stderr); |
| 257 | err("Unterminated list"); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 258 | return false; |
| 259 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 260 | is->get(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 261 | |
| 262 | return true; |
| 263 | } |
| 264 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 265 | bool readint(struct istream *is, value_t *val) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 266 | { |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 267 | skipws(is); |
| 268 | |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 269 | int number = 0; |
| 270 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 271 | if (!isdigit(is->peek(is))) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 272 | return false; |
| 273 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 274 | while (isdigit(is->peek(is))) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 275 | { |
| 276 | number *= 10; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 277 | number += is->get(is) - '0'; |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 278 | } |
| 279 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 280 | *val = intval(number); |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 284 | bool readquote(struct istream *is, value_t *val) |
| 285 | { |
| 286 | skipws(is); |
| 287 | |
| 288 | char c = is->peek(is); |
| 289 | |
| 290 | if (c == '\'' || c == '`' || c == ',') |
| 291 | { |
| 292 | is->get(is); |
| 293 | |
| 294 | if (c == '`' && is->peek(is) == '@') |
| 295 | { |
| 296 | // This is actually a splice |
| 297 | is->get(is); |
| 298 | c = '@'; |
| 299 | } |
| 300 | |
| 301 | // Read the next form and wrap it in the appropriate function |
| 302 | |
| 303 | value_t wrapped; |
| 304 | bool has_next = read1(is, &wrapped); |
| 305 | |
| 306 | if (!has_next) |
| 307 | { |
| 308 | fprintf(stderr, "Expected a form after reader macro char %c\n", c); |
| 309 | is->showpos(is, stderr); |
| 310 | err("Invalid reader macro"); |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | value_t symbol = nil; |
| 315 | |
| 316 | switch (c) |
| 317 | { |
| 318 | case '\'': |
| 319 | symbol = symval("quote"); |
| 320 | break; |
| 321 | case '`': |
| 322 | symbol = symval("backquote"); |
| 323 | break; |
| 324 | case ',': |
| 325 | symbol = symval("unquote"); |
| 326 | break; |
| 327 | case '@': |
swissChili | 6b47b6d | 2021-06-30 22:08:55 -0700 | [diff] [blame] | 328 | symbol = symval("unquote-splice"); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | |
| 332 | *val = cons(symbol, cons(wrapped, nil)); |
| 333 | |
| 334 | return true; |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | return false; |
| 339 | } |
| 340 | } |
| 341 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 342 | bool read1(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 343 | { |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 344 | // This could all be one big short-circuiting || but that is ugly. |
| 345 | if (readquote(is, val)) |
| 346 | return true; |
| 347 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 348 | if (readsym(is, val)) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 349 | return true; |
| 350 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 351 | if (readstr(is, val)) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 352 | return true; |
| 353 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 354 | if (readint(is, val)) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 355 | return true; |
| 356 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 357 | if (readlist(is, val)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 358 | return true; |
| 359 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 363 | void set_cons_info(value_t cons, int line, char *name) |
| 364 | { |
| 365 | if (!consp(cons)) |
| 366 | return; |
| 367 | |
| 368 | struct cons *ca = (void *)(cons ^ CONS_TAG); |
| 369 | |
| 370 | ca->line = line; |
| 371 | ca->name = name; |
| 372 | } |
| 373 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 374 | value_t readn(struct istream *is) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 375 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 376 | value_t first = nil; |
| 377 | value_t *last = &first; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 378 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 379 | value_t read_val; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 380 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 381 | while (read1(is, &read_val)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 382 | { |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 383 | int line; |
| 384 | char *file; |
| 385 | |
| 386 | is->getpos(is, &line, &file); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 387 | *last = cons(read_val, nil); |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 388 | set_cons_info(*last, line, file); |
| 389 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 390 | last = cdrref(*last); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return first; |
| 394 | } |
| 395 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 396 | bool startswith(struct istream *s, char *pattern) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 397 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 398 | char *check = strdup(pattern); |
| 399 | s->read(s, check, strlen(pattern)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 400 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 401 | bool res = strcmp(check, pattern) == 0; |
| 402 | free(check); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 403 | |
| 404 | return res; |
| 405 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 406 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 407 | static value_t strptrval(char *str, int tag) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 408 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 409 | value_t v; |
| 410 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 411 | struct alloc *al = malloc_aligned(strlen(str) + 1 + sizeof(struct alloc)); |
| 412 | add_this_alloc(al, SYMBOL_TAG); |
| 413 | |
| 414 | char *a = (char *)(al + 1); |
| 415 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 416 | strcpy(a, str); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 417 | v = (value_t)a; |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 418 | v |= tag; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 419 | |
| 420 | return v; |
| 421 | } |
| 422 | |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 423 | value_t strval(char *str) |
| 424 | { |
| 425 | return strptrval(str, STRING_TAG); |
| 426 | } |
| 427 | |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 428 | value_t symval(char *str) |
| 429 | { |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 430 | return strptrval(str, SYMBOL_TAG); |
swissChili | b6c858c | 2021-06-30 21:12:43 -0700 | [diff] [blame] | 431 | } |
| 432 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 433 | bool integerp(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 434 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 435 | return (v & INT_MASK) == INT_TAG; |
| 436 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 437 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 438 | bool symbolp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 439 | { |
| 440 | return (v & HEAP_MASK) == SYMBOL_TAG; |
| 441 | } |
| 442 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 443 | bool stringp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 444 | { |
| 445 | return (v & HEAP_MASK) == STRING_TAG; |
| 446 | } |
| 447 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 448 | bool consp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 449 | { |
| 450 | return (v & HEAP_MASK) == CONS_TAG; |
| 451 | } |
| 452 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 453 | bool heapp(value_t v) |
| 454 | { |
| 455 | return consp(v) || stringp(v) || symbolp(v); |
| 456 | } |
| 457 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 458 | bool listp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 459 | { |
| 460 | value_t next = v; |
| 461 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 462 | while (consp(next)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 463 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 464 | next = cdr(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 465 | } |
| 466 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 467 | return nilp(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 468 | } |
| 469 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 470 | value_t car(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 471 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 472 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 473 | return nil; |
| 474 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 475 | return *carref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 476 | } |
| 477 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 478 | value_t cdr(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 479 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 480 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 481 | return nil; |
| 482 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 483 | return *cdrref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 484 | } |
| 485 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 486 | value_t *carref(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 487 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 488 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 489 | return NULL; |
| 490 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 491 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 492 | return &c->car; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 493 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 494 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 495 | value_t *cdrref(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 496 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 497 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 498 | return NULL; |
| 499 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 500 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 501 | return &c->cdr; |
| 502 | } |
| 503 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 504 | bool nilp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 505 | { |
| 506 | return v == nil; |
| 507 | } |
| 508 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 509 | int length(value_t v) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 510 | { |
| 511 | int i = 0; |
| 512 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 513 | for (; !nilp(v); v = cdr(v)) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 514 | i++; |
| 515 | |
| 516 | return i; |
| 517 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 518 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 519 | value_t elt(value_t v, int index) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 520 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 521 | for (int i = 0; i < index; i++) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 522 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 523 | v = cdr(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 524 | } |
| 525 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 526 | return car(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 527 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 528 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 529 | bool symstreq(value_t sym, char *str) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 530 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 531 | if ((sym & HEAP_MASK) != SYMBOL_TAG) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 532 | return false; |
| 533 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 534 | return strcmp((char *)(sym ^ SYMBOL_TAG), str) == 0; |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 535 | } |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 536 | |
| 537 | unsigned char make_pool() |
| 538 | { |
| 539 | return ++max_pool; |
| 540 | } |
| 541 | |
| 542 | unsigned char push_pool(unsigned char pool) |
| 543 | { |
| 544 | unsigned char old = current_pool; |
| 545 | current_pool = pool; |
| 546 | return old; |
| 547 | } |
| 548 | |
| 549 | void pop_pool(unsigned char pool) |
| 550 | { |
| 551 | current_pool = pool; |
| 552 | } |
| 553 | |
| 554 | bool pool_alive(unsigned char pool) |
| 555 | { |
| 556 | return pool != 0; |
| 557 | } |
swissChili | f1ba8c1 | 2021-07-02 18:45:38 -0700 | [diff] [blame^] | 558 | |
| 559 | int cons_line(value_t val) |
| 560 | { |
| 561 | if (!consp(val)) |
| 562 | return 0; |
| 563 | |
| 564 | struct cons *c = (void *)(val ^ CONS_TAG); |
| 565 | |
| 566 | return c->line; |
| 567 | } |
| 568 | |
| 569 | char *cons_file(value_t val) |
| 570 | { |
| 571 | if (!consp(val)) |
| 572 | return NULL; |
| 573 | |
| 574 | struct cons *c = (void *)(val ^ CONS_TAG); |
| 575 | |
| 576 | return c->name; |
| 577 | } |