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