blob: 9078b45e586d96af07405c7c83cfb39d8dc4890a [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
swissChilib8fd4712021-06-23 15:32:04 -070033/// Default pool (no pool)
34#define NO_POOL 0
35
36/**
37 * The max used pool number, don't touch this.
38 */
39extern unsigned char max_pool;
40
41/**
42 * Current allocation pool, default 0 (no pool)
43 */
44extern unsigned char current_pool;
swissChilie9fec8b2021-06-22 13:59:33 -070045
46// It is integral that this be 16 bytes long so that whatever follows it is
47// still aligned to 4 bits.
swissChili9e57da42021-06-15 22:22:46 -070048struct alloc
swissChili7a6f5eb2021-04-13 16:46:02 -070049{
swissChilib8fd4712021-06-23 15:32:04 -070050 /**
51 * One of the type tags, eg CONS_TAG, etc
52 */
swissChilie9fec8b2021-06-22 13:59:33 -070053 unsigned int type_tag; // 4
54 struct alloc *prev, *next; // + 8
swissChilib8fd4712021-06-23 15:32:04 -070055 /**
56 * Zero if this is not part of a release pool, pool number otherwise.
57 */
58 unsigned char pool; // + 1
59 /**
60 * Reserved for the GC.
61 */
62 unsigned int mark : 24; // + 2 = 16
swissChili9e57da42021-06-15 22:22:46 -070063
64 // Whatever else
65};
66
swissChilie9fec8b2021-06-22 13:59:33 -070067extern struct alloc *first_a, *last_a;
68
swissChili9e57da42021-06-15 22:22:46 -070069struct cons_alloc
70{
71 struct alloc alloc;
72 struct cons cons;
swissChili7a6f5eb2021-04-13 16:46:02 -070073};
74
swissChilib8fd4712021-06-23 15:32:04 -070075/**
76 * Create a new allocation pool.
77 */
78unsigned char make_pool();
79
80/**
81 * Set the allocation pull
82 * @returns the old pool, you should reset this later with pop_pool.
83 */
84unsigned char push_pool(unsigned char pool);
85
86/**
87 * Set the allocation pool and throw away the old value.
88 */
89void pop_pool(unsigned char pool);
90
91/**
92 * @returns true if pool is still alive (in scope).
93 */
94bool pool_alive(unsigned char pool);
95
swissChili53472e82021-05-08 16:06:32 -070096bool startswith(struct istream *s, char *pattern);
swissChili7a6f5eb2021-04-13 16:46:02 -070097
swissChili53472e82021-05-08 16:06:32 -070098bool readsym(struct istream *is, value_t *val);
99bool readstr(struct istream *is, value_t *val);
100bool readlist(struct istream *is, value_t *val);
101bool readint(struct istream *is, value_t *val);
swissChili7a6f5eb2021-04-13 16:46:02 -0700102
swissChili53472e82021-05-08 16:06:32 -0700103value_t intval(int i);
104value_t strval(char *str);
105value_t cons(value_t car, value_t cdr);
106bool read1(struct istream *is, value_t *val);
107value_t read(struct istream *is);
108value_t readn(struct istream *is);
swissChilibed80922021-04-13 21:58:05 -0700109
swissChili53472e82021-05-08 16:06:32 -0700110value_t car(value_t v);
111value_t cdr(value_t v);
112value_t *carref(value_t v);
113value_t *cdrref(value_t v);
swissChili7a6f5eb2021-04-13 16:46:02 -0700114
swissChili53472e82021-05-08 16:06:32 -0700115bool integerp(value_t v);
116bool symbolp(value_t v);
117bool stringp(value_t v);
118bool consp(value_t v);
119bool listp(value_t v);
120bool nilp(value_t v);
swissChili9e57da42021-06-15 22:22:46 -0700121bool heapp(value_t v);
swissChili53472e82021-05-08 16:06:32 -0700122int length(value_t v);
123value_t elt(value_t v, int index);
swissChili8cfb7c42021-04-18 21:17:58 -0700124
swissChili53472e82021-05-08 16:06:32 -0700125void printval(value_t v, int depth);
swissChili7a6f5eb2021-04-13 16:46:02 -0700126
swissChili53472e82021-05-08 16:06:32 -0700127void err(const char *msg);
swissChilibed80922021-04-13 21:58:05 -0700128
swissChili53472e82021-05-08 16:06:32 -0700129bool symstreq(value_t sym, char *str);
swissChilica107a02021-04-14 12:07:30 -0700130
swissChili8fc5e2f2021-04-22 13:45:10 -0700131extern value_t nil;
swissChili923b5362021-05-09 20:31:43 -0700132extern value_t t;