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 | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 30 | value_t cons(value_t car, value_t cdr) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 31 | { |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 32 | struct cons_alloc *item = malloc_aligned(sizeof(struct cons_alloc)); |
| 33 | struct cons *c = &item->cons; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 34 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 35 | c->car = car; |
| 36 | c->cdr = cdr; |
| 37 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 38 | item->alloc.type_tag = CONS_TAG; |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 39 | item->alloc.pool = current_pool; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 40 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 41 | if (last_a) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 42 | { |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 43 | item->alloc.prev = last_a; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 44 | last_a->next = item; |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 45 | item->alloc.next = NULL; |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 46 | last_a = item; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 47 | } |
| 48 | else |
| 49 | { |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 50 | item->alloc.prev = item->alloc.next = NULL; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 51 | first_a = last_a = item; |
| 52 | } |
| 53 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 54 | value_t v = (value_t)c; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 55 | v |= CONS_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 56 | |
| 57 | return v; |
| 58 | } |
| 59 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 60 | void skipws(struct istream *is) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 61 | { |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 62 | start: |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 63 | while (isspace(is->peek(is))) |
| 64 | is->get(is); |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 65 | |
| 66 | if (is->peek(is) == ';') |
| 67 | { |
| 68 | while (is->get(is) != '\n') |
| 69 | {} |
| 70 | |
| 71 | // Only time I ever use labels is for stuff like this. Compiler would |
| 72 | // probably optimize this if I used recursion but I don't want to |
| 73 | // bother. |
| 74 | goto start; |
| 75 | } |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 76 | } |
| 77 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 78 | bool isallowedchar(char c) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 79 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 80 | return (c >= '#' && c <= '\'') || (c >= '*' && c <= '/') || |
| 81 | (c >= '>' && c <= '@'); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 82 | } |
| 83 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 84 | bool issymstart(char c) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 85 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 86 | return isalpha(c) || isallowedchar(c); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 87 | } |
| 88 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 89 | bool issym(char c) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 90 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 91 | return isalpha(c) || isallowedchar(c) || isdigit(c); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 92 | } |
| 93 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 94 | bool readsym(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 95 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 96 | skipws(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 97 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 98 | if (!issymstart(is->peek(is))) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 99 | return false; |
| 100 | |
| 101 | int size = 8; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 102 | char *s = malloc_aligned(size); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 103 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 104 | s[0] = is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 105 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 106 | for (int i = 1;; i++) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 107 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 108 | if (issym(is->peek(is))) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 109 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 110 | if (i >= size) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 111 | { |
| 112 | size *= 2; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 113 | s = realloc_aligned(s, size); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 114 | } |
| 115 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 116 | s[i] = is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 117 | } |
| 118 | else |
| 119 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 120 | s[i] = 0; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 121 | *val = (value_t)s; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 122 | *val |= SYMBOL_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 123 | |
| 124 | return true; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 129 | bool readstr(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 130 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 131 | skipws(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 132 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 133 | if (is->peek(is) != '"') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 134 | return false; |
| 135 | |
| 136 | bool escape = false; |
| 137 | int size = 8; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 138 | char *s = malloc_aligned(size); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 139 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 140 | (void)is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 141 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 142 | for (int i = 0;; i++) |
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 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 146 | if (i >= size) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 147 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 148 | size *= 2; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 149 | s = realloc_aligned(s, size); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 150 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 151 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 152 | char c = is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 153 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 154 | if (escape && c == 'n') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 155 | c = '\n'; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 156 | else if (escape && c == '\\') |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 157 | c = '\\'; |
| 158 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 159 | if (c == '\\' && !escape) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 160 | { |
| 161 | escape = true; |
| 162 | i--; // will be incremented again, UGLY. |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | escape = false; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 167 | s[i] = c; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | else |
| 171 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 172 | is->get(is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 173 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 174 | *val = (value_t)s; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 175 | *val |= STRING_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 176 | |
| 177 | return true; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 182 | void printval(value_t v, int depth) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 183 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 184 | for (int i = 0; i < depth; i++) |
| 185 | printf(" "); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 186 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 187 | if (symbolp(v)) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 188 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 189 | printf("'%s\n", (char *)(v ^ SYMBOL_TAG)); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 190 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 191 | else if (stringp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 192 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 193 | printf("\"%s\"\n", (char *)(v ^ STRING_TAG)); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 194 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 195 | else if (integerp(v)) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 196 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 197 | printf("%d\n", v >> 2); |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 198 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 199 | else if (consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 200 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 201 | if (listp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 202 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 203 | printf("list:\n"); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 204 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 205 | for (value_t n = v; !nilp(n); n = cdr(n)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 206 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 207 | printval(car(n), depth + 1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | else |
| 211 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 212 | printf("cons:\n"); |
| 213 | printval(car(v), depth + 1); |
| 214 | printval(cdr(v), depth + 1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 215 | } |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 216 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 217 | else if (nilp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 218 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 219 | printf("nil\n"); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 220 | } |
| 221 | else |
| 222 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 223 | printf("<unknown %d>\n", v); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 227 | bool readlist(struct istream *is, value_t *val) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 228 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 229 | skipws(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 230 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 231 | if (is->peek(is) != '(') |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 232 | return false; |
| 233 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 234 | is->get(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 235 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 236 | *val = readn(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 237 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 238 | if (is->peek(is) != ')') |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 239 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 240 | is->showpos(is, stderr); |
| 241 | err("Unterminated list"); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 242 | return false; |
| 243 | } |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 244 | is->get(is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 249 | bool readint(struct istream *is, value_t *val) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 250 | { |
| 251 | int number = 0; |
| 252 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 253 | if (!isdigit(is->peek(is))) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 254 | return false; |
| 255 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 256 | while (isdigit(is->peek(is))) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 257 | { |
| 258 | number *= 10; |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 259 | number += is->get(is) - '0'; |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 260 | } |
| 261 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 262 | *val = intval(number); |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 266 | bool read1(struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 267 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 268 | if (readsym(is, val)) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 269 | return true; |
| 270 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 271 | if (readstr(is, val)) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 272 | return true; |
| 273 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 274 | if (readint(is, val)) |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 275 | return true; |
| 276 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 277 | if (readlist(is, val)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 278 | return true; |
| 279 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 283 | value_t readn(struct istream *is) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 284 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 285 | value_t first = nil; |
| 286 | value_t *last = &first; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 287 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 288 | value_t read_val; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 289 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 290 | while (read1(is, &read_val)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 291 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 292 | *last = cons(read_val, nil); |
| 293 | last = cdrref(*last); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | return first; |
| 297 | } |
| 298 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 299 | bool startswith(struct istream *s, char *pattern) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 300 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 301 | char *check = strdup(pattern); |
| 302 | s->read(s, check, strlen(pattern)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 303 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 304 | bool res = strcmp(check, pattern) == 0; |
| 305 | free(check); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 306 | |
| 307 | return res; |
| 308 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 309 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 310 | value_t strval(char *str) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 311 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 312 | value_t v; |
| 313 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 314 | char *a = malloc_aligned(strlen(str) + 1); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 315 | v = (value_t)a; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 316 | v |= STRING_TAG; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 317 | |
| 318 | return v; |
| 319 | } |
| 320 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 321 | bool integerp(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 322 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 323 | return (v & INT_MASK) == INT_TAG; |
| 324 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 325 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 326 | bool symbolp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 327 | { |
| 328 | return (v & HEAP_MASK) == SYMBOL_TAG; |
| 329 | } |
| 330 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 331 | bool stringp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 332 | { |
| 333 | return (v & HEAP_MASK) == STRING_TAG; |
| 334 | } |
| 335 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 336 | bool consp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 337 | { |
| 338 | return (v & HEAP_MASK) == CONS_TAG; |
| 339 | } |
| 340 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 341 | bool heapp(value_t v) |
| 342 | { |
| 343 | return consp(v) || stringp(v) || symbolp(v); |
| 344 | } |
| 345 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 346 | bool listp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 347 | { |
| 348 | value_t next = v; |
| 349 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 350 | while (consp(next)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 351 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 352 | next = cdr(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 353 | } |
| 354 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 355 | return nilp(next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 356 | } |
| 357 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 358 | value_t car(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 359 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 360 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 361 | return nil; |
| 362 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 363 | return *carref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 364 | } |
| 365 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 366 | value_t cdr(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 367 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 368 | if (!consp(v)) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 369 | return nil; |
| 370 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 371 | return *cdrref(v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 372 | } |
| 373 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 374 | value_t *carref(value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 375 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 376 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 377 | return NULL; |
| 378 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 379 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 380 | return &c->car; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 381 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 382 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 383 | value_t *cdrref(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 384 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 385 | if (!consp(v)) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 386 | return NULL; |
| 387 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 388 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 389 | return &c->cdr; |
| 390 | } |
| 391 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 392 | bool nilp(value_t v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 393 | { |
| 394 | return v == nil; |
| 395 | } |
| 396 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 397 | int length(value_t v) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 398 | { |
| 399 | int i = 0; |
| 400 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 401 | for (; !nilp(v); v = cdr(v)) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 402 | i++; |
| 403 | |
| 404 | return i; |
| 405 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 406 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 407 | value_t elt(value_t v, int index) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 408 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 409 | for (int i = 0; i < index; i++) |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 410 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 411 | v = cdr(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 412 | } |
| 413 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 414 | return car(v); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 415 | } |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 416 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 417 | bool symstreq(value_t sym, char *str) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 418 | { |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 419 | if ((sym & HEAP_MASK) != SYMBOL_TAG) |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 420 | return false; |
| 421 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 422 | return strcmp((char *)(sym ^ SYMBOL_TAG), str) == 0; |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 423 | } |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 424 | |
| 425 | unsigned char make_pool() |
| 426 | { |
| 427 | return ++max_pool; |
| 428 | } |
| 429 | |
| 430 | unsigned char push_pool(unsigned char pool) |
| 431 | { |
| 432 | unsigned char old = current_pool; |
| 433 | current_pool = pool; |
| 434 | return old; |
| 435 | } |
| 436 | |
| 437 | void pop_pool(unsigned char pool) |
| 438 | { |
| 439 | current_pool = pool; |
| 440 | } |
| 441 | |
| 442 | bool pool_alive(unsigned char pool) |
| 443 | { |
| 444 | return pool != 0; |
| 445 | } |