blob: 03ab75eed93870cce65f80feaef37281d085a829 [file] [log] [blame]
swissChili7a6f5eb2021-04-13 16:46:02 -07001#pragma once
2
swissChili923b5362021-05-09 20:31:43 -07003#include "istream.h"
swissChili7a6f5eb2021-04-13 16:46:02 -07004#include <stdbool.h>
swissChilibed80922021-04-13 21:58:05 -07005#include <stdio.h>
swissChili7a6f5eb2021-04-13 16:46:02 -07006
swissChili8cfb7c42021-04-18 21:17:58 -07007#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
swissChili6eee4f92021-04-20 09:34:30 -070020#define STRING_TAG 0b011
swissChili8cfb7c42021-04-18 21:17:58 -070021#define SYMBOL_TAG 0b101
22#define CLOSURE_TAG 0b110
swissChili7a6f5eb2021-04-13 16:46:02 -070023
24struct cons;
25
swissChili8cfb7c42021-04-18 21:17:58 -070026typedef unsigned int value_t;
swissChili7a6f5eb2021-04-13 16:46:02 -070027
28struct cons
29{
swissChili8cfb7c42021-04-18 21:17:58 -070030 value_t car, cdr;
swissChili7a6f5eb2021-04-13 16:46:02 -070031};
32
swissChilie9fec8b2021-06-22 13:59:33 -070033
34// It is integral that this be 16 bytes long so that whatever follows it is
35// still aligned to 4 bits.
swissChili9e57da42021-06-15 22:22:46 -070036struct alloc
swissChili7a6f5eb2021-04-13 16:46:02 -070037{
swissChilie9fec8b2021-06-22 13:59:33 -070038 unsigned int type_tag; // 4
39 struct alloc *prev, *next; // + 8
40 unsigned int mark; // + 4 = 16
swissChili9e57da42021-06-15 22:22:46 -070041
42 // Whatever else
43};
44
swissChilie9fec8b2021-06-22 13:59:33 -070045extern struct alloc *first_a, *last_a;
46
swissChili9e57da42021-06-15 22:22:46 -070047struct cons_alloc
48{
49 struct alloc alloc;
50 struct cons cons;
swissChili7a6f5eb2021-04-13 16:46:02 -070051};
52
swissChili53472e82021-05-08 16:06:32 -070053bool startswith(struct istream *s, char *pattern);
swissChili7a6f5eb2021-04-13 16:46:02 -070054
swissChili53472e82021-05-08 16:06:32 -070055bool readsym(struct istream *is, value_t *val);
56bool readstr(struct istream *is, value_t *val);
57bool readlist(struct istream *is, value_t *val);
58bool readint(struct istream *is, value_t *val);
swissChili7a6f5eb2021-04-13 16:46:02 -070059
swissChili53472e82021-05-08 16:06:32 -070060value_t intval(int i);
61value_t strval(char *str);
62value_t cons(value_t car, value_t cdr);
63bool read1(struct istream *is, value_t *val);
64value_t read(struct istream *is);
65value_t readn(struct istream *is);
swissChilibed80922021-04-13 21:58:05 -070066
swissChili53472e82021-05-08 16:06:32 -070067value_t car(value_t v);
68value_t cdr(value_t v);
69value_t *carref(value_t v);
70value_t *cdrref(value_t v);
swissChili7a6f5eb2021-04-13 16:46:02 -070071
swissChili53472e82021-05-08 16:06:32 -070072bool integerp(value_t v);
73bool symbolp(value_t v);
74bool stringp(value_t v);
75bool consp(value_t v);
76bool listp(value_t v);
77bool nilp(value_t v);
swissChili9e57da42021-06-15 22:22:46 -070078bool heapp(value_t v);
swissChili53472e82021-05-08 16:06:32 -070079int length(value_t v);
80value_t elt(value_t v, int index);
swissChili8cfb7c42021-04-18 21:17:58 -070081
swissChili53472e82021-05-08 16:06:32 -070082void printval(value_t v, int depth);
swissChili7a6f5eb2021-04-13 16:46:02 -070083
swissChili53472e82021-05-08 16:06:32 -070084void err(const char *msg);
swissChilibed80922021-04-13 21:58:05 -070085
swissChili53472e82021-05-08 16:06:32 -070086bool symstreq(value_t sym, char *str);
swissChilica107a02021-04-14 12:07:30 -070087
swissChili8fc5e2f2021-04-22 13:45:10 -070088extern value_t nil;
swissChili923b5362021-05-09 20:31:43 -070089extern value_t t;