blob: 1424abe11225f4051cf9fae64a7b4b381fba66a0 [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{
swissChili8cfb7c42021-04-18 21:17:58 -070042 value_t car, cdr;
swissChili7a6f5eb2021-04-13 16:46:02 -070043};
44
swissChili9e57da42021-06-15 22:22:46 -070045struct alloc
swissChili7a6f5eb2021-04-13 16:46:02 -070046{
swissChili9e57da42021-06-15 22:22:46 -070047 unsigned int type_tag;
48 struct alloc *prev, *next;
49 unsigned int marked;
50
51 // Whatever else
52};
53
54struct cons_alloc
55{
56 struct alloc alloc;
57 struct cons cons;
swissChili7a6f5eb2021-04-13 16:46:02 -070058};
59
swissChili53472e82021-05-08 16:06:32 -070060bool startswith(struct istream *s, char *pattern);
swissChili7a6f5eb2021-04-13 16:46:02 -070061
swissChili53472e82021-05-08 16:06:32 -070062bool readsym(struct istream *is, value_t *val);
63bool readstr(struct istream *is, value_t *val);
64bool readlist(struct istream *is, value_t *val);
65bool readint(struct istream *is, value_t *val);
swissChili7a6f5eb2021-04-13 16:46:02 -070066
swissChili53472e82021-05-08 16:06:32 -070067value_t intval(int i);
68value_t strval(char *str);
69value_t cons(value_t car, value_t cdr);
70bool read1(struct istream *is, value_t *val);
71value_t read(struct istream *is);
72value_t readn(struct istream *is);
swissChilibed80922021-04-13 21:58:05 -070073
swissChili53472e82021-05-08 16:06:32 -070074value_t car(value_t v);
75value_t cdr(value_t v);
76value_t *carref(value_t v);
77value_t *cdrref(value_t v);
swissChili7a6f5eb2021-04-13 16:46:02 -070078
swissChili53472e82021-05-08 16:06:32 -070079bool integerp(value_t v);
80bool symbolp(value_t v);
81bool stringp(value_t v);
82bool consp(value_t v);
83bool listp(value_t v);
84bool nilp(value_t v);
swissChili9e57da42021-06-15 22:22:46 -070085bool heapp(value_t v);
swissChili53472e82021-05-08 16:06:32 -070086int length(value_t v);
87value_t elt(value_t v, int index);
swissChili8cfb7c42021-04-18 21:17:58 -070088
swissChili53472e82021-05-08 16:06:32 -070089void printval(value_t v, int depth);
swissChili7a6f5eb2021-04-13 16:46:02 -070090
swissChili53472e82021-05-08 16:06:32 -070091void err(const char *msg);
swissChilibed80922021-04-13 21:58:05 -070092
swissChili53472e82021-05-08 16:06:32 -070093bool symstreq(value_t sym, char *str);
swissChilica107a02021-04-14 12:07:30 -070094
swissChili8fc5e2f2021-04-22 13:45:10 -070095extern value_t nil;
swissChili923b5362021-05-09 20:31:43 -070096extern value_t t;