-
Notifications
You must be signed in to change notification settings - Fork 3
/
eval.lua
754 lines (485 loc) · 13.6 KB
/
eval.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
-------------------------------------------------------------------------
--[[
To Do:
--]]
-------------------------------------------------------------------------
do
local lisp = require "lisp"
local common = require "eval_common"
local old_setmetatable = _ENV.setmetatable
local old_getmetatable = _ENV.getmetatable
local old_error = _ENV.error
local old_table = _ENV.table
local old_type = _ENV.type
local old_string = _ENV.string
local old_math = math
local old_print = _ENV.print
local old_pairs = _ENV.pairs
_ENV = {}
_ENV.setmetatable = old_setmetatable
_ENV.getmetatable = old_getmetatable
_ENV.error = old_error
_ENV.is_pair = lisp.is_pair
_ENV.cons = lisp.cons
_ENV.car = lisp.car
_ENV.cdr = lisp.cdr
_ENV.cadr = lisp.cadr
_ENV.list = lisp.list
_ENV.length = lisp.length
_ENV.map = lisp.map
_ENV.empty_list = lisp.empty_list
_ENV.list_unpack = lisp.list_unpack
_ENV.list_tostring = lisp.list_tostring
_ENV.table = table
_ENV.type = old_type
_ENV.string = old_string
_ENV.math = old_math
_ENV.print = old_print
_ENV.pairs = old_pairs
_ENV.common = common
end
-------------------------------------------------------------------------
-- -- environment structure -- --
Frame = {}
Frame.__index = Frame
function Frame:new()
local result = {}
setmetatable(result, self)
return result
end
local nil_val = { __tostring = function(v) return 'Nil' end }
setmetatable(nil_val, nil_val)
function Frame:newFromList(varNames, varValues)
local result = {}
local function addProp(nameList, valueList)
if nameList ~= nil and nameList ~= empty_list then
local value = car(valueList) or nil_val
result[car(nameList)] = value
return addProp(cdr(nameList), cdr(valueList))
end
end
addProp(varNames, varValues)
setmetatable(result, self)
return result
end
Enviornment = {}
Enviornment.__index = Enviornment
function Enviornment.initEnviornment()
return Enviornment:new(Procedure.primitiveProcedures)
end
function Enviornment:new(frame, enclosing)
local result = {}
result.frame = frame
result.enclosing = enclosing
setmetatable(result, self)
return result
end
function Enviornment:lookup(var)
if self.frame[var] ~= nil then
if self.frame[var] == nil_val then
return nil_val
else
return self.frame[var]
end
elseif self.enclosing then
return self.enclosing:lookup(var)
else
return error("Unknown Variable [" .. var .. "].")
end
end
function Enviornment:setVar(var, val)
if self.frame[var] ~= nil then
-- val could be boolean 'false'
val = ((val == nil) and nil_val) or val
self.frame[var] = val
return val
elseif self.enclosing then
return self.enclosing:setVar(var, val)
else
return nil
end
end
function Enviornment:defineVar(var, val)
-- val could be boolean 'false'
val = ((val == nil) and nil_val) or val
self.frame[var] = val
end
--- evaluation
function Enviornment:eval(exp)
if is_self_evaluating(exp) then
return exp
elseif is_quoted(exp) then
return text_of_quotation(exp)
elseif is_variable(exp) then
return self:lookup(exp)
elseif is_assignment(exp) then
return self:evalAssignment(exp)
elseif is_definition(exp) then
return self:evalDefinition(exp)
elseif is_if(exp) then
return self:evalIf(exp)
elseif is_and(exp) then
return self:eval(and_to_if(cdr(exp)))
elseif is_or(exp) then
return self:eval(or_to_if(cdr(exp)))
elseif is_not(exp) then
return self:eval(not_to_if(cdr(exp)))
elseif is_lambda(exp) then
return Procedure:new(
lambda_param(exp),
lambda_body(exp),
self)
elseif is_let(exp) then
return self:eval(let_to_lambda_apply(exp))
elseif is_begin(exp) then
return self:evalSequence(cdr(exp))
elseif is_cond(exp) then
return self:eval(cond_to_if(exp))
elseif is_force(exp) then
local operator = self:eval(cadr(exp))
return operator:apply(nil)
elseif is_delay(exp) then
return self:evalDelay(exp)
elseif is_cons_stream(exp) then
local toCar = cadr(exp)
local toCdr = make_lambda(nil, cdr(cdr(exp)))
return self:eval(list('cons', toCar, toCdr))
elseif is_application(exp) then
local operator = self:eval(car(exp))
local operands = self:listOfValues(cdr(exp))
if operator == nil or getmetatable(operator) ~= Procedure then
error("Invalid operator in application: " .. tostring(exp))
end
return operator:apply(operands)
else
return nil
end
end
function Enviornment:evalAssignment(exp)
local assign_var = assignment_variable(exp)
local assign_val = assignment_value(exp)
self:setVar(assign_var,
self:eval(assign_val))
end
function Enviornment:evalDefinition(exp)
self:defineVar(definition_var(exp),
self:eval(definition_val(exp)))
end
function Enviornment:evalIf(exp)
if self:eval(if_predicate(exp)) then
return self:eval(if_consequent(exp))
else
return self:eval(if_alternative(exp))
end
end
function Enviornment:evalDelay(exp)
local body = cdr(exp)
return self:eval(make_lambda(nil, body))
end
function Enviornment:evalSequence(exp)
local result = self:eval(first_exp(exp))
if is_last_exp(exp) then
return result
else
return self:evalSequence(rest_exp(exp))
end
end
function Enviornment:listOfValues(exps)
if exps == nil or exps == empty_list then
return empty_list
else
return cons(self:eval(car(exps)),
self:listOfValues(cdr(exps)))
end
end
-- -- environment structure -- --
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-- -- ---- procedure ---- -- --
Procedure = {}
Procedure.__index = Procedure
local function procedure_to_string(proc)
if proc.primitiveName then
return "[Primitive: " .. proc.primitiveName .. "]"
end
return "[Procedure: " .. list_tostring(proc.params) .. " " ..
list_tostring(proc.body) .. "]"
end
Procedure.__tostring = procedure_to_string
function Procedure:new(params, body, env, type, primitivename)
local result = {
params = params,
body = body,
env = env,
type = type or 'procedure',
primitiveName = primitivename
}
setmetatable(result, self)
return result
end
Procedure.primitiveProcedures = {
["car"] = Procedure:new(nil, car, nil, 'primitive', 'car'),
["cdr"] = Procedure:new(nil, cdr, nil, 'primitive', 'cdr'),
["cons"] = Procedure:new(nil, cons, nil, 'primitive', 'cons'),
["list"] = Procedure:new(nil, list, nil, 'primitive', 'list'),
["length"] = Procedure:new(nil, length, nil, 'primitive', 'length'),
["null?"] = Procedure:new(nil, common.primitive_null, nil, 'primitive', 'null?'),
["pair?"] = Procedure:new(nil, is_pair, nil, 'primitive', 'pair?'),
["atom?"] = Procedure:new(nil, common.primitive_atom, nil, 'primitive', 'atom?'),
["+"] = Procedure:new(nil, common.primitive_algebra('+'), nil, 'primitive', '+'),
["-"] = Procedure:new(nil, common.primitive_algebra('-'), nil, 'primitive', '-'),
["*"] = Procedure:new(nil, common.primitive_algebra('*'), nil, 'primitive', '*'),
["/"] = Procedure:new(nil, common.primitive_algebra('/'), nil, 'primitive', '/'),
["<"] = Procedure:new(nil, common.primitive_algebra('<'), nil, 'primitive', '<'),
[">"] = Procedure:new(nil, common.primitive_algebra('>'), nil, 'primitive', '>'),
["eq?"] = Procedure:new(nil, common.primitive_algebra('=='), nil, 'primitive', 'eq?'),
["="] = Procedure:new(nil, common.primitive_algebra('=='), nil, 'primitive', '='),
["print"] = Procedure:new(nil, print, nil, 'primitive', 'print'),
["tostring"] = Procedure:new(nil, common.primitive_tostring, nil, 'primitive', 'tostring'),
["string-append"] = Procedure:new(nil, common.primitive_string_append, nil, 'primitive', "string-append"),
["sqrt"] = Procedure:new(nil, math.sqrt, nil, "primitive", "sqrt"),
["mod"] = Procedure:new(nil, math.fmod, nil, "primitive", "mod"),
["floor"] = Procedure:new(nil, math.floor, nil, "primitive", "floor")
}
setmetatable(Procedure.primitiveProcedures, Frame)
function Procedure:isPrimitive()
return self.type == 'primitive'
end
function Procedure:isCombound()
return self.type == 'procedure'
end
function Procedure:apply(arguments)
if self:isPrimitive() then
return apply_primitive_procedure(self.body, arguments)
elseif self:isCombound() then
local newEnv = Enviornment:new(
Frame:newFromList(
self.params,
arguments),
self.env)
return newEnv:evalSequence(self.body)
end
end
function apply_primitive_procedure(proc, args)
return proc(list_unpack(args))
end
-- -- ---- procedure ---- -- --
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-- -- 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 is_pair(exp) and
car(exp) == tag
end
function is_quoted(exp)
return is_tagged(exp, 'quote')
end
function is_assignment(exp)
return is_tagged(exp, 'set!')
end
function is_definition(exp)
return is_tagged(exp, 'define')
end
function is_if(exp)
return is_tagged(exp, 'if')
end
function is_and(exp)
return is_tagged(exp, 'and')
end
function is_or(exp)
return is_tagged(exp, 'or')
end
function is_not(exp)
return is_tagged(exp, 'not')
end
function is_lambda(exp)
return is_tagged(exp, 'lambda')
end
function is_begin(exp)
return is_tagged(exp, 'begin')
end
function is_cond(exp)
return is_tagged(exp, 'cond')
end
function is_application(exp)
return is_pair(exp)
end
function is_last_exp(exp)
return cdr(exp) == empty_list
end
function is_delay(exp)
return is_tagged(exp, 'delay')
end
function is_force(exp)
return is_tagged(exp, 'force')
end
function is_cons_stream(exp)
return is_tagged(exp, 'cons-stream')
end
function is_let(exp)
return is_tagged(exp, 'let')
end
-- -- expression predicates -- --
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-- -- evaluation rules -- --
function text_of_quotation(exp)
return cadr(exp)
end
--- assignment
function assignment_variable(exp)
return cadr(exp)
end
function assignment_value(exp)
return cadr(cdr(exp))
end
--- lambda / function
function make_lambda(params, body)
return cons('lambda', cons(params, body))
end
function definition_var(exp)
local var = cadr(exp)
if is_pair(var) then
return car(var)
else
return var
end
end
function definition_val(exp)
local var = cadr(exp)
if is_pair(var) then
local param = cdr(car(cdr(exp)))
local body = cdr(cdr(exp))
return make_lambda(param, body)
else
return car(cdr(cdr(exp)))
end
end
function lambda_param(exp)
return cadr(exp)
end
function lambda_body(exp)
return cdr(cdr(exp))
end
--- if / if-else
function if_predicate(exp)
return cadr(exp)
end
function if_consequent(exp)
return cadr(cdr(exp))
end
function if_alternative(exp)
local alt = cadr(cdr(cdr(exp)))
if alt then
return alt
else
return 'false'
end
end
local function make_if(predicate, consequent, alternative)
return list('if', predicate, consequent, alternative)
end
--- cond
function cond_clauses(exp)
return cdr(exp)
end
function cond_to_if(exp)
return expand_clauses(cond_clauses(exp))
end
function cond_predicate(clause)
return car(clause)
end
function cond_action(clause)
return cdr(clause)
end
function is_cond_else_clause(clause)
return cond_predicate(clause) == 'else'
end
function expand_clauses(clauses)
if clauses == empty_list then
return 'false'
elseif is_cond_else_clause(car(clauses)) then
if cdr(clauses) == empty_list then
return sequence_to_exp(cond_action(car(clauses)))
else
return empty_list
end
else
return make_if(cond_predicate(car(clauses)),
sequence_to_exp(cond_action(car(clauses))),
expand_clauses(cdr(clauses)))
end
end
--- relation
function and_to_if(exp)
if cdr(exp) == empty_list then
return car(exp)
else
return make_if(car(exp), and_to_if(cdr(exp)), false)
end
end
function or_to_if(exp)
if cdr(exp) == empty_list then
return car(exp)
else
return make_if(car(exp), true, or_to_if(cdr(exp)))
end
end
function not_to_if(exp)
return make_if(car(exp), false, true)
end
--- sequence
function first_exp(sequence)
return car(sequence)
end
function last_exp(sequence)
return cdr(sequence) == empty_list
end
function rest_exp(sequence)
return cdr(sequence)
end
function begin_actions(exp)
return cdr(exp)
end
local function make_begin(sequence)
return cons('begin', sequence)
end
function sequence_to_exp(sequence)
if sequence == empty_list then
return 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 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 map(car, cadr(exp))
end
function let_arguments(exp)
return map(cadr, cadr(exp))
end
function let_lambda_body(exp)
return cdr(cdr(exp))
end
-- -- evaluation rules -- --
-------------------------------------------------------------------------
return _ENV