-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlispsyntax.pas
309 lines (259 loc) · 5.3 KB
/
lispsyntax.pas
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
{$ifdef Interface}
{ S-Exprs }
function LispRead(Port: LV): LV;
function LispReadString(S: string): LV;
procedure LispWrite(X, Port: LV);
procedure LispDisplay(X, Port: LV);
{$else}
const
LispWhitespace = [#9, #10, #13, #32];
OverChars = LispWhitespace + [']', ')'];
{ Reading S-Exprs }
function ReadOver(var C: Char; Port: LV): Boolean;
begin
if LispEOF(Port) then
begin
Result := True;
end
else
begin
C := LispPeekChar(Port);
if C in OverChars then
begin
Result := True;
end
else
begin
LispNextChar(Port);
Result := False;
end;
end;
end;
function ReadSymbol(First: string; Port: LV): LV;
var
Name: string;
C: Char;
begin
Name := First;
while not ReadOver(C, Port) do
begin
Name := Name + C;
end;
Result := LispSymbol(Name);
end;
function ReadNumber(First: string; Port: LV): LV;
var
Str: string;
C: Char;
Fixnum: Boolean;
begin
Str := First;
Fixnum := True;
while not ReadOver(C, Port) do
begin
Str := Str + C;
if C = '.' then
begin
if Fixnum then
begin
Fixnum := False;
end
else
begin
Result := ReadSymbol(Str, Port);
exit;
end;
end
else if not (C in ['0' .. '9']) then
begin
Result := ReadSymbol(Str, Port);
exit;
end;
end;
if Fixnum then
begin
Result := LispNumber(StrToInt(Str));
end
else
begin
Result := LispNumber(StrToFloat(Str));
end;
end;
function ReadString(Port: LV): LV;
var
Str: string;
C: Char;
begin
Str := '';
C := LispReadChar(Port);
while not (C = '"') do
begin
Str := Str + C;
C := LispReadChar(Port);
end;
Result := LispString(Str);
end;
function ReadHash(Port: LV): LV;
var
C: Char;
begin
C := LispReadChar(Port);
case C of
't': Result := LispTrue;
'f': Result := LispFalse;
'v': Result := LispVoid;
'\': Result := LispChar(LispReadChar(Port));
else raise ELispError.Create('Unknown use of hash syntax', nil);
end;
end;
function ReadComment(Port: LV): LV;
var
C: Char;
begin
if LispEOF(Port) then
begin
Result := LispEOFObject;
exit;
end;
C := LispPeekChar(Port);
while not ((C = #10) or LispEOF(Port)) do
begin
C := LispReadChar(Port);
end;
if LispEOF(Port) then
begin
Result := LispEOFObject;
exit;
end;
Result := LispRead(Port);
end;
function ReadWithWrapper(Name: string; Port: LV): LV;
var
Rest: LV;
begin
Rest := LispCons(LispRead(Port), LispEmpty);
Result := LispCons(LispSymbol(Name), Rest);
end;
function ReadList(EndC: Char; Port: LV): LV; forward;
function ReadWithChar(C: Char; Port: LV): LV;
begin
if LispEOF(Port) then
begin
Result := LispEOFObject;
exit;
end;
while C in LispWhitespace do
begin
C := LispReadChar(Port);
if LispEOF(Port) then
begin
Result := LispEOFObject;
exit;
end;
end;
case C of
'0' .. '9': Result := ReadNumber(C, Port);
'"': Result := ReadString(Port);
'#': Result := ReadHash(Port);
'(': Result := ReadList(')', Port);
'[': Result := ReadList(']', Port);
']', ')': raise ELispError.Create('unexpected list end', nil);
'''': Result := ReadWithWrapper('quote', Port);
'`': Result := ReadWithWrapper('quasiquote', Port);
';': Result := ReadComment(Port);
',': if LispPeekChar(Port) = '@' then
begin
LispNextChar(Port);
Result := ReadWithWrapper('unquote-splicing', Port);
end
else
begin
Result := ReadWithWrapper('unquote', Port);
end;
else Result := ReadSymbol(C, Port);
end;
end;
function ReadList(EndC: Char; Port: LV): LV;
var
Cur, Next: TLispPair;
C: Char;
ExpectingCons: Boolean;
begin
Result := LispEmpty;
ExpectingCons := False;
while True do
begin
repeat
if LispEOF(Port) then
begin
raise ELispError.Create('Unterminated list', nil);
end;
C := LispReadChar(Port);
until not (C in LispWhitespace);
if C = EndC then
begin
exit;
end
else
if C = '.' then
begin
if (Result = LispEmpty) or ExpectingCons then
begin
raise ELispError.Create('Invalid cons', nil);
end
else
begin
Cur.D := LispRead(Port);
ExpectingCons := True;
end;
end
else
begin
if ExpectingCons then
begin
raise ELispError.Create('Invalid cons', nil);
end
else if Result = LispEmpty then
begin
Next := LispCons(ReadWithChar(C, Port), LispEmpty);
Cur := Next;
Result := Next;
end
else
begin
Next := LispCons(ReadWithChar(C, Port), LispEmpty);
Cur.D := Next;
Cur := Next;
end;
end;
end;
end;
function LispRead(Port: LV): LV;
begin
if LispEOF(Port) then
begin
Result := LispEOFObject;
exit;
end;
Result := ReadWithChar(LispReadChar(Port), Port);
end;
function LispReadString(S: string): LV;
begin
Result := LispRead(LispInputString(S));
end;
{ Writing S-Exprs }
procedure LispWrite(X, Port: LV);
begin
if X <> LispVoid then
begin
LispWriteString(LispToWrite(X), Port);
end;
end;
procedure LispDisplay(X, Port: LV);
begin
if X <> LispVoid then
begin
LispWriteString(LispToDisplay(X), Port);
end;
end;
{$endif}