swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 3 | #include "istream.h" |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 4 | #include <stdbool.h> |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 5 | #include <stdio.h> |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 6 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 7 | #define INT_MASK 0b11 |
| 8 | #define INT_TAG 0b00 |
| 9 | |
| 10 | #define CHAR_MASK 0xff |
| 11 | #define CHAR_TAG 0b00001111 |
| 12 | |
| 13 | #define BOOL_MASK 0b1111111 |
| 14 | #define BOOL_TAG 0b0011111 |
| 15 | |
| 16 | #define HEAP_MASK 0b111 |
| 17 | |
| 18 | #define CONS_TAG 0b001 |
| 19 | #define VECTOR_TAG 0b010 |
swissChili | 6eee4f9 | 2021-04-20 09:34:30 -0700 | [diff] [blame] | 20 | #define STRING_TAG 0b011 |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 21 | #define SYMBOL_TAG 0b101 |
| 22 | #define CLOSURE_TAG 0b110 |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 23 | |
| 24 | struct cons; |
| 25 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 26 | typedef unsigned int value_t; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 27 | |
| 28 | struct cons |
| 29 | { |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 30 | value_t car, cdr; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame^] | 33 | |
| 34 | // It is integral that this be 16 bytes long so that whatever follows it is |
| 35 | // still aligned to 4 bits. |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 36 | struct alloc |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 37 | { |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame^] | 38 | unsigned int type_tag; // 4 |
| 39 | struct alloc *prev, *next; // + 8 |
| 40 | unsigned int mark; // + 4 = 16 |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 41 | |
| 42 | // Whatever else |
| 43 | }; |
| 44 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame^] | 45 | extern struct alloc *first_a, *last_a; |
| 46 | |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 47 | struct cons_alloc |
| 48 | { |
| 49 | struct alloc alloc; |
| 50 | struct cons cons; |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 53 | bool startswith(struct istream *s, char *pattern); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 54 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 55 | bool readsym(struct istream *is, value_t *val); |
| 56 | bool readstr(struct istream *is, value_t *val); |
| 57 | bool readlist(struct istream *is, value_t *val); |
| 58 | bool readint(struct istream *is, value_t *val); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 59 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 60 | value_t intval(int i); |
| 61 | value_t strval(char *str); |
| 62 | value_t cons(value_t car, value_t cdr); |
| 63 | bool read1(struct istream *is, value_t *val); |
| 64 | value_t read(struct istream *is); |
| 65 | value_t readn(struct istream *is); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 66 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 67 | value_t car(value_t v); |
| 68 | value_t cdr(value_t v); |
| 69 | value_t *carref(value_t v); |
| 70 | value_t *cdrref(value_t v); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 71 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 72 | bool integerp(value_t v); |
| 73 | bool symbolp(value_t v); |
| 74 | bool stringp(value_t v); |
| 75 | bool consp(value_t v); |
| 76 | bool listp(value_t v); |
| 77 | bool nilp(value_t v); |
swissChili | 9e57da4 | 2021-06-15 22:22:46 -0700 | [diff] [blame] | 78 | bool heapp(value_t v); |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 79 | int length(value_t v); |
| 80 | value_t elt(value_t v, int index); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 81 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 82 | void printval(value_t v, int depth); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 83 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 84 | void err(const char *msg); |
swissChili | bed8092 | 2021-04-13 21:58:05 -0700 | [diff] [blame] | 85 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 86 | bool symstreq(value_t sym, char *str); |
swissChili | ca107a0 | 2021-04-14 12:07:30 -0700 | [diff] [blame] | 87 | |
swissChili | 8fc5e2f | 2021-04-22 13:45:10 -0700 | [diff] [blame] | 88 | extern value_t nil; |
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 89 | extern value_t t; |