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 | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 10 | #define MIN(a, b) (a) > (b) ? (b) : (a) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 11 | |
| 12 | struct alloc_list *first_a = NULL, *last_a = NULL; |
| 13 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 14 | value_t nil = 0b00101111; // magic ;) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 15 | |
| 16 | void err (const char *msg) |
| 17 | { |
| 18 | fprintf (stderr, "ERROR: %s\n", msg); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 19 | exit (1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 20 | } |
| 21 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 22 | value_t intval (int i) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 23 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 24 | i <<= 2; |
| 25 | i |= INT_TAG; |
| 26 | return i; |
| 27 | } |
| 28 | |
| 29 | value_t cons (value_t car, value_t cdr) |
| 30 | { |
| 31 | struct cons *c = malloc_aligned (sizeof (struct cons)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 32 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 33 | c->car = car; |
| 34 | c->cdr = cdr; |
| 35 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 36 | struct alloc_list *item = malloc (sizeof (struct alloc_list)); |
| 37 | item->type = T_CONS; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 38 | item->cons_val = c; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 39 | |
| 40 | if ( last_a ) |
| 41 | { |
| 42 | item->prev = last_a; |
| 43 | last_a->next = item; |
| 44 | item->next = NULL; |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | item->prev = item->next = NULL; |
| 49 | first_a = last_a = item; |
| 50 | } |
| 51 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 52 | value_t v = (value_t)c; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 53 | v |= CONS_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 54 | |
| 55 | return v; |
| 56 | } |
| 57 | |
| 58 | void skipws (struct istream *is) |
| 59 | { |
| 60 | while ( isspace (is->peek (is)) ) |
| 61 | is->get (is); |
| 62 | } |
| 63 | |
| 64 | bool isallowedchar (char c) |
| 65 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 66 | return (c >= '#' && c <= '\'') || (c >= '*' && c <= '/') || |
| 67 | (c >= '>' && c <= '@'); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | bool issymstart (char c) |
| 71 | { |
| 72 | return isalpha (c) || isallowedchar (c); |
| 73 | } |
| 74 | |
| 75 | bool issym (char c) |
| 76 | { |
| 77 | return isalpha (c) || isallowedchar (c) || isdigit (c); |
| 78 | } |
| 79 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 80 | bool readsym (struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 81 | { |
| 82 | skipws (is); |
| 83 | |
| 84 | if ( !issymstart (is->peek (is)) ) |
| 85 | return false; |
| 86 | |
| 87 | int size = 8; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 88 | char *s = malloc_aligned (size); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 89 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 90 | s[ 0 ] = is->get (is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 91 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 92 | for ( int i = 1;; i++ ) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 93 | { |
| 94 | if ( issym (is->peek (is)) ) |
| 95 | { |
| 96 | if ( i >= size ) |
| 97 | { |
| 98 | size *= 2; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 99 | s = realloc_aligned (s, size); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 100 | } |
| 101 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 102 | s[ i ] = is->get (is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 103 | } |
| 104 | else |
| 105 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 106 | s[ i ] = 0; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 107 | *val = (value_t)s; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 108 | *val |= SYMBOL_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 109 | |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 115 | bool readstr (struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 116 | { |
| 117 | skipws (is); |
| 118 | |
| 119 | if ( is->peek (is) != '"' ) |
| 120 | return false; |
| 121 | |
| 122 | bool escape = false; |
| 123 | int size = 8; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 124 | char *s = malloc_aligned (size); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 125 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 126 | (void)is->get (is); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 127 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 128 | for ( int i = 0;; i++ ) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 129 | { |
| 130 | if ( is->peek (is) != '"' ) |
| 131 | { |
| 132 | if ( i >= size ) |
| 133 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 134 | size *= 2; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 135 | s = realloc_aligned (s, size); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 136 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 137 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 138 | char c = is->get (is); |
| 139 | |
| 140 | if ( escape && c == 'n' ) |
| 141 | c = '\n'; |
| 142 | else if ( escape && c == '\\' ) |
| 143 | c = '\\'; |
| 144 | |
| 145 | if ( c == '\\' && !escape ) |
| 146 | { |
| 147 | escape = true; |
| 148 | i--; // will be incremented again, UGLY. |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | escape = false; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 153 | s[ i ] = c; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | is->get (is); |
| 159 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 160 | *val = (value_t)s; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 161 | *val |= STRING_TAG; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 162 | |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 168 | void printval (value_t v, int depth) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 169 | { |
| 170 | for ( int i = 0; i < depth; i++ ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 171 | printf (" "); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 172 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 173 | if ( symbolp (v) ) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 174 | { |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 175 | printf ("'%s\n", (char *)(v ^ SYMBOL_TAG)); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 176 | } |
| 177 | else if ( stringp (v) ) |
| 178 | { |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 179 | printf ("\"%s\"\n", (char *)(v ^ STRING_TAG)); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 180 | } |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 181 | else if ( integerp (v) ) |
| 182 | { |
| 183 | printf ("%d\n", v >> 2); |
| 184 | } |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 185 | else if ( consp (v) ) |
| 186 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 187 | if ( listp (v) ) |
| 188 | { |
| 189 | printf ("list:\n"); |
| 190 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 191 | for ( value_t n = v; !nilp (n); n = cdr (n) ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 192 | { |
| 193 | printval (car (n), depth + 1); |
| 194 | } |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | printf ("cons:\n"); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 199 | printval (car (v), depth + 1); |
| 200 | printval (cdr (v), depth + 1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 201 | } |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 202 | } |
| 203 | else if ( nilp (v) ) |
| 204 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 205 | printf ("nil\n"); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 206 | } |
| 207 | else |
| 208 | { |
| 209 | printf ("<unknown %d>\n", v); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 213 | bool readlist (struct istream *is, value_t *val) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 214 | { |
| 215 | skipws (is); |
| 216 | |
| 217 | if ( is->peek (is) != '(' ) |
| 218 | return false; |
| 219 | |
| 220 | is->get (is); |
| 221 | |
| 222 | *val = readn (is); |
| 223 | |
| 224 | if ( is->peek (is) != ')' ) |
| 225 | { |
| 226 | is->showpos (is, stderr); |
| 227 | err ("Unterminated list"); |
| 228 | return false; |
| 229 | } |
| 230 | is->get (is); |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 235 | bool readint (struct istream *is, value_t *val) |
| 236 | { |
| 237 | int number = 0; |
| 238 | |
| 239 | if ( !isdigit (is->peek (is)) ) |
| 240 | return false; |
| 241 | |
| 242 | while ( isdigit (is->peek (is)) ) |
| 243 | { |
| 244 | number *= 10; |
| 245 | number += is->get (is) - '0'; |
| 246 | } |
| 247 | |
| 248 | *val = intval (number); |
| 249 | return true; |
| 250 | } |
| 251 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 252 | bool read1 (struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 253 | { |
| 254 | if ( readsym (is, val) ) |
| 255 | return true; |
| 256 | |
| 257 | if ( readstr (is, val) ) |
| 258 | return true; |
| 259 | |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 260 | if ( readint (is, val) ) |
| 261 | return true; |
| 262 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 263 | if ( readlist (is, val) ) |
| 264 | return true; |
| 265 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 266 | return false; |
| 267 | } |
| 268 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 269 | value_t readn (struct istream *is) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 270 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 271 | value_t first = nil; |
| 272 | value_t *last = &first; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 273 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 274 | value_t read_val; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 275 | |
| 276 | while ( read1 (is, &read_val) ) |
| 277 | { |
| 278 | *last = cons (read_val, nil); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 279 | last = cdrref (*last); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | return first; |
| 283 | } |
| 284 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 285 | struct stristream_private |
| 286 | { |
| 287 | char *val; |
| 288 | int i; |
| 289 | int length; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 290 | int line; |
| 291 | int fromleft; |
| 292 | int linestart; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | int stristream_peek (struct istream *is) |
| 296 | { |
| 297 | struct stristream_private *p = is->data; |
| 298 | |
| 299 | if ( p->i < p->length ) |
| 300 | return p->val[ p->i ]; |
| 301 | else |
| 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | int stristream_get (struct istream *is) |
| 306 | { |
| 307 | struct stristream_private *p = is->data; |
| 308 | |
| 309 | if ( p->i < p->length ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 310 | { |
| 311 | char c = p->val[ p->i++ ]; |
| 312 | |
| 313 | p->fromleft++; |
| 314 | |
| 315 | if ( c == '\n' ) |
| 316 | { |
| 317 | p->fromleft = 1; |
| 318 | p->line++; |
| 319 | p->linestart = p->i; |
| 320 | } |
| 321 | |
| 322 | return c; |
| 323 | } |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 324 | else |
| 325 | return -1; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | int stristream_read (struct istream *s, char *buffer, int size) |
| 329 | { |
| 330 | struct stristream_private *p = s->data; |
| 331 | |
| 332 | int len = MIN (size, p->length - p->i); |
| 333 | memcpy (buffer, p->val, len); |
| 334 | return len; |
| 335 | } |
| 336 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 337 | void stristream_showpos (struct istream *s, FILE *out) |
| 338 | { |
| 339 | struct stristream_private *p = s->data; |
| 340 | |
| 341 | fprintf (out, "line: %d, char %d\n", p->line, p->fromleft); |
| 342 | |
| 343 | int end = p->length; |
| 344 | |
| 345 | for ( int i = p->linestart; i < p->length; i++ ) |
| 346 | { |
| 347 | if ( p->val[ i ] == '\n' ) |
| 348 | { |
| 349 | end = i; |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | fprintf (out, " | %.*s\n", end - p->linestart, p->val + p->linestart); |
| 355 | fprintf (out, " | "); |
| 356 | for ( int i = 0; i < p->fromleft - 1; i++ ) |
| 357 | fprintf (out, " "); |
| 358 | |
| 359 | fprintf (out, "\033[31m^\033[0m\n"); |
| 360 | } |
| 361 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 362 | struct istream *new_stristream (char *str, int length) |
| 363 | { |
| 364 | struct istream *is = malloc (sizeof (struct istream)); |
| 365 | struct stristream_private *p = malloc (sizeof (struct stristream_private)); |
| 366 | |
| 367 | p->val = strndup (str, length); |
| 368 | p->i = 0; |
| 369 | p->length = length; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 370 | p->line = 1; |
| 371 | p->fromleft = 1; |
| 372 | p->linestart = 0; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 373 | |
| 374 | is->data = p; |
| 375 | is->get = stristream_get; |
| 376 | is->peek = stristream_peek; |
| 377 | is->read = stristream_read; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 378 | is->showpos = stristream_showpos; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 379 | |
| 380 | return is; |
| 381 | } |
| 382 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 383 | void del_stristream (struct istream *stristream) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 384 | { |
| 385 | struct stristream_private *p = stristream->data; |
| 386 | free (p->val); |
| 387 | free (p); |
| 388 | free (stristream); |
| 389 | } |
| 390 | |
| 391 | struct istream *new_stristream_nt (char *str) |
| 392 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 393 | return new_stristream (str, strlen (str)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 394 | } |
| 395 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 396 | bool startswith (struct istream *s, char *pattern) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 397 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 398 | char *check = strdup (pattern); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 399 | s->read (s, check, strlen (pattern)); |
| 400 | |
| 401 | bool res = strcmp (check, pattern) == 0; |
| 402 | free (check); |
| 403 | |
| 404 | return res; |
| 405 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 406 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 407 | value_t strval (char *str) |
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 | |
| 411 | char *a = malloc_aligned (strlen (str) + 1); |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 412 | v = (value_t)a; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 413 | v |= STRING_TAG; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 414 | |
| 415 | return v; |
| 416 | } |
| 417 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 418 | bool integerp (value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 419 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 420 | return (v & INT_MASK) == INT_TAG; |
| 421 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 422 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 423 | bool symbolp (value_t v) |
| 424 | { |
| 425 | return (v & HEAP_MASK) == SYMBOL_TAG; |
| 426 | } |
| 427 | |
| 428 | bool stringp (value_t v) |
| 429 | { |
| 430 | return (v & HEAP_MASK) == STRING_TAG; |
| 431 | } |
| 432 | |
| 433 | bool consp (value_t v) |
| 434 | { |
| 435 | return (v & HEAP_MASK) == CONS_TAG; |
| 436 | } |
| 437 | |
| 438 | bool listp (value_t v) |
| 439 | { |
| 440 | value_t next = v; |
| 441 | |
| 442 | while ( consp (next) ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 443 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 444 | next = cdr (next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 445 | } |
| 446 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 447 | return nilp (next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 448 | } |
| 449 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 450 | value_t car (value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 451 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 452 | if ( !consp (v) ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 453 | return nil; |
| 454 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 455 | return *carref (v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 456 | } |
| 457 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 458 | value_t cdr (value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 459 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 460 | if ( !consp (v) ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 461 | return nil; |
| 462 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 463 | return *cdrref (v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 464 | } |
| 465 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 466 | value_t *carref (value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 467 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 468 | if ( !consp (v) ) |
| 469 | return NULL; |
| 470 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 471 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 472 | return &c->car; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 473 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 474 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 475 | value_t *cdrref (value_t v) |
| 476 | { |
| 477 | if ( !consp (v) ) |
| 478 | return NULL; |
| 479 | |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 480 | struct cons *c = (void *)(v ^ CONS_TAG); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 481 | return &c->cdr; |
| 482 | } |
| 483 | |
| 484 | bool nilp (value_t v) |
| 485 | { |
| 486 | return v == nil; |
| 487 | } |
| 488 | |
| 489 | int length (value_t v) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 490 | { |
| 491 | int i = 0; |
| 492 | |
| 493 | FOREACH (item, v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 494 | { |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 495 | (void)item; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 496 | i++; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 497 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 498 | |
| 499 | return i; |
| 500 | } |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame^] | 501 | |
| 502 | value_t elt (value_t v, int index) |
| 503 | { |
| 504 | for ( int = 0; i < index; i++ ) |
| 505 | { |
| 506 | v = cdr (v); |
| 507 | } |
| 508 | |
| 509 | return car (v); |
| 510 | } |