forked from schani/forthlisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisp.fs
488 lines (378 loc) · 10.1 KB
/
lisp.fs
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
\ -*- forth -*-
\ utilities
: string-new { a u -- a u }
a u allocate drop dup >r u cmove
r> u ;
: string>num ( a u -- n )
0 swap 0 ?do
10 * over i + c@ [char] 0 - +
loop
nip ;
\ symbol table
struct
cell% field symtab-namea
cell% field symtab-nameu
cell% field symtab-lisp
cell% field symtab-next
end-struct symtab
0 variable symtab-first
drop
: symtab-lookup { namea nameu -- }
symtab-first @
begin
dup 0<>
while
>r
r@ symtab-namea @ r@ symtab-nameu @ namea nameu compare
0= if
r> symtab-lisp @ unloop exit
endif
r> symtab-next @
repeat
drop 0 ;
: symtab-add { namea nameu lisp -- }
symtab %allocate throw
dup symtab-namea namea swap !
dup symtab-nameu nameu swap !
dup symtab-lisp lisp swap !
dup symtab-next symtab-first @ swap !
symtab-first ! ;
: symtab-save ( -- ptr )
symtab-first @ ;
: symtab-restore ( ptr -- )
symtab-first ! ;
\ lisp interpreter
0 constant lisp-pair-tag
1 constant lisp-number-tag
2 constant lisp-builtin-tag
3 constant lisp-symbol-tag
4 constant lisp-special-tag
5 constant lisp-compound-tag
6 constant lisp-max-tag
lisp-max-tag cells allocate throw constant eval-dispatch
lisp-max-tag cells allocate throw constant display-dispatch
lisp-max-tag cells allocate throw constant eq?-dispatch
struct
cell% field lisp-tag
end-struct lisp
struct
cell% field pair-tag
cell% field pair-car
cell% field pair-cdr
end-struct lisp-pair
struct
cell% field number-tag
cell% field number-num
end-struct lisp-number
struct
cell% field builtin-tag
cell% field builtin-xt
end-struct lisp-builtin
struct
cell% field symbol-tag
cell% field symbol-namea
cell% field symbol-nameu
end-struct lisp-symbol
struct
cell% field special-tag
cell% field special-xt
end-struct lisp-special
struct
cell% field compound-tag
cell% field compound-args
cell% field compound-body
end-struct lisp-compound
: cons { car cdr -- lisp }
lisp-pair %allocate throw
dup pair-tag lisp-pair-tag swap !
dup pair-car car swap !
dup pair-cdr cdr swap ! ;
: car ( pair -- lisp )
pair-car @ ;
: cdr ( pair -- lisp )
pair-cdr @ ;
: number { num -- lisp }
lisp-number %allocate throw
dup number-tag lisp-number-tag swap !
dup number-num num swap ! ;
: builtin { xt -- lisp }
lisp-builtin %allocate throw
dup builtin-tag lisp-builtin-tag swap !
dup builtin-xt xt swap ! ;
: symbol { namea nameu -- lisp }
lisp-symbol %allocate throw
dup symbol-tag lisp-symbol-tag swap !
dup symbol-namea namea swap !
dup symbol-nameu nameu swap ! ;
: symbol-new ( namea nameu -- lisp )
string-new symbol ;
: special { xt -- lisp }
lisp-special %allocate throw
dup special-tag lisp-special-tag swap !
dup special-xt xt swap ! ;
: compound { args body -- lisp }
lisp-compound %allocate throw
dup compound-tag lisp-compound-tag swap !
dup compound-args args swap !
dup compound-body body swap ! ;
: lisp-display ( lisp -- )
dup 0= if
drop [char] ( emit [char] ) emit
else
dup lisp-tag @ cells display-dispatch + @ execute
endif ;
: lisp-display-pair ( lisp -- )
[char] ( emit 32 emit
begin
dup car lisp-display 32 emit
cdr
dup 0=
until
drop
[char] ) emit ;
' lisp-display-pair display-dispatch lisp-pair-tag cells + !
: lisp-display-number ( lisp -- )
number-num @ . ;
' lisp-display-number display-dispatch lisp-number-tag cells + !
: lisp-display-builtin ( lisp -- )
[char] $ emit special-xt @ . ;
' lisp-display-builtin display-dispatch lisp-builtin-tag cells + !
: lisp-display-symbol { lisp -- }
lisp symbol-namea @ lisp symbol-nameu @ type ;
' lisp-display-symbol display-dispatch lisp-symbol-tag cells + !
: lisp-display-special ( lisp -- )
[char] # emit special-xt @ . ;
' lisp-display-special display-dispatch lisp-special-tag cells + !
: lisp-display-compound ( lisp -- )
[char] & emit compound-body @ . ;
' lisp-display-compound display-dispatch lisp-compound-tag cells + !
: lisp-eval ( lisp -- lisp )
dup 0<> if
dup lisp-tag @ cells eval-dispatch + @ execute
endif ;
: lisp-eval-list recursive ( lisp -- lisp )
dup 0<> if
dup car lisp-eval swap cdr lisp-eval-list cons
endif ;
: lisp-bind-var ( name value -- )
>r dup symbol-namea @ swap symbol-nameu @ r> symtab-add ;
: lisp-bind-vars ( names values -- )
swap
begin
dup 0<>
while
2dup car swap car lisp-bind-var
cdr swap cdr swap
repeat
2drop ;
: lisp-apply-compound ( func args -- lisp )
symtab-save >r
over compound-args @ swap lisp-bind-vars
compound-body @ lisp-eval
r> symtab-restore ;
: lisp-apply ( func args -- lisp )
>r dup lisp-tag @ lisp-builtin-tag = if
r> swap builtin-xt @ execute
else
r> lisp-apply-compound
endif ;
: lisp-eval-pair ( lisp -- lisp )
>r
r@ car lisp-eval
dup lisp-tag @ lisp-special-tag = if
r> cdr swap special-xt @ execute
else
r> cdr lisp-eval-list lisp-apply
endif ;
' lisp-eval-pair eval-dispatch lisp-pair-tag cells + !
: lisp-eval-number ( lisp -- lisp ) ;
' lisp-eval-number eval-dispatch lisp-number-tag cells + !
: lisp-eval-builtin ( lisp -- lisp ) ;
' lisp-eval-builtin eval-dispatch lisp-builtin-tag cells + !
: lisp-eval-symbol { lisp -- lisp }
lisp symbol-namea @ lisp symbol-nameu @ symtab-lookup ;
' lisp-eval-symbol eval-dispatch lisp-symbol-tag cells + !
: lisp-eval-special ( lisp -- lisp ) ;
' lisp-eval-special eval-dispatch lisp-special-tag cells + !
: lisp-eval-compound ( lisp -- lisp ) ;
' lisp-eval-compound eval-dispatch lisp-compound-tag cells + !
\ the reader
: lisp-read-char ( e a -- e a c )
2dup <= if
0
else
dup c@ swap 1+ swap
endif ;
: lisp-unread-char ( e a -- e a )
1- ;
: lisp-is-ws ( c -- flag )
dup 10 = swap dup 13 = swap dup 9 = swap 32 = or or or ;
: lisp-skip-ws ( e a -- e a )
lisp-read-char
begin
dup 0<> over lisp-is-ws and
while
drop lisp-read-char
repeat
0<> if
lisp-unread-char
endif ;
128 allocate throw constant token-buffer
: lisp-read-token ( e a -- e a a u )
lisp-skip-ws
0 >r
lisp-read-char
begin
dup [char] ) <> over 0<> and over lisp-is-ws 0= and
while
token-buffer r@ + c! r> 1+ >r lisp-read-char
repeat
0<> if
lisp-unread-char
endif
token-buffer r> ;
defer lisp-read-lisp
: lisp-read-list recursive ( e a -- e a lisp )
lisp-skip-ws lisp-read-char
dup [char] ) = swap 0 = or if
0
else
lisp-unread-char lisp-read-lisp >r lisp-read-list r> swap cons
endif ;
: lisp-read-number ( e a -- e a lisp )
lisp-read-token string>num number ;
: lisp-read-symbol ( e a -- e a lisp )
lisp-read-token string-new symbol ;
: _lisp-read-lisp ( e a -- e a lisp )
lisp-skip-ws lisp-read-char
dup 0= if
drop 0
else
dup [char] ( = if
drop lisp-read-list
else
dup [char] 0 >= swap [char] 9 <= and if
lisp-unread-char lisp-read-number
else
lisp-unread-char lisp-read-symbol
endif
endif
endif ;
' _lisp-read-lisp is lisp-read-lisp
: lisp-load-from-string ( a u -- lisp )
over + swap 0 >r
begin
lisp-skip-ws 2dup >
while
r> drop lisp-read-lisp lisp-eval >r
repeat
2drop r> ;
8192 allocate throw constant read-buffer
: lisp-load-from-file ( a u -- lisp )
r/o open-file
0<> if
drop 0
else
>r read-buffer 8192 r@ read-file
0<> if
r> 2drop 0
else
r> close-file drop
read-buffer swap lisp-load-from-string
endif
endif ;
\ specials
: lisp-special-quote ( lisp -- lisp )
car ;
s" quote" string-new ' lisp-special-quote special symtab-add
: lisp-special-lambda ( lisp -- lisp )
dup car swap cdr car compound ;
s" lambda" string-new ' lisp-special-lambda special symtab-add
: lisp-special-define ( lisp -- lisp )
dup car swap cdr car lisp-eval lisp-bind-var 0 ;
s" define" string-new ' lisp-special-define special symtab-add
0 constant lisp-false
0 0 cons constant lisp-true
s" t" string-new lisp-true symtab-add
: lisp-special-cond recursive ( lisp -- lisp )
dup car car lisp-eval 0<> if
car cdr car lisp-eval
else
cdr dup 0<> if
lisp-special-cond
endif
endif ;
s" cond" string-new ' lisp-special-cond special symtab-add
\ builtins
: lisp-builtin-+ ( lisp -- lisp )
0 swap
begin
dup 0<>
while
dup car number-num @ rot + swap cdr
repeat
drop number ;
s" +" string-new ' lisp-builtin-+ builtin symtab-add
: lisp-builtin-- ( lisp -- lisp )
dup car number-num @ swap cdr dup 0= if
drop negate number
else
swap
begin
over car number-num @ - swap cdr swap
over 0=
until
nip number
endif ;
s" -" string-new ' lisp-builtin-- builtin symtab-add
: lisp-builtin-* ( lisp -- lisp )
1 swap
begin
dup 0<>
while
dup car number-num @ rot * swap cdr
repeat
drop number ;
s" *" string-new ' lisp-builtin-* builtin symtab-add
: lisp-builtin-cons ( lisp -- lisp )
dup car swap cdr car cons ;
s" cons" string-new ' lisp-builtin-cons builtin symtab-add
: lisp-builtin-car ( lisp -- lisp )
car car ;
s" car" string-new ' lisp-builtin-car builtin symtab-add
: lisp-builtin-cdr ( lisp -- lisp )
car cdr ;
s" cdr" string-new ' lisp-builtin-cdr builtin symtab-add
: lisp-builtin-eq? ( lisp -- lisp )
dup car swap cdr car 2dup = if
2drop lisp-true
else
2dup lisp-tag @ swap lisp-tag @ <> if
2drop lisp-false
else
dup lisp-tag @ cells eq?-dispatch + @ execute
endif
endif ;
s" eq?" string-new ' lisp-builtin-eq? builtin symtab-add
' lisp-false eq?-dispatch lisp-pair-tag cells + !
: lisp-eq?-number ( lisp lisp -- lisp )
number-num @ swap number-num @ = if
lisp-true
else
lisp-false
endif ;
' lisp-eq?-number eq?-dispatch lisp-number-tag cells + !
' lisp-false eq?-dispatch lisp-builtin-tag cells + !
: lisp-eq?-symbol { lisp1 lisp2 -- lisp }
lisp1 symbol-namea @ lisp1 symbol-nameu @
lisp2 symbol-namea @ lisp2 symbol-nameu @
compare 0= if
lisp-true
else
lisp-false
endif ;
' lisp-eq?-symbol eq?-dispatch lisp-symbol-tag cells + !
' lisp-false eq?-dispatch lisp-compound-tag cells + !
: lisp-builtin-display ( lisp -- lisp )
car lisp-display 0 ;
s" display" string-new ' lisp-builtin-display builtin symtab-add