blob: 48de7d422320f6543aad78d51fefb0d1d258479a [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
7enum 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
swissChili8cfb7c42021-04-18 21:17:58 -070019#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
swissChili6eee4f92021-04-20 09:34:30 -070032#define STRING_TAG 0b011
swissChili8cfb7c42021-04-18 21:17:58 -070033#define SYMBOL_TAG 0b101
34#define CLOSURE_TAG 0b110
swissChili7a6f5eb2021-04-13 16:46:02 -070035
36struct cons;
37
swissChili8cfb7c42021-04-18 21:17:58 -070038typedef unsigned int value_t;
swissChili7a6f5eb2021-04-13 16:46:02 -070039
40struct cons
41{
42 int magic;
43 int marked; // must be reserved
swissChili8cfb7c42021-04-18 21:17:58 -070044 value_t car, cdr;
swissChili7a6f5eb2021-04-13 16:46:02 -070045};
46
47struct alloc_list
48{
49 int type;
swissChilib3ca4fb2021-04-20 10:33:00 -070050 union {
swissChili8cfb7c42021-04-18 21:17:58 -070051 struct cons *cons_val;
52 };
swissChili7a6f5eb2021-04-13 16:46:02 -070053 struct alloc_list *next, *prev;
54};
55
swissChili53472e82021-05-08 16:06:32 -070056bool startswith(struct istream *s, char *pattern);
swissChili7a6f5eb2021-04-13 16:46:02 -070057
swissChili53472e82021-05-08 16:06:32 -070058bool readsym(struct istream *is, value_t *val);
59bool readstr(struct istream *is, value_t *val);
60bool readlist(struct istream *is, value_t *val);
61bool readint(struct istream *is, value_t *val);
swissChili7a6f5eb2021-04-13 16:46:02 -070062
swissChili53472e82021-05-08 16:06:32 -070063value_t intval(int i);
64value_t strval(char *str);
65value_t cons(value_t car, value_t cdr);
66bool read1(struct istream *is, value_t *val);
67value_t read(struct istream *is);
68value_t readn(struct istream *is);
swissChilibed80922021-04-13 21:58:05 -070069
swissChili53472e82021-05-08 16:06:32 -070070value_t car(value_t v);
71value_t cdr(value_t v);
72value_t *carref(value_t v);
73value_t *cdrref(value_t v);
swissChili7a6f5eb2021-04-13 16:46:02 -070074
swissChili53472e82021-05-08 16:06:32 -070075bool integerp(value_t v);
76bool symbolp(value_t v);
77bool stringp(value_t v);
78bool consp(value_t v);
79bool listp(value_t v);
80bool nilp(value_t v);
81int length(value_t v);
82value_t elt(value_t v, int index);
swissChili8cfb7c42021-04-18 21:17:58 -070083
swissChili53472e82021-05-08 16:06:32 -070084void printval(value_t v, int depth);
swissChili7a6f5eb2021-04-13 16:46:02 -070085
swissChili53472e82021-05-08 16:06:32 -070086void err(const char *msg);
swissChilibed80922021-04-13 21:58:05 -070087
swissChili53472e82021-05-08 16:06:32 -070088bool symstreq(value_t sym, char *str);
swissChilica107a02021-04-14 12:07:30 -070089
swissChili8fc5e2f2021-04-22 13:45:10 -070090extern value_t nil;
swissChili923b5362021-05-09 20:31:43 -070091extern value_t t;