blob: d8bc0e4ad267b806e2195d34491c6273027d3958 [file] [log] [blame]
swissChili7a6f5eb2021-04-13 16:46:02 -07001#pragma once
2
3#include <stdbool.h>
swissChilibed80922021-04-13 21:58:05 -07004#include <stdio.h>
swissChili7a6f5eb2021-04-13 16:46:02 -07005
6enum type
7{
8 T_INT = 0,
9 T_FLOAT,
10 T_NIL,
11 T_SYMBOL,
12 T_STRING,
13 T_VECTOR,
14 T_CLASS,
15 T_CONS,
16};
17
18struct tag
19{
20 unsigned int type : 3;
21 unsigned int length : 29;
22} __attribute__ ((packed));
23
24struct cons;
25
26union value_type {
27 int int_val;
28 float float_val;
29 struct cons *cons_val;
30 char *symbol_val; // interned
31 char *string_val;
32};
33
34struct value
35{
36 struct tag tag;
37 union value_type value;
38} __attribute__ ((packed));
39
40struct cons
41{
42 int magic;
43 int marked; // must be reserved
44 struct value car, cdr;
45};
46
47struct alloc_list
48{
49 int type;
50 union value_type data;
51 struct alloc_list *next, *prev;
52};
53
54struct istream
55{
56 void *data;
57
58 // These two return -1 on error
59 int (*peek) (struct istream *s);
60 int (*get) (struct istream *s);
61
62 int (*read) (struct istream *s, char *buffer, int size);
swissChilibed80922021-04-13 21:58:05 -070063
64 void (*showpos) (struct istream *s, FILE *out);
swissChili7a6f5eb2021-04-13 16:46:02 -070065};
66
swissChilibed80922021-04-13 21:58:05 -070067bool startswith (struct istream *s, char *pattern);
swissChili7a6f5eb2021-04-13 16:46:02 -070068
69bool readsym (struct istream *is, struct value *val);
70bool readstr (struct istream *is, struct value *val);
swissChilibed80922021-04-13 21:58:05 -070071bool readlist (struct istream *is, struct value *val);
swissChili7a6f5eb2021-04-13 16:46:02 -070072
swissChilibed80922021-04-13 21:58:05 -070073struct value strval (char *str);
swissChili7a6f5eb2021-04-13 16:46:02 -070074struct value cons (struct value car, struct value cdr);
75bool read1 (struct istream *is, struct value *val);
swissChilibed80922021-04-13 21:58:05 -070076struct value read (struct istream *is);
77struct value readn (struct istream *is);
78
79struct value car (struct value v);
80struct value cdr (struct value v);
81bool listp (struct value v);
82bool nilp (struct value v);
swissChilica107a02021-04-14 12:07:30 -070083int length (struct value v);
swissChili7a6f5eb2021-04-13 16:46:02 -070084
85void printval (struct value v, int depth);
86
87struct istream *new_stristream (char *str, int length);
88// same as above but null terminated
89struct istream *new_stristream_nt (char *str);
swissChilibed80922021-04-13 21:58:05 -070090void del_stristream (struct istream *stristream);
91
92void err (const char *msg);
93
94extern struct value nil;
swissChilica107a02021-04-14 12:07:30 -070095
96#define FOREACH(item, list) \
97 for ( ; listp (list); ) \
98 for ( struct value item = car (list), _foreach_current = list; \
99 !nilp (_foreach_current); \
100 _foreach_current = cdr (_foreach_current), \
101 item = car (_foreach_current) )