swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdbool.h> |
| 4 | |
| 5 | extern unsigned int cons_magic; |
| 6 | |
| 7 | enum type |
| 8 | { |
| 9 | T_INT = 0, |
| 10 | T_FLOAT, |
| 11 | T_NIL, |
| 12 | T_SYMBOL, |
| 13 | T_STRING, |
| 14 | T_VECTOR, |
| 15 | T_CLASS, |
| 16 | T_CONS, |
| 17 | }; |
| 18 | |
| 19 | struct tag |
| 20 | { |
| 21 | unsigned int type : 3; |
| 22 | unsigned int length : 29; |
| 23 | } __attribute__ ((packed)); |
| 24 | |
| 25 | struct cons; |
| 26 | |
| 27 | union value_type { |
| 28 | int int_val; |
| 29 | float float_val; |
| 30 | struct cons *cons_val; |
| 31 | char *symbol_val; // interned |
| 32 | char *string_val; |
| 33 | }; |
| 34 | |
| 35 | struct value |
| 36 | { |
| 37 | struct tag tag; |
| 38 | union value_type value; |
| 39 | } __attribute__ ((packed)); |
| 40 | |
| 41 | struct cons |
| 42 | { |
| 43 | int magic; |
| 44 | int marked; // must be reserved |
| 45 | struct value car, cdr; |
| 46 | }; |
| 47 | |
| 48 | struct alloc_list |
| 49 | { |
| 50 | int type; |
| 51 | union value_type data; |
| 52 | struct alloc_list *next, *prev; |
| 53 | }; |
| 54 | |
| 55 | struct istream |
| 56 | { |
| 57 | void *data; |
| 58 | |
| 59 | // These two return -1 on error |
| 60 | int (*peek) (struct istream *s); |
| 61 | int (*get) (struct istream *s); |
| 62 | |
| 63 | int (*read) (struct istream *s, char *buffer, int size); |
| 64 | }; |
| 65 | |
| 66 | bool startswith (struct istream *s, const char *pattern); |
| 67 | |
| 68 | bool readsym (struct istream *is, struct value *val); |
| 69 | bool readstr (struct istream *is, struct value *val); |
| 70 | |
| 71 | struct value cons (struct value car, struct value cdr); |
| 72 | bool read1 (struct istream *is, struct value *val); |
| 73 | struct value read (struct istream); |
| 74 | struct value readn (struct istream); |
| 75 | |
| 76 | void printval (struct value v, int depth); |
| 77 | |
| 78 | struct istream *new_stristream (char *str, int length); |
| 79 | // same as above but null terminated |
| 80 | struct istream *new_stristream_nt (char *str); |
| 81 | void del_stristream(struct istream *stristream); |