-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLisp.m
729 lines (673 loc) · 14.6 KB
/
Lisp.m
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
MODULE Lisp;
IMPORT
Conv, In, Out, Strings;
TYPE
LObjDesc = RECORD END;
LObj = POINTER TO LObjDesc;
SubrFnDesc = RECORD END;
SubrFn = POINTER TO SubrFnDesc;
NilDesc = RECORD (LObjDesc) END;
NumDesc = RECORD (LObjDesc) data: INTEGER END;
SymDesc = RECORD (LObjDesc) data: ARRAY 256 OF CHAR END;
ErrorDesc = RECORD (LObjDesc) data: ARRAY 256 OF CHAR END;
ConsDesc = RECORD (LObjDesc) car, cdr: LObj END;
SubrDesc = RECORD (LObjDesc) fn: SubrFn END;
ExprDesc = RECORD (LObjDesc) args, body, env: LObj END;
Nil = POINTER TO NilDesc;
Num = POINTER TO NumDesc;
Sym = POINTER TO SymDesc;
Error = POINTER TO ErrorDesc;
Cons = POINTER TO ConsDesc;
Subr = POINTER TO SubrDesc;
Expr = POINTER TO ExprDesc;
SubrCarDesc = RECORD (SubrFnDesc) END;
SubrCar = POINTER TO SubrCarDesc;
SubrCdrDesc = RECORD (SubrFnDesc) END;
SubrCdr = POINTER TO SubrCdrDesc;
SubrConsDesc = RECORD (SubrFnDesc) END;
SubrCons = POINTER TO SubrConsDesc;
SubrEqDesc = RECORD (SubrFnDesc) END;
SubrEq = POINTER TO SubrEqDesc;
SubrAtomDesc = RECORD (SubrFnDesc) END;
SubrAtom = POINTER TO SubrAtomDesc;
SubrNumberpDesc = RECORD (SubrFnDesc) END;
SubrNumberp = POINTER TO SubrNumberpDesc;
SubrSymbolpDesc = RECORD (SubrFnDesc) END;
SubrSymbolp = POINTER TO SubrSymbolpDesc;
CalcDesc = RECORD END;
Calc = POINTER TO CalcDesc;
CalcAddDesc = RECORD (CalcDesc) END;
CalcAdd = POINTER TO CalcAddDesc;
CalcMulDesc = RECORD (CalcDesc) END;
CalcMul = POINTER TO CalcMulDesc;
CalcSubDesc = RECORD (CalcDesc) END;
CalcSub = POINTER TO CalcSubDesc;
CalcDivDesc = RECORD (CalcDesc) END;
CalcDiv = POINTER TO CalcDivDesc;
CalcModDesc = RECORD (CalcDesc) END;
CalcMod = POINTER TO CalcModDesc;
SubrArithDesc = RECORD (SubrFnDesc) calc: Calc END;
SubrAddOrMulDesc = RECORD (SubrArithDesc) initVal: INTEGER END;
SubrAddOrMul = POINTER TO SubrAddOrMulDesc;
SubrSubOrDivOrModDesc = RECORD (SubrArithDesc) END;
SubrSubOrDivOrMod = POINTER TO SubrSubOrDivOrModDesc;
VAR
kLPar: CHAR;
kRPar: CHAR;
kQuote: CHAR;
kNil: LObj;
gEnv: LObj;
symTable: ARRAY 65536 OF Sym;
symTableSize: INTEGER;
symT: LObj;
symQuote: LObj;
symIf: LObj;
symLambda: LObj;
symDefun: LObj;
symSetq: LObj;
PROCEDURE MakeNum(n: INTEGER): LObj;
VAR
num: Num;
BEGIN
NEW(num);
num.data := n;
RETURN num
END MakeNum;
PROCEDURE MakeSym(s: ARRAY OF CHAR): LObj;
VAR
i: INTEGER;
sym: Sym;
BEGIN
i := 0;
LOOP
IF i >= symTableSize THEN
EXIT;
END;
IF s = symTable[i].data THEN
RETURN symTable[i]
END;
i := i + 1
END;
NEW(sym);
Strings.Append(s, sym.data);
symTable[symTableSize] := sym;
symTableSize := symTableSize + 1;
RETURN sym
END MakeSym;
PROCEDURE MakeError(s: ARRAY OF CHAR): LObj;
VAR
err: Error;
BEGIN
NEW(err);
Strings.Append(s, err.data);
RETURN err
END MakeError;
PROCEDURE MakeCons(a, d: LObj): LObj;
VAR
cons: Cons;
BEGIN
NEW(cons);
cons.car := a;
cons.cdr := d;
RETURN cons
END MakeCons;
PROCEDURE MakeSubr(fn: SubrFn): LObj;
VAR
subr: Subr;
BEGIN
NEW(subr);
subr.fn := fn;
RETURN subr
END MakeSubr;
PROCEDURE MakeExpr(args, env: LObj): LObj;
VAR
expr: Expr;
BEGIN
NEW(expr);
expr.args := SafeCar(args);
expr.body := SafeCdr(args);
expr.env := env;
RETURN expr
END MakeExpr;
PROCEDURE SafeCar(obj: LObj): LObj;
BEGIN
WITH
obj: Cons DO
RETURN obj.car
ELSE RETURN kNil
END
END SafeCar;
PROCEDURE SafeCdr(obj: LObj): LObj;
BEGIN
WITH
obj: Cons DO
RETURN obj.cdr
ELSE RETURN kNil
END
END SafeCdr;
PROCEDURE Nreverse(lst: LObj): LObj;
VAR
ret: LObj;
tmp: LObj;
BEGIN
ret := kNil;
LOOP
WITH
lst: Cons DO
tmp := lst.cdr;
lst.cdr := ret;
ret := lst;
ELSE
EXIT
END;
lst := tmp (* This statement can't be in WITH due to type checking. *)
END;
RETURN ret;
END Nreverse;
PROCEDURE Pairlis(lst1, lst2: LObj): LObj;
VAR
ret: LObj;
BEGIN
ret := kNil;
WHILE (lst1 IS Cons) & (lst2 IS Cons) DO
ret := MakeCons(MakeCons(SafeCar(lst1), SafeCar(lst2)), ret);
lst1 := SafeCdr(lst1);
lst2 := SafeCdr(lst2)
END;
RETURN Nreverse(ret)
END Pairlis;
PROCEDURE IsSpace(c: CHAR): BOOLEAN;
BEGIN
IF (c = CHR(9)) OR (c = CHR(10)) OR (c = CHR(13)) OR (c = CHR(32)) THEN
RETURN TRUE
END;
RETURN FALSE
END IsSpace;
PROCEDURE IsDelimiter(c: CHAR): BOOLEAN;
BEGIN
IF (c = kLPar) OR (c = kRPar) OR (c = kQuote) OR (IsSpace(c)) THEN
RETURN TRUE
END;
RETURN FALSE
END IsDelimiter;
PROCEDURE SkipSpaces(str: ARRAY OF CHAR; i: INTEGER): INTEGER;
BEGIN
WHILE i < Strings.Length(str) DO
IF ~IsSpace(str[i]) THEN
RETURN i
END;
i := i + 1
END;
RETURN i
END SkipSpaces;
PROCEDURE MakeNumOrSym(str: ARRAY OF CHAR): LObj;
VAR
i: INTEGER;
BEGIN
IF (Strings.Length(str) = 1) & (str[0] = "0") THEN
RETURN MakeNum(0)
END;
i := Conv.IntVal(str);
IF i = 0 THEN
RETURN MakeSym(str)
END;
RETURN MakeNum(i)
END MakeNumOrSym;
PROCEDURE ReadAtom(str: ARRAY OF CHAR; i: INTEGER; VAR obj: LObj): INTEGER;
VAR
j: INTEGER;
atom: ARRAY 256 OF CHAR;
BEGIN
j := i;
LOOP
IF (j >= Strings.Length(str)) OR (IsDelimiter(str[j])) THEN
EXIT;
END;
j := j + 1
END;
Strings.Extract(str, i, j - i, atom);
obj := MakeNumOrSym(atom);
RETURN j
END ReadAtom;
PROCEDURE Read(str: ARRAY OF CHAR; i: INTEGER; VAR obj: LObj): INTEGER;
VAR
j: INTEGER;
elm: LObj;
BEGIN
i := SkipSpaces(str, i);
IF i >= Strings.Length(str) THEN
obj:= MakeError("empty input");
RETURN i
ELSIF str[i] = kRPar THEN
obj := MakeError("invalid syntax");
RETURN Strings.Length(str)
ELSIF str[i] = kLPar THEN
RETURN ReadList(str, i + 1, obj)
ELSIF str[i] = kQuote THEN
j := Read(str, i + 1, elm);
obj := MakeCons(symQuote, MakeCons(elm, kNil));
RETURN j
ELSE
RETURN ReadAtom(str, i, obj)
END;
END Read;
PROCEDURE ReadList(str: ARRAY OF CHAR; i: INTEGER; VAR obj: LObj): INTEGER;
VAR
elm: LObj;
BEGIN
obj := kNil;
LOOP
i := SkipSpaces(str, i);
IF i >= Strings.Length(str) THEN
obj := MakeError("unfinished parenthesis");
RETURN i
ELSIF str[i] = kRPar THEN
EXIT;
END;
i := Read(str, i, elm);
IF elm IS Error THEN
obj := elm;
RETURN i
END;
obj := MakeCons(elm, obj);
END;
obj := Nreverse(obj);
RETURN i + 1;
END ReadList;
PROCEDURE PrintObj(obj: LObj; VAR str: ARRAY OF CHAR);
VAR
buf: ARRAY 32 OF CHAR;
BEGIN
WITH
obj: Nil DO
Strings.Append("nil", str);
| obj: Num DO
Conv.ConvInt(obj.data, buf);
Strings.Append(buf, str)
| obj: Sym DO
Strings.Append(obj.data, str)
| obj: Error DO
Strings.Append("<error: ", str);
Strings.Append(obj.data, str);
Strings.Append(">", str)
| obj: Cons DO
PrintList(obj, str);
| obj: Subr DO
Strings.Append("<subr>", str)
| obj: Expr DO
Strings.Append("<expr>", str)
ELSE
Strings.Append("<unknown>", str)
END
END PrintObj;
PROCEDURE PrintList(obj: LObj; VAR str: ARRAY OF CHAR);
VAR
first: BOOLEAN;
BEGIN
first := TRUE;
Strings.Append("(", str);
WHILE obj IS Cons DO
IF first THEN
first := FALSE
ELSE
Strings.Append(" ", str);
END;
PrintObj(SafeCar(obj), str);
obj := SafeCdr(obj);
END;
IF ~(obj = kNil) THEN
Strings.Append(" . ", str);
PrintObj(obj, str)
END;
Strings.Append(")", str)
END PrintList;
PROCEDURE FindVar(sym, env: LObj): LObj;
VAR
alist: LObj;
BEGIN
WHILE env IS Cons DO
alist := SafeCar(env);
WHILE alist IS Cons DO
IF SafeCar(SafeCar(alist)) = sym THEN
RETURN SafeCar(alist)
END;
alist := SafeCdr(alist)
END;
env := SafeCdr(env)
END;
RETURN kNil
END FindVar;
PROCEDURE AddToEnv(sym, val, env: LObj);
BEGIN
WITH
env: Cons DO
env.car := MakeCons(MakeCons(sym, val), env.car)
END
END AddToEnv;
PROCEDURE Eval(obj, env: LObj): LObj;
VAR
bind: LObj;
buf: ARRAY 256 OF CHAR;
op: LObj;
args: LObj;
c: LObj;
expr: LObj;
sym: LObj;
val: LObj;
BEGIN
IF (obj IS Nil) OR (obj IS Num) OR (obj IS Error) THEN
RETURN obj
ELSIF obj IS Sym THEN
bind := FindVar(obj, env);
WITH
bind: Cons DO
RETURN bind.cdr
ELSE
PrintObj(obj, buf);
Strings.Append(" has no value", buf);
RETURN MakeError(buf)
END
END;
op := SafeCar(obj);
args := SafeCdr(obj);
IF op = symQuote THEN
RETURN SafeCar(args)
ELSIF op = symIf THEN
c := Eval(SafeCar(args), env);
IF c IS Error THEN
RETURN c
ELSIF c = kNil THEN
RETURN Eval(SafeCar(SafeCdr(SafeCdr(args))), env)
ELSE
RETURN Eval(SafeCar(SafeCdr(args)), env)
END
ELSIF op = symLambda THEN
RETURN MakeExpr(args, env)
ELSIF op = symDefun THEN
expr := MakeExpr(SafeCdr(args), env);
sym := SafeCar(args);
AddToEnv(sym, expr, gEnv);
RETURN sym
ELSIF op = symSetq THEN
val := Eval(SafeCar(SafeCdr(args)), env);
sym := SafeCar(args);
bind := FindVar(sym, env);
WITH
bind: Cons DO
bind.cdr := val
ELSE
AddToEnv(sym, val, gEnv);
END;
RETURN val;
END;
RETURN Apply(Eval(op, env), Evlis(args, env))
END Eval;
PROCEDURE Evlis(lst, env: LObj): LObj;
VAR
ret: LObj;
elm: LObj;
BEGIN
ret := kNil;
WHILE lst IS Cons DO
elm := Eval(SafeCar(lst), env);
IF elm IS Error THEN
RETURN elm
END;
ret := MakeCons(elm, ret);
lst := SafeCdr(lst)
END;
RETURN Nreverse(ret)
END Evlis;
PROCEDURE Progn(body, env: LObj): LObj;
VAR
ret: LObj;
BEGIN
ret := kNil;
WHILE body IS Cons DO
ret := Eval(SafeCar(body), env);
body := SafeCdr(body)
END;
RETURN ret
END Progn;
PROCEDURE Apply(fn, args: LObj): LObj;
VAR
buf: ARRAY 256 OF CHAR;
BEGIN
IF fn IS Error THEN
RETURN fn
ELSIF args IS Error THEN
RETURN args
END;
WITH
fn: Subr DO
RETURN fn.fn.Call(args)
| fn: Expr DO
RETURN Progn(fn.body, MakeCons(Pairlis(fn.args, args), fn.env))
ELSE
PrintObj(fn, buf);
Strings.Append(" is not function", buf);
RETURN MakeError(buf)
END
END Apply;
PROCEDURE (f: SubrFn) Call(args: LObj): LObj;
BEGIN
RETURN MakeError("unknown subr")
END Call;
PROCEDURE (f: SubrCar) Call(args: LObj): LObj;
BEGIN
RETURN SafeCar(SafeCar(args))
END Call;
PROCEDURE (f: SubrCdr) Call(args: LObj): LObj;
BEGIN
RETURN SafeCdr(SafeCar(args))
END Call;
PROCEDURE (f: SubrCons) Call(args: LObj): LObj;
BEGIN
RETURN MakeCons(SafeCar(args), SafeCar(SafeCdr(args)))
END Call;
PROCEDURE (f: SubrEq) Call(args: LObj): LObj;
VAR
x: LObj;
y: LObj;
BEGIN
x := SafeCar(args);
y := SafeCar(SafeCdr(args));
WITH
x: Num DO
WITH
y: Num DO
IF x.data = y.data THEN
RETURN symT
ELSE
RETURN kNil
END
ELSE
RETURN kNil
END
ELSE
IF x = y THEN
RETURN symT
ELSE
RETURN kNil
END
END
END Call;
PROCEDURE (f: SubrAtom) Call(args: LObj): LObj;
BEGIN
IF SafeCar(args) IS Cons THEN
RETURN kNil
ELSE
RETURN symT
END
END Call;
PROCEDURE (f: SubrNumberp) Call(args: LObj): LObj;
BEGIN
IF SafeCar(args) IS Num THEN
RETURN symT
ELSE
RETURN kNil
END
END Call;
PROCEDURE (f: SubrSymbolp) Call(args: LObj): LObj;
BEGIN
IF SafeCar(args) IS Sym THEN
RETURN symT
ELSE
RETURN kNil
END
END Call;
PROCEDURE (f: Calc) Call(x, y: INTEGER): INTEGER;
BEGIN
RETURN 0; (* dummy *)
END Call;
PROCEDURE (f: CalcAdd) Call(x, y: INTEGER): INTEGER;
BEGIN
RETURN x + y;
END Call;
PROCEDURE (f: CalcMul) Call(x, y: INTEGER): INTEGER;
BEGIN
RETURN x * y;
END Call;
PROCEDURE (f: CalcSub) Call(x, y: INTEGER): INTEGER;
BEGIN
RETURN x - y;
END Call;
PROCEDURE (f: CalcDiv) Call(x, y: INTEGER): INTEGER;
BEGIN
RETURN x DIV y;
END Call;
PROCEDURE (f: CalcMod) Call(x, y: INTEGER): INTEGER;
BEGIN
RETURN x MOD y;
END Call;
PROCEDURE (f: SubrAddOrMul) Call(args: LObj): LObj;
VAR
ret: INTEGER;
elm: LObj;
BEGIN
ret := f.initVal;
WHILE args IS Cons DO
elm := SafeCar(args);
WITH
elm: Num DO
ret := f.calc.Call(ret, elm.data);
args := SafeCdr(args)
ELSE
RETURN MakeError("wrong type")
END
END;
RETURN MakeNum(ret)
END Call;
PROCEDURE (f: SubrSubOrDivOrMod) Call(args: LObj): LObj;
VAR
x: LObj;
y: LObj;
BEGIN
x := SafeCar(args);
y := SafeCar(SafeCdr(args));
WITH
x: Num DO
WITH
y: Num DO
RETURN MakeNum(f.calc.Call(x.data, y.data))
ELSE
RETURN MakeError("wrong type")
END
ELSE
RETURN MakeError("wrong type")
END
END Call;
PROCEDURE Init();
VAR
nil: Nil;
subrCar: SubrCar;
subrCdr: SubrCdr;
subrCons: SubrCons;
subrEq: SubrEq;
subrAtom: SubrAtom;
subrNumberp: SubrNumberp;
subrSymbolp: SubrSymbolp;
calcAdd: CalcAdd;
calcMul: CalcMul;
calcSub: CalcSub;
calcDiv: CalcDiv;
calcMod: CalcMod;
subrAdd: SubrAddOrMul;
subrMul: SubrAddOrMul;
subrSub: SubrSubOrDivOrMod;
subrDiv: SubrSubOrDivOrMod;
subrMod: SubrSubOrDivOrMod;
BEGIN
kLPar := "(";
kRPar := ")";
kQuote := "'";
NEW(nil);
kNil := nil;
symTableSize := 0;
symT := MakeSym("t");
symQuote := MakeSym("quote");
symIf := MakeSym("if");
symLambda := MakeSym("lambda");
symDefun := MakeSym("defun");
symSetq := MakeSym("setq");
gEnv := MakeCons(kNil, kNil);
AddToEnv(symT, symT, gEnv);
NEW(subrCar);
AddToEnv(MakeSym("car"), MakeSubr(subrCar), gEnv);
NEW(subrCdr);
AddToEnv(MakeSym("cdr"), MakeSubr(subrCdr), gEnv);
NEW(subrCons);
AddToEnv(MakeSym("cons"), MakeSubr(subrCons), gEnv);
NEW(subrEq);
AddToEnv(MakeSym("eq"), MakeSubr(subrEq), gEnv);
NEW(subrAtom);
AddToEnv(MakeSym("atom"), MakeSubr(subrAtom), gEnv);
NEW(subrNumberp);
AddToEnv(MakeSym("numberp"), MakeSubr(subrNumberp), gEnv);
NEW(subrSymbolp);
AddToEnv(MakeSym("symbolp"), MakeSubr(subrSymbolp), gEnv);
NEW(calcAdd);
NEW(calcMul);
NEW(calcSub);
NEW(calcDiv);
NEW(calcMod);
NEW(subrAdd);
subrAdd.initVal := 0;
subrAdd.calc := calcAdd;
AddToEnv(MakeSym("+"), MakeSubr(subrAdd), gEnv);
NEW(subrMul);
subrMul.initVal := 1;
subrMul.calc := calcMul;
AddToEnv(MakeSym("*"), MakeSubr(subrMul), gEnv);
NEW(subrSub);
subrSub.calc := calcSub;
AddToEnv(MakeSym("-"), MakeSubr(subrSub), gEnv);
NEW(subrDiv);
subrDiv.calc := calcDiv;
AddToEnv(MakeSym("/"), MakeSubr(subrDiv), gEnv);
NEW(subrMod);
subrMod.calc := calcMod;
AddToEnv(MakeSym("mod"), MakeSubr(subrMod), gEnv);
END Init;
PROCEDURE Main();
VAR
line: ARRAY 10240 OF CHAR;
obj: LObj;
i: INTEGER;
BEGIN
Init();
LOOP
Out.String("> ");
In.Line(line);
IF Strings.Length(line) = 0 THEN EXIT END;
i := Read(line, 0, obj);
obj := Eval(obj, gEnv);
line := "";
PrintObj(obj, line);
Out.String(line);
Out.Ln;
END
END Main;
BEGIN
Main();
END Lisp.