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