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 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 18 | #define INT_MASK 0b11 |
| 19 | #define INT_TAG 0b00 |
| 20 | |
| 21 | #define CHAR_MASK 0xff |
| 22 | #define CHAR_TAG 0b00001111 |
| 23 | |
| 24 | #define BOOL_MASK 0b1111111 |
| 25 | #define BOOL_TAG 0b0011111 |
| 26 | |
| 27 | #define HEAP_MASK 0b111 |
| 28 | |
| 29 | #define CONS_TAG 0b001 |
| 30 | #define VECTOR_TAG 0b010 |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 31 | #define STRING_TAG 0b011 |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 32 | #define SYMBOL_TAG 0b101 |
| 33 | #define CLOSURE_TAG 0b110 |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 34 | |
| 35 | struct cons; |
| 36 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 37 | typedef unsigned int value_t; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 38 | |
| 39 | struct cons |
| 40 | { |
| 41 | int magic; |
| 42 | int marked; // must be reserved |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 43 | value_t car, cdr; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct alloc_list |
| 47 | { |
| 48 | int type; |
swissChili | b3ca4fb | 2021-04-20 10:33:00 -0700 | [diff] [blame] | 49 | union { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 50 | struct cons *cons_val; |
| 51 | }; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 52 | struct alloc_list *next, *prev; |
| 53 | }; |
| 54 | |
| 55 | struct istream |
| 56 | { |
| 57 | void *data; |
| 58 | |
| 59 | // These two return -1 on error |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 60 | int (*peek)(struct istream *s); |
| 61 | int (*get)(struct istream *s); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 62 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 63 | int (*read)(struct istream *s, char *buffer, int size); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 64 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 65 | void (*showpos)(struct istream *s, FILE *out); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 68 | bool startswith(struct istream *s, char *pattern); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 69 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 70 | bool readsym(struct istream *is, value_t *val); |
| 71 | bool readstr(struct istream *is, value_t *val); |
| 72 | bool readlist(struct istream *is, value_t *val); |
| 73 | bool readint(struct istream *is, value_t *val); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 74 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 75 | value_t intval(int i); |
| 76 | value_t strval(char *str); |
| 77 | value_t cons(value_t car, value_t cdr); |
| 78 | bool read1(struct istream *is, value_t *val); |
| 79 | value_t read(struct istream *is); |
| 80 | value_t readn(struct istream *is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 81 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 82 | value_t car(value_t v); |
| 83 | value_t cdr(value_t v); |
| 84 | value_t *carref(value_t v); |
| 85 | value_t *cdrref(value_t v); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 86 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 87 | bool integerp(value_t v); |
| 88 | bool symbolp(value_t v); |
| 89 | bool stringp(value_t v); |
| 90 | bool consp(value_t v); |
| 91 | bool listp(value_t v); |
| 92 | bool nilp(value_t v); |
| 93 | int length(value_t v); |
| 94 | value_t elt(value_t v, int index); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 95 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 96 | void printval(value_t v, int depth); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 97 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 98 | struct istream *new_stristream(char *str, int length); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 99 | // same as above but null terminated |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 100 | struct istream *new_stristream_nt(char *str); |
| 101 | void del_stristream(struct istream *stristream); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 102 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 103 | void err(const char *msg); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 104 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame^] | 105 | bool symstreq(value_t sym, char *str); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 106 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 107 | extern value_t nil; |