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