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 | } |
| 180 | else if ( consp (v) ) |
| 181 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 182 | if ( listp (v) ) |
| 183 | { |
| 184 | printf ("list:\n"); |
| 185 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 186 | for ( value_t n = v; !nilp (n); n = cdr (n) ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 187 | { |
| 188 | printval (car (n), depth + 1); |
| 189 | } |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | printf ("cons:\n"); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 194 | printval (car (v), depth + 1); |
| 195 | printval (cdr (v), depth + 1); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 196 | } |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 197 | } |
| 198 | else if ( nilp (v) ) |
| 199 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 200 | printf ("nil\n"); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 201 | } |
| 202 | else |
| 203 | { |
| 204 | printf ("<unknown %d>\n", v); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 208 | bool readlist (struct istream *is, value_t *val) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 209 | { |
| 210 | skipws (is); |
| 211 | |
| 212 | if ( is->peek (is) != '(' ) |
| 213 | return false; |
| 214 | |
| 215 | is->get (is); |
| 216 | |
| 217 | *val = readn (is); |
| 218 | |
| 219 | if ( is->peek (is) != ')' ) |
| 220 | { |
| 221 | is->showpos (is, stderr); |
| 222 | err ("Unterminated list"); |
| 223 | return false; |
| 224 | } |
| 225 | is->get (is); |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 230 | bool read1 (struct istream *is, value_t *val) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 231 | { |
| 232 | if ( readsym (is, val) ) |
| 233 | return true; |
| 234 | |
| 235 | if ( readstr (is, val) ) |
| 236 | return true; |
| 237 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 238 | if ( readlist (is, val) ) |
| 239 | return true; |
| 240 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 244 | value_t readn (struct istream *is) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 245 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 246 | value_t first = nil; |
| 247 | value_t *last = &first; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 248 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 249 | value_t read_val; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 250 | |
| 251 | while ( read1 (is, &read_val) ) |
| 252 | { |
| 253 | *last = cons (read_val, nil); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 254 | last = cdrref (*last); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return first; |
| 258 | } |
| 259 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 260 | struct stristream_private |
| 261 | { |
| 262 | char *val; |
| 263 | int i; |
| 264 | int length; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 265 | int line; |
| 266 | int fromleft; |
| 267 | int linestart; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | int stristream_peek (struct istream *is) |
| 271 | { |
| 272 | struct stristream_private *p = is->data; |
| 273 | |
| 274 | if ( p->i < p->length ) |
| 275 | return p->val[ p->i ]; |
| 276 | else |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | int stristream_get (struct istream *is) |
| 281 | { |
| 282 | struct stristream_private *p = is->data; |
| 283 | |
| 284 | if ( p->i < p->length ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 285 | { |
| 286 | char c = p->val[ p->i++ ]; |
| 287 | |
| 288 | p->fromleft++; |
| 289 | |
| 290 | if ( c == '\n' ) |
| 291 | { |
| 292 | p->fromleft = 1; |
| 293 | p->line++; |
| 294 | p->linestart = p->i; |
| 295 | } |
| 296 | |
| 297 | return c; |
| 298 | } |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 299 | else |
| 300 | return -1; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | int stristream_read (struct istream *s, char *buffer, int size) |
| 304 | { |
| 305 | struct stristream_private *p = s->data; |
| 306 | |
| 307 | int len = MIN (size, p->length - p->i); |
| 308 | memcpy (buffer, p->val, len); |
| 309 | return len; |
| 310 | } |
| 311 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 312 | void stristream_showpos (struct istream *s, FILE *out) |
| 313 | { |
| 314 | struct stristream_private *p = s->data; |
| 315 | |
| 316 | fprintf (out, "line: %d, char %d\n", p->line, p->fromleft); |
| 317 | |
| 318 | int end = p->length; |
| 319 | |
| 320 | for ( int i = p->linestart; i < p->length; i++ ) |
| 321 | { |
| 322 | if ( p->val[ i ] == '\n' ) |
| 323 | { |
| 324 | end = i; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | fprintf (out, " | %.*s\n", end - p->linestart, p->val + p->linestart); |
| 330 | fprintf (out, " | "); |
| 331 | for ( int i = 0; i < p->fromleft - 1; i++ ) |
| 332 | fprintf (out, " "); |
| 333 | |
| 334 | fprintf (out, "\033[31m^\033[0m\n"); |
| 335 | } |
| 336 | |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 337 | struct istream *new_stristream (char *str, int length) |
| 338 | { |
| 339 | struct istream *is = malloc (sizeof (struct istream)); |
| 340 | struct stristream_private *p = malloc (sizeof (struct stristream_private)); |
| 341 | |
| 342 | p->val = strndup (str, length); |
| 343 | p->i = 0; |
| 344 | p->length = length; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 345 | p->line = 1; |
| 346 | p->fromleft = 1; |
| 347 | p->linestart = 0; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 348 | |
| 349 | is->data = p; |
| 350 | is->get = stristream_get; |
| 351 | is->peek = stristream_peek; |
| 352 | is->read = stristream_read; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 353 | is->showpos = stristream_showpos; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 354 | |
| 355 | return is; |
| 356 | } |
| 357 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 358 | void del_stristream (struct istream *stristream) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 359 | { |
| 360 | struct stristream_private *p = stristream->data; |
| 361 | free (p->val); |
| 362 | free (p); |
| 363 | free (stristream); |
| 364 | } |
| 365 | |
| 366 | struct istream *new_stristream_nt (char *str) |
| 367 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 368 | return new_stristream (str, strlen (str)); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 369 | } |
| 370 | |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 371 | bool startswith (struct istream *s, char *pattern) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 372 | { |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 373 | char *check = strdup (pattern); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 374 | s->read (s, check, strlen (pattern)); |
| 375 | |
| 376 | bool res = strcmp (check, pattern) == 0; |
| 377 | free (check); |
| 378 | |
| 379 | return res; |
| 380 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 381 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 382 | value_t strval (char *str) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 383 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 384 | value_t v; |
| 385 | |
| 386 | char *a = malloc_aligned (strlen (str) + 1); |
| 387 | v = (value_t) a; |
| 388 | v |= STRING_TAG; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 389 | |
| 390 | return v; |
| 391 | } |
| 392 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 393 | bool integerp (value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 394 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 395 | return (v & INT_MASK) == INT_TAG; |
| 396 | } |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 397 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 398 | bool symbolp (value_t v) |
| 399 | { |
| 400 | return (v & HEAP_MASK) == SYMBOL_TAG; |
| 401 | } |
| 402 | |
| 403 | bool stringp (value_t v) |
| 404 | { |
| 405 | return (v & HEAP_MASK) == STRING_TAG; |
| 406 | } |
| 407 | |
| 408 | bool consp (value_t v) |
| 409 | { |
| 410 | return (v & HEAP_MASK) == CONS_TAG; |
| 411 | } |
| 412 | |
| 413 | bool listp (value_t v) |
| 414 | { |
| 415 | value_t next = v; |
| 416 | |
| 417 | while ( consp (next) ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 418 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 419 | next = cdr (next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 420 | } |
| 421 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 422 | return nilp (next); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 423 | } |
| 424 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 425 | value_t car (value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 426 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 427 | if ( !consp (v) ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 428 | return nil; |
| 429 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 430 | return *carref (v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 431 | } |
| 432 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 433 | value_t cdr (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 | if ( !consp (v) ) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 436 | return nil; |
| 437 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 438 | return *cdrref (v); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 439 | } |
| 440 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 441 | value_t *carref (value_t v) |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 442 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 443 | if ( !consp (v) ) |
| 444 | return NULL; |
| 445 | |
| 446 | struct cons *c = (void *) (v ^ CONS_TAG); |
| 447 | return &c->car; |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 448 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 449 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 450 | value_t *cdrref (value_t v) |
| 451 | { |
| 452 | if ( !consp (v) ) |
| 453 | return NULL; |
| 454 | |
| 455 | struct cons *c = (void *) (v ^ CONS_TAG); |
| 456 | return &c->cdr; |
| 457 | } |
| 458 | |
| 459 | bool nilp (value_t v) |
| 460 | { |
| 461 | return v == nil; |
| 462 | } |
| 463 | |
| 464 | int length (value_t v) |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 465 | { |
| 466 | int i = 0; |
| 467 | |
| 468 | FOREACH (item, v) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 469 | { |
| 470 | (void) item; |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 471 | i++; |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame^] | 472 | } |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 473 | |
| 474 | return i; |
| 475 | } |