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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
const std = @import("std");
const gc = @import("gc.zig");
const value = @import("value.zig");
const Value = value.Value;
const Next = enum {
start,
datum,
hash_end,
rune_datum_end,
quote_end,
list,
list_end,
done,
};
const State = struct {
alloc: std.mem.Allocator,
input: []const u8,
pos: usize = 0,
mode: enum { code, data } = .code,
next: Next = .start,
parent: ?*State = null,
last_rune: ?Value = null,
list_tail: ?Value = null,
retval: Value = value.eof.eof,
fn eof(self: *State) bool {
return self.pos >= self.input.len;
}
fn peek(self: *State) u8 {
return self.input[self.pos];
}
fn getc(self: *State) u8 {
const c = self.peek();
self.pos += 1;
return c;
}
fn isFinalNull(self: *State) bool {
return self.peek() == 0 and self.pos == self.input.len - 1;
}
fn newChild(self: *State, next: Next) *State {
const s = self.alloc.create(State) catch @panic("OOM");
s.* = State{ .alloc = self.alloc, .input = self.input };
s.pos = self.pos;
s.mode = self.mode;
s.next = next;
s.parent = self;
return s;
}
fn setReturn(self: *State, val: Value) *State {
self.retval = val;
self.next = .done;
return self;
}
fn finish(self: *State) ?*State {
if (self.parent) |p| {
p.retval = self.retval;
p.pos = self.pos;
p.alloc.destroy(self);
return p;
} else {
return null;
}
}
};
pub fn read(input: []const u8) Value {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
var top = State{ .alloc = gpa.allocator(), .input = input };
var s = ⊤
while (true) s = switch (s.next) {
.start => start(s),
.datum => datum(s),
.hash_end => hashEnd(s),
.rune_datum_end => runeDatumEnd(s),
.quote_end => quoteEnd(s),
.list => list(s),
.list_end => list(s),
.done => s.finish() orelse break,
};
if (s.eof() or s.isFinalNull()) {
return s.retval;
} else {
@panic("unconsumed input");
}
}
fn start(s: *State) *State {
while (true) {
if (s.eof()) {
s.next = .done;
return s;
}
const c = s.getc();
if (isWhitespace(c)) {
continue;
}
return switch (c) {
// whitespace checked above
0...31, 127...255 => err(s, "invalid character"),
')', ']', '}' => err(s, "unexpected closing bracket"),
';' => semi(s),
else => ret: {
// backtrack; let other function handle it
s.pos -= 1;
break :ret datum(s);
},
};
}
}
fn semi(s: *State) *State {
while (true) {
if (s.eof()) {
s.next = .done;
return s;
}
const c = s.getc();
if (c == '\n') {
break;
}
}
return s;
}
fn datum(s: *State) *State {
const c = s.getc();
if (isWhitespace(c)) {
return err(s, "expected datum, got whitespace");
}
return switch (c) {
// whitespace checked above
0...31, 127...255 => err(s, "invalid character"),
')', ']', '}' => err(s, "unexpected closing bracket"),
';' => err(s, "expected datum, got semicolon"),
'"' => string(s),
'#' => hash(s),
'\'' => quote(s),
'(' => list(s),
'+' => plus(s),
',' => comma(s),
'.' => dot(s),
'0'...'9' => number(s),
'[' => square(s),
'`' => backtick(s),
'{' => brace(s),
else => symbol(s),
};
}
fn isWhitespace(c: u8) bool {
return switch (c) {
'\t', '\n', ' ' => true,
else => false,
};
}
// Whitespace, semicolon, and closing brackets of any kind
fn isEndDelimiter(c: u8) bool {
return switch (c) {
'\t', '\n', ' ', ';' => true,
')', ']', '}' => true,
else => false,
};
}
fn string(s: *State) *State {
const str = readString(s) catch return err(s, "unclosed string");
if (s.mode == .code) {
// "foo bar" => (#string . "foo bar")
const rune = value.rune.pack("string");
const pair = value.pair.cons(rune, str);
return s.setReturn(pair);
} else {
return s.setReturn(str);
}
}
const StringReadError = enum { UnclosedString };
fn readString(s: *State) error{UnclosedString}!Value {
return try tryReadSstr(s) orelse readLongString(s);
}
fn tryReadSstr(s: *State) error{UnclosedString}!?Value {
// We will reset to this position if we fail.
const start_pos = s.pos;
var buf: [6]u8 = undefined;
var i: usize = 0;
while (!s.eof()) {
const c = s.getc();
if (c == '"') {
// ok, return what we accumulated
return value.sstr.pack(buf[0..i]);
}
if (i == 6) {
// failed; reset and bail out
s.pos = start_pos;
return null;
}
// ok, save this byte and go on
buf[i] = c;
i += 1;
}
return error.UnclosedString;
}
fn readLongString(s: *State) Value {
_ = s;
@panic("not implemented");
}
fn hash(s: *State) *State {
if (isWhitespace(s.peek())) {
return err(s, "whitespace after hash sign");
}
// is it a datum comment?
if (s.peek() == ';') {
// consume semicolon
_ = s.getc();
// Just ignore value and return to starting state after reading it.
s.next = .start;
} else {
s.next = .hash_end;
}
// No whitespace or anything; hash must be immediately followed by datum,
// including if it's a datum comment. Note that if it's actually a rune
// we're reading, like #foo, we abuse our ability to reading an sstr here
// and later turn it into a rune instead, since they're the same length.
return s.newChild(.datum);
}
fn hashEnd(s: *State) *State {
// It's not actually a sstr but a rune, like: #foo or #foo(...)
if (value.sstr.check(s.retval)) {
return hashRuneEnd(s);
}
// Hash followed by an actual datum; becomes a (#hash ...) invocation:
//
// #(...) -> (#hash . (...))
//
// #"..." -> (#hash . "...")
//
// But data mode doesn't allow that.
if (s.mode == .data) {
return err(s, "invalid use of hash in data mode");
}
// Also, bare long strings are not OK here; too similar to a rune.
if (value.ptr.checkZisp(s.retval, .string)) {
return err(s, "long string after hash sign");
}
return s.setReturn(value.pair.cons(
value.rune.pack("hash"),
s.retval,
));
}
// Note: Can only come here from hashEnd().
fn hashRuneEnd(s: *State) *State {
// Convert the fake sstr that was meant to be a rune.
const rune = value.rune.make(s.retval);
// Maybe it's a stand-alone rune, like: #foo
if (isEndDelimiter(s.peek())) {
// Which is only allowed in data mode.
if (s.mode == .code) {
return err(s, "bare runes not allowed in code");
} else {
return s.setReturn(rune);
}
}
// Otherwise, it's followed by a datum, like: #foo(...)
// Which is only allowed in code mode.
if (s.mode == .data) {
return err(s, "invalid use of hash in data mode");
} else {
s.last_rune = rune;
s.next = .rune_datum_end;
return s.newChild(.datum);
}
}
fn runeDatumEnd(s: *State) *State {
if (s.last_rune) |rune| {
return s.setReturn(value.pair.cons(rune, s.retval));
} else {
unreachable;
}
}
fn quote(s: *State) *State {
// Allowed in Scheme, but why? Not in Zisp.
if (isWhitespace(s.peek())) {
return err(s, "whitespace after apostrophe");
}
s.next = .quote_end;
const c = s.newChild(.datum);
c.mode = .data;
return c;
}
fn quoteEnd(s: *State) *State {
return s.setReturn(value.pair.cons(
value.rune.pack("quote"),
s.retval,
));
}
fn list(s: *State) *State {
return s;
}
fn plus(s: *State) *State {
return s;
}
fn comma(s: *State) *State {
return s;
}
fn dot(s: *State) *State {
return s;
}
fn number(s: *State) *State {
return s;
}
fn square(s: *State) *State {
return s;
}
fn backtick(s: *State) *State {
return s;
}
fn brace(s: *State) *State {
return s;
}
fn symbol(s: *State) *State {
return s;
}
fn err(s: *State, msg: []const u8) *State {
_ = s;
std.debug.print("{s}\n", .{msg});
@panic("reader error");
}
|