-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval_cont.lua
1110 lines (664 loc) · 20.2 KB
/
eval_cont.lua
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
do
local lisp = require "lisp"
local common = require "eval_common"
local old_error = error
local old_setmetatable = setmetatable
local old_math = math
local old_type = type
local print_lua = print
_ENV = {}
_ENV.lisp = lisp
_ENV.math = old_math
_ENV.error = old_error
_ENV.eval_exp = eval_exp
_ENV.setmetatable = old_setmetatable
_ENV.type = old_type
_ENV.print_lua = print_lua
_ENV.common = common
end
Object = {}
Object.__index = Object
function Object:new(o)
local result = o or {}
result.__index = result
setmetatable(result, self)
return result
end
--------------------------------------------------------------------------------------------
------------------------------ Environment ------------------------------
Environment = {}
Environment.__index = Environment
local nil_val = { __tostring = function(v) return 'Nil' end }
setmetatable(nil_val, nil_val)
function Environment:new(enclosing, params, args)
local result = o or {}
result.enclosing = enclosing
result.binds = {}
local function addProp(nameList, valueList)
if nameList ~= nil and nameList ~= lisp.empty_list then
local value = lisp.car(valueList) or nil_val
result.binds[lisp.car(nameList)] = value
return addProp(lisp.cdr(nameList), lisp.cdr(valueList))
end
end
addProp(params, args)
setmetatable(result, self)
return result
end
function Environment:lookup(name, k)
if self.binds[name] ~= nil then
return k:resume(self.binds[name])
elseif self.enclosing then
return self.enclosing:lookup(name, k)
else
error("Unknown varaible: " .. name)
end
end
function Environment:update(name, val, k)
if self.binds[name] ~= nil then
-- val could be boolean 'false'
val = ((val == nil) and nil_val) or val
self.binds[name] = val
return k:resume(val)
elseif self.enclosing then
return self.enclosing:update(name, val, k)
else
error("Update unknown varaible: " .. name)
end
end
function Environment:define(var, val, k)
if self.binds[name] then
error("Redefine variable: " .. var)
else
-- val could be boolean 'false'
val = ((val == nil) and nil_val) or val
self.binds[var] = val
return k:resume(val)
end
end
function Environment.initEnv()
local e = Environment:new()
e.binds = LispPrimitive.primitives
return e
end
------------------------------ Environment ------------------------------
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
------------------------------ Function ------------------------------
LispFunction = {}
LispFunction.__index = LispFunction
LispFunction.__tostring = function(proc)
return "[Procedure: " .. lisp.list_tostring(proc.params) .. " " ..
lisp.list_tostring(proc.body) .. "]"
end
function LispFunction:new(params, body, env)
local result = {}
result.params = params
result.body = body
result.env = env
result.name = name
setmetatable(result, self)
return result
end
function LispFunction:invoke(args, env, k)
local extended_env = Environment:new(self.env, self.params, args)
return eval_begin(self.body, extended_env, k)
end
LispPrimitive = {}
LispPrimitive.__index = LispPrimitive
LispPrimitive.__tostring = function(proc)
return "[Primitive: " .. proc.name .. "]"
end
function LispPrimitive:new(primitive, name)
local result = {}
result.primitive = primitive
result.name = name
result.__index = result
setmetatable(result, self)
return result
end
function LispPrimitive:invoke(args, env, k)
local r = self.primitive(lisp.list_unpack(args))
return k:resume(r)
end
LispCallCCPrimitive = LispPrimitive:new()
function LispCallCCPrimitive:new()
local result = LispPrimitive:new(nil, "call/cc")
setmetatable(result, self)
return result
end
function LispCallCCPrimitive:invoke(args, env, k)
local func = lisp.car(args)
return func:invoke(lisp.list(k), env, k)
end
LispEvalPrimitive = LispPrimitive:new()
function LispEvalPrimitive:new()
local result = LispPrimitive:new(nil, "eval")
setmetatable(result, self)
return result
end
function LispEvalPrimitive:invoke(args, env, k)
return eval_begin(lisp.car(args), env, k)
end
LispPrimitive.primitives =
{
["car"] = LispPrimitive:new(lisp.car, "car"),
["cdr"] = LispPrimitive:new(lisp.cdr, "cdr"),
["cons"] = LispPrimitive:new(lisp.cons, "cons"),
["length"] = LispPrimitive:new(lisp.length, "length"),
["print"] = LispPrimitive:new(print_lua, "print"),
["display"] = LispPrimitive:new(print_lua, "print"),
["list"] = LispPrimitive:new(lisp.list, "list"),
["atom?"] = LispPrimitive:new(common.primitive_atom, "atom?"),
["pair?"] = LispPrimitive:new(lisp.is_pair, "pair?"),
["null?"] = LispPrimitive:new(common.primitive_null, "null?"),
["call/cc"] = LispCallCCPrimitive:new(),
["+"] = LispPrimitive:new(common.primitive_algebra('+'), "+"),
["-"] = LispPrimitive:new(common.primitive_algebra('-'), "-"),
["*"] = LispPrimitive:new(common.primitive_algebra('*'), "*"),
["/"] = LispPrimitive:new(common.primitive_algebra('/'), "/"),
["eq?"] = LispPrimitive:new(common.primitive_algebra('=='), "eq?"),
["<"] = LispPrimitive:new(common.primitive_algebra('<'), "<"),
[">"] = LispPrimitive:new(common.primitive_algebra('>'), ">"),
["tostring"] = LispPrimitive:new(common.primitive_tostring, "tostring"),
["string-append"] = LispPrimitive:new(common.primitive_string_append, "string-append"),
["sqrt"] = LispPrimitive:new(math.sqrt, "sqrt"),
["mod"] = LispPrimitive:new(math.fmod, "mod"),
["floor"] = LispPrimitive:new(math.floor, "floor"),
["open-input-file"] = LispPrimitive:new(common.primitive_open('r'), "open-input-file"),
["read-string"] = LispPrimitive:new(common.primitive_read_string, "read-string"),
["read"] = LispPrimitive:new(common.primitive_read, "read"),
["close-input-port"] = LispPrimitive:new(common.primitive_close, "close-input-port"),
["eval"] = LispEvalPrimitive:new()
}
------------------------------ Function ------------------------------
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
------------------------------ Continuation ------------------------------
Continuation = {}
Continuation.__index = Continuation
function Continuation:new(env, k)
local result = {}
result.__index = result
result.env = env
result.continuation = k
setmetatable(result, self)
return result
end
function Continuation:invoke(args, env, k)
return self:resume(lisp.car(args))
end
function Continuation:catchLookup(tag, throw_k)
return self.continuation:catchLookup(tag, throw_k)
end
ContinuationBottom = Continuation:new()
function ContinuationBottom:new(func)
local result = Continuation:new()
result.func = func
setmetatable(result, self)
return result
end
function ContinuationBottom:resume(val)
self.func(val)
return val
end
function ContinuationBottom:catchLookup(tag, throw_k)
error("Uncaught exception: " .. lisp.list_tostring(tag))
end
ContinuationIf = Continuation:new()
function ContinuationIf:new(true_exp, false_exp, env, k)
local result = Continuation:new(env, k)
result.true_exp = true_exp
result.false_exp = false_exp
setmetatable(result, self)
return result
end
function ContinuationIf:resume(val)
local eval_val
if val then
eval_val = self.true_exp
else
eval_val = self.false_exp
end
return eval(eval_val, self.env, self.continuation)
end
ContinuationBegin = Continuation:new()
function ContinuationBegin:new(exp_list, env, k)
local result = Continuation:new(env, k)
result.exp_list = exp_list
setmetatable(result, self)
return result;
end
function ContinuationBegin:resume(val)
return eval_begin(lisp.cdr(self.exp_list),
self.env, self.continuation)
end
ContinuationSet = Continuation:new()
function ContinuationSet:new(name, env, k)
local result = Continuation:new(env, k)
result.set_name = name
setmetatable(result, self)
return result
end
function ContinuationSet:resume(val)
return self.env:update(self.set_name, val, self.continuation)
end
ContinuationDefine = Continuation:new()
function ContinuationDefine:new(name, env, k)
local result = Continuation:new(env, k)
result.define_name = name
setmetatable(result, self)
return result
end
function ContinuationDefine:resume(val)
return self.env:define(self.define_name, val, self.continuation)
end
ContinuationEvalFunction = Continuation:new()
function ContinuationEvalFunction:new(exp_list, env, k)
local result = Continuation:new(env, k)
result.exp_list = exp_list
setmetatable(result, self)
return result
end
function ContinuationEvalFunction:resume(func)
return eval_arguments(self.exp_list, self.env,
ContinuationApply:new(func, self.env, self.continuation))
end
ContinuationArguments = Continuation:new()
function ContinuationArguments:new(exp_list, env, k)
local result = Continuation:new(env, k)
result.exp_list = exp_list
setmetatable(result, self)
return result
end
function ContinuationArguments:resume(val)
return eval_arguments(lisp.cdr(self.exp_list), self.env,
ContinuationGather:new(val, self.continuation))
end
ContinuationGather = Continuation:new()
function ContinuationGather:new(exp_list, k)
local result = Continuation:new(nil, k)
result.exp_list = exp_list
setmetatable(result, self)
return result
end
function ContinuationGather:resume(val)
return self.continuation:resume(lisp.cons(self.exp_list, val))
end
ContinuationApply = Continuation:new()
function ContinuationApply:new(func, env, k)
local result = Continuation:new(env, k)
result.func = func
setmetatable(result, self)
return result
end
function ContinuationApply:resume(val)
return self.func:invoke(val, self.env, self.continuation)
end
ContinuationCatch = Continuation:new()
function ContinuationCatch:new(body, env, k)
local result = Continuation:new(env, k)
result.body = body
setmetatable(result, self)
return result
end
function ContinuationCatch:resume(val)
return eval_begin(self.body, self.env,
ContinuationLabel:new(val, self.continuation))
end
ContinuationLabel = Continuation:new()
function ContinuationLabel:new(tag, k)
local result = Continuation:new(nil, k)
result.tag = tag
setmetatable(result, self)
return result
end
function ContinuationLabel:resume(val)
return self.continuation:resume(val)
end
function ContinuationLabel:catchLookup(tag, throw_k)
if tag == self.tag then
return eval(throw_k.from, throw_k.env,
ContinuationThrowing:new(tag, self, throw_k))
else
return self.continuation:catchLookup(tag, throw_k)
end
end
ContinuationThrow = Continuation:new()
function ContinuationThrow:new(from, env, k)
local result = Continuation:new(env, k)
result.from = from
setmetatable(result, self)
return result
end
function ContinuationThrow:resume(val)
return self:catchLookup(val, self)
end
ContinuationThrowing = Continuation:new()
function ContinuationThrowing:new(tag, label_k, k)
local result = Continuation:new(nil, k)
result.label_k = label_k
result.tag = tag
setmetatable(result, self)
return result
end
function ContinuationThrowing:resume(val)
return self.label_k:resume(val)
end
------------------------------ Continuation ------------------------------
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
------------------------------ Eval Rules ------------------------------
function text_of_quotation(exp)
return lisp.cadr(exp)
end
--- assignment
function assignment_variable(exp)
return cadr(exp)
end
function assignment_value(exp)
return lisp.cadr(cdr(exp))
end
--- lambda / function
function make_lambda(params, body)
return lisp.cons('lambda', lisp.cons(params, body))
end
function definition_var(exp)
local var = lisp.cadr(exp)
if lisp.is_pair(var) then
return lisp.car(var)
else
return var
end
end
function definition_val(exp)
local var = lisp.cadr(exp)
if lisp.is_pair(var) then
local param = lisp.cdr(lisp.cadr(exp))
local body = lisp.cdr(lisp.cdr(exp))
return make_lambda(param, body)
else
return lisp.cadr(lisp.cdr(exp))
end
end
function lambda_param(exp)
return lisp.cadr(exp)
end
function lambda_body(exp)
return lisp.cdr(cdr(exp))
end
--- if / if-else
function if_predicate(exp)
return lisp.cadr(exp)
end
function if_consequent(exp)
return lisp.cadr(cdr(exp))
end
function if_alternative(exp)
local alt = lisp.cadr(lisp.cdr(lisp.cdr(exp)))
if alt then
return alt
else
return 'false'
end
end
local function make_if(predicate, consequent, alternative)
return lisp.list('if', predicate, consequent, alternative)
end
--- cond
function cond_clauses(exp)
return lisp.cdr(exp)
end
function cond_to_if(exp)
return expand_clauses(cond_clauses(exp))
end
function cond_predicate(clause)
return lisp.car(clause)
end
function cond_action(clause)
return lisp.cdr(clause)
end
function is_cond_else_clause(clause)
return cond_predicate(clause) == 'else'
end
function expand_clauses(clauses)
if clauses == lisp.empty_list then
return 'false'
elseif is_cond_else_clause(lisp.car(clauses)) then
if lisp.cdr(clauses) == lisp.empty_list then
return sequence_to_exp(cond_action(lisp.car(clauses)))
else
return lisp.empty_list
end
else
return make_if(cond_predicate(lisp.car(clauses)),
sequence_to_exp(cond_action(lisp.car(clauses))),
expand_clauses(lisp.cdr(clauses)))
end
end
--- relation
function and_to_if(exp)
if lisp.cdr(exp) == lisp.empty_list then
return lisp.car(exp)
else
return make_if(lisp.car(exp), and_to_if(lisp.cdr(exp)), false)
end
end
function or_to_if(exp)
if lisp.cdr(exp) == lisp.empty_list then
return lisp.car(exp)
else
return make_if(lisp.car(exp), true, or_to_if(lisp.cdr(exp)))
end
end
function not_to_if(exp)
return make_if(lisp.car(exp), false, true)
end
--- sequence
function first_exp(sequence)
return lisp.car(sequence)
end
function last_exp(sequence)
return lisp.cdr(sequence) == lisp.empty_list
end
function rest_exp(sequence)
return lisp.cdr(sequence)
end
function begin_actions(exp)
return lisp.cdr(exp)
end
local function make_begin(sequence)
return lisp.cons('begin', sequence)
end
function sequence_to_exp(sequence)
if sequence == lisp.empty_list then
return lisp.empty_list
elseif last_exp(sequence) then
return first_exp(sequence)
else
return make_begin(sequence)
end
end
--- let
function let_to_lambda_apply(exp)
local lambda = let_to_lambda(exp)
local arguments = let_arguments(exp)
return lisp.cons(lambda, arguments)
end
function let_to_lambda(exp)
local param = let_lambda_parameter(exp)
local body = let_lambda_body(exp)
return make_lambda(param, body)
end
function let_lambda_parameter(exp)
return lisp.map(lisp.car, lisp.cadr(exp))
end
function let_arguments(exp)
return lisp.map(lisp.cadr, lisp.cadr(exp))
end
function let_lambda_body(exp)
return lisp.cdr(lisp.cdr(exp))
end
------------------------------ Eval Rules ------------------------------
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
------------------------------ Evaluator ------------------------------
function eval(exp, env, k)
if is_variable(exp) then
return eval_variable(exp, env, k)
elseif is_self_evaluating(exp) then
return eval_quote(exp, env, k)
elseif is_quoted(exp) then
return eval_quote(lisp.cadr(exp), env, k)
elseif is_if(exp) then
return eval_if(lisp.cadr(exp),
lisp.cadr(lisp.cdr(exp)),
lisp.cadr(lisp.cdr(lisp.cdr(exp))),
env, k)
elseif is_and(exp) then
return eval(and_to_if(lisp.cdr(exp)), env, k)
elseif is_or(exp) then
return eval(or_to_if(lisp.cdr(exp)), env, k)
elseif is_not(exp) then
return eval(not_to_if(lisp.cdr(exp)), env, k)
elseif is_cond(exp) then
return eval(cond_to_if(exp), env, k)
elseif is_begin(exp) then
return eval_begin(lisp.cdr(exp), env, k)
elseif is_assignment(exp) then
return eval_set(lisp.cadr(exp), lisp.cadr(lisp.cdr(exp)), env, k)
elseif is_definition(exp) then
return eval_definition(exp, env, k)
elseif is_lambda(exp) then
return eval_lambda(lisp.cadr(exp), lisp.cdr(lisp.cdr(exp)), env, k)
elseif is_let(exp) then
return eval(let_to_lambda_apply(exp), env, k)
elseif is_catch(exp) then
return eval_catch(lisp.cadr(exp), lisp.cdr(lisp.cdr(exp)), env, k)
elseif is_throw(exp) then
return eval_throw(lisp.cadr(exp), lisp.cadr(lisp.cdr(exp)), env, k)
else
return eval_application(lisp.car(exp), lisp.cdr(exp), env, k)
end
end
function eval_quote(val, env, k)
return k:resume(val)
end
function eval_variable(name, env, k)
return env:lookup(name, k)
end
function eval_if(cond_exp, true_exp, false_exp, env, k)
return eval(cond_exp, env,
ContinuationIf:new(true_exp, false_exp, env, k))
end
function eval_begin(exp_list, env, k)
if lisp.is_pair(exp_list) then
if lisp.is_pair(lisp.cdr(exp_list)) then
return eval(lisp.car(exp_list), env,
ContinuationBegin:new(exp_list, env, k))
else
return eval(lisp.car(exp_list), env, k)
end
else
return k:resume(lisp.empty_list)
end
end
function eval_lambda(params, exp_list, env, k)
return k:resume(LispFunction:new(params, exp_list, env))
end
function eval_set(name, exp, env, k)
return eval(exp, env, ContinuationSet:new(name, env, k))
end
function eval_definition(exp, env, k)
local name = definition_var(exp)
local val_exp = definition_val(exp)
return eval(val_exp, env, ContinuationDefine:new(name, env, k))
end
function eval_arguments(args, env, k)
if lisp.is_pair(args) then
return eval(lisp.car(args), env, ContinuationArguments:new(args, env, k))
else
return k:resume(lisp.empty_list)
end
end
function eval_application(func, args, env, k)
return eval(func, env, ContinuationEvalFunction:new(args, env, k))
end
function eval_catch(tag, body, env, k)
return eval(tag, env, ContinuationCatch:new(body, env, k))
end
function eval_throw(tag, from, env, k)
return eval(tag, env, ContinuationThrow:new(from, env, k))
end
------------------------------ Evaluator ------------------------------
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
------------------------------ expression predicates ------------------------------
function is_self_evaluating(exp)
-- string is quote rather than self-evaluating
return type(exp) == 'number' or type(exp) == 'boolean' or false
end
function is_variable(exp)
return (type(exp) == 'string' and (not is_self_evaluating(exp)))
end
local function is_tagged(exp, tag)
return lisp.is_pair(exp) and
lisp.car(exp) == tag
end
function is_quoted(exp)