/
reader.c
292 lines (249 loc) · 6.35 KB
/
reader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "reader.h"
#include "debug.h"
#include "exception.h"
#include "symtab.h"
#include "types.h"
#include "util.h"
#include "valuestack.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
const char* cursor;
StackIndex tokenIndex;
int eof;
} Tokeniser;
static void skipComment(Tokeniser* t)
{
const char* p;
for (p = 0; *p != 0 && *p != '\n'; p++) {
}
// The cursor is now either pointing at the trailing zero, or at the
// newline (which will get skipped as whitespace).
t->cursor = p;
}
static void skipSpace(Tokeniser* t)
{
while (1) {
char c = *t->cursor;
if (c == ';') {
skipComment(t);
}
else if (isspace(c)) {
t->cursor++;
}
else {
return;
}
}
}
static int isdelim(char c)
{
switch (c) {
case '(': case ')': case '\'': case '"':
return 1;
default:
return 0;
}
}
static int tokeniser_eof(Tokeniser* t)
{
return t->eof;
}
typedef void (CharSink)(char c, void* context);
static const char* stringEscape(const char* s, CharSink* emit, void* context)
{
for (const char* p = s; ; p++) {
char c = *p;
switch (*p) {
case 0:
case '"':
// Return p still pointing to the last char, so the caller can
// decide what to do.
return p;
case '\\':
p++;
switch (c = *p) {
case 'n': emit('\n', context); break;
case 't': emit('\t', context); break;
case '\\': emit('\\', context); break;
default: emit(c, context); break;
}
break;
default:
emit(c, context);
break;
}
}
}
static void countChars(char c, void* context)
{
int* count = (void*) context;
(*count)++;
}
static void copyChars(char c, void* context)
{
char** dest = (char**) context;
**dest = c;
(*dest)++;
}
static Pointer makeToken(const char* start, int length)
{
Pointer ptr = symbol_alloc(length);
char* dest = (char*) symbol_get(ptr);
memcpy(dest, start, length);
dest[length] = 0;
return ptr;
}
static void tokeniser_advance(Tokeniser* t)
{
ASSERT(!tokeniser_eof(t), "next() on empty tokeniser");
SET(t->tokenIndex, nil_make());
skipSpace(t);
if (*t->cursor == 0) {
t->eof = 1;
return;
}
const char* punctChars = "()'";
for (int i = 0; punctChars[i] != 0; i++) {
if (*t->cursor == punctChars[i]) {
t->cursor++;
SET(t->tokenIndex, makeToken(&punctChars[i], 1));
return;
}
}
if (*t->cursor == '"') {
// Find out how long the escaped string is, and check it was terminated
// properly.
int length = 0;
t->cursor++; // advance past the initial "
const char* end = stringEscape(t->cursor, countChars, &length);
if (*end != '"') {
THROW("Expected \", got EOF");
}
// Now allocate a string and copy it in.
Pointer ptr = string_alloc(length);
char* dest = (char*) string_get(ptr);
stringEscape(t->cursor, copyChars, &dest);
*dest = 0;
// Advance the cursor past the string.
t->cursor = end + 1;
SET(t->tokenIndex, ptr);
}
else {
// Scan out a token
const char* start = t->cursor;
while (1) {
char c = *t->cursor;
if ((c == 0) || isspace(c) || isdelim(c)) {
int length = t->cursor - start;
SET(t->tokenIndex, makeToken(start, length));
break;
}
// Advance the cursor now, not earlier.
t->cursor++;
}
}
}
static void tokeniser_next(Tokeniser* t)
{
tokeniser_advance(t);
}
static void tokeniser_init(Tokeniser* t, const char* input)
{
t->cursor = input;
t->eof = 0;
t->tokenIndex = RESERVE();
tokeniser_advance(t);
}
static Pointer tokeniser_token(Tokeniser* t)
{
return GET(t->tokenIndex);
}
static void tokeniser_deinit(Tokeniser* t)
{
DROP(1);
}
static int readInteger(const char* t, int* value)
{
int sign = 1;
switch (*t) {
case '+': t++; break;
case '-': sign = -1; t++; break;
default: break;
}
if (*t == 0)
return 0;
*value = 0;
for ( ; *t != 0; t++) {
if (isdigit(*t)) {
*value = (*value * 10) + (*t - '0');
}
else {
return 0;
}
}
*value *= sign;
return 1;
}
static Pointer readForm(Tokeniser* t);
static Pointer readList(Tokeniser* t)
{
if (tokeniser_eof(t)) {
THROW("Expected ), got EOF");
}
Pointer token = tokeniser_token(t);
if ((token.type == Type_symbol) && util_streq(symbol_get(token), ")")) {
tokeniser_next(t);
return nil_make();
}
StackIndex carIndex = PUSH(readForm(t));
StackIndex cdrIndex = PUSH(readList(t));
Pointer ret = pair_make(carIndex, cdrIndex);
DROP(2);
return ret;
}
static Pointer readForm(Tokeniser* t)
{
if (tokeniser_eof(t)) {
return nil_make();
}
Pointer ret = tokeniser_token(t);
// If ret is a string, it's ready to return
if (ret.type == Type_symbol) {
const char* token = symbol_get(ret);
int intValue;
if (util_streq(token, "(")) {
tokeniser_next(t); // consume the '(', read the list and
return readList(t); // return immediately to avoid hitting the
// tokeniser_next() below
}
else if (util_streq(token, "nil")) {
ret = nil_make();
}
else if (util_streq(token, "true")) {
ret = boolean_make(1);
}
else if (util_streq(token, "false")) {
ret = boolean_make(0);
}
else if (readInteger(token, &intValue)) {
ret = integer_make(intValue);
}
else {
// Insert the symbol into the symbol table. We may or may not get
// the same symbol back.
ret = symtab_insert(ret);
}
}
PUSH(ret);
tokeniser_next(t);
return POP();
}
Pointer readLine(const char* input)
{
Tokeniser t;
tokeniser_init(&t, input);
Pointer ptr = readForm(&t); tokeniser_deinit(&t);
return ptr;
}