-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheme.py
588 lines (517 loc) · 19.3 KB
/
scheme.py
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
"""This module implements the core Scheme interpreter functions, including the
eval/apply mutual recurrence, environment model, and read-eval-print loop.
"""
from scheme_primitives import *
from scheme_reader import *
from ucb import main, trace
##############
# Eval/Apply #
##############
def scheme_eval(expr, env):
"""Evaluate Scheme expression EXPR in environment ENV.
>>> expr = read_line("(+ 2 2)")
>>> expr
Pair('+', Pair(2, Pair(2, nil)))
>>> scheme_eval(expr, create_global_frame())
4
>>> env = create_global_frame()
>>> scheme_eval(read_line("(define (f1 a b) (+ a b))"), env)
>>> scheme_eval(read_line("(f1 1 2)"), env)
3
>>> scheme_eval(read_line("(define f2 (lambda () 5))"), env)
>>> scheme_eval(read_line("(f2)"), env)
5
>>> scheme_eval(read_line("(f2 2)"), env) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
>>> env = create_global_frame()
>>> scheme_eval(read_line("(cond ((= 1 2)))"), env) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
>>> env = create_global_frame()
>>> scheme_eval(read_line("(let ((2 2)) 2)"), env) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
>>> scheme_eval(read_line("(let ((x 2 3)) 2)"), env) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
"""
if expr is None:
raise SchemeError("Cannot evaluate an undefined expression.")
# Evaluate Atoms
if scheme_symbolp(expr):
return env.lookup(expr)
elif scheme_atomp(expr):
return expr
# All non-atomic expressions are lists.
if not scheme_listp(expr):
raise SchemeError("malformed list: {0}".format(str(expr)))
first, rest = expr.first, expr.second
# Evaluate Combinations
if first in LOGIC_FORMS:
return scheme_eval(LOGIC_FORMS[first](rest, env), env)
elif first == "lambda":
return do_lambda_form(rest, env)
elif first == "mu":
return do_mu_form(rest)
elif first == "define":
return do_define_form(rest, env)
elif first == "quote":
return do_quote_form(rest)
elif first == "let":
expr, env = do_let_form(rest, env)
return scheme_eval(expr, env)
else:
procedure = scheme_eval(first, env)
args = rest.map(lambda operand: scheme_eval(operand, env))
return scheme_apply(procedure, args, env)
def scheme_apply(procedure, args, env):
"""Apply Scheme PROCEDURE to argument values ARGS in environment ENV."""
if isinstance(procedure, PrimitiveProcedure):
return apply_primitive(procedure, args, env)
elif isinstance(procedure, LambdaProcedure):
if scheme_length(procedure.formals) != scheme_length(args):
raise SchemeError("Formals amount error")
new_frame = procedure.env.make_call_frame(procedure.formals, args)
return scheme_eval(procedure.body, new_frame)
elif isinstance(procedure, MuProcedure):
if scheme_length(procedure.formals) != scheme_length(args):
raise SchemeError("Formals amount error")
new_frame = env.make_call_frame(procedure.formals, args)
return scheme_eval(procedure.body, new_frame)
else:
raise SchemeError("Cannot call {0}".format(str(procedure)))
def apply_primitive(procedure, args, env):
"""Apply PrimitiveProcedure PROCEDURE to a Scheme list of ARGS in ENV.
>>> env = create_global_frame()
>>> plus = env.bindings["+"]
>>> twos = Pair(2, Pair(2, nil))
>>> apply_primitive(plus, twos, env)
4
"""
arg_list = []
for a in args:
arg_list.append(a)
if procedure.use_env == True:
arg_list.append(env)
try:
return procedure.fn(*arg_list)
except TypeError as e:
raise SchemeError(e)
################
# Environments #
################
class Frame(object):
"""An environment frame binds Scheme symbols to Scheme values."""
def __init__(self, parent):
"""An empty frame with a PARENT frame (that may be None)."""
self.bindings = {}
self.parent = parent
def __repr__(self):
if self.parent is None:
return "<Global Frame>"
else:
s = sorted('{0}: {1}'.format(k,v) for k,v in self.bindings.items())
return "<{{{0}}} -> {1}>".format(', '.join(s), repr(self.parent))
def lookup(self, symbol):
"""Return the value bound to SYMBOL. Errors if SYMBOL is not found."""
e = self
while e is not None:
if symbol in e.bindings:
return e.bindings[symbol]
e = e.parent
raise SchemeError("unknown identifier: {0}".format(str(symbol)))
def global_frame(self):
"""The global environment at the root of the parent chain."""
e = self
while e.parent is not None:
e = e.parent
return e
def make_call_frame(self, formals, vals):
"""Return a new local frame whose parent is SELF, in which the symbols
in the Scheme formal parameter list FORMALS are bound to the Scheme
values in the Scheme value list VALS.
>>> env = create_global_frame()
>>> formals, vals = read_line("(a b c)"), read_line("(1 2 3)")
>>> env.make_call_frame(formals, vals)
<{a: 1, b: 2, c: 3} -> <Global Frame>>
>>> formals, vals = read_line("(a b c)"), read_line("(1 2)")
>>> env.make_call_frame(formals, vals) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
"""
frame = Frame(self)
while scheme_pairp(formals):
if scheme_nullp(vals):
raise SchemeError("Less args than needed")
frame.define(formals.first, vals.first)
formals = formals.second
vals = vals.second
if not scheme_nullp(formals):
frame.define(formals, vals)
elif not scheme_nullp(vals):
raise SchemeError("More agrs than needed")
return frame
def define(self, sym, val):
"""Define Scheme symbol SYM to have value VAL in SELF."""
self.bindings[sym] = val
class LambdaProcedure(object):
"""A procedure defined by a lambda expression or the complex define form."""
def __init__(self, formals, body, env):
"""A procedure whose formal parameter list is FORMALS (a Scheme list),
whose body is the single Scheme expression BODY, and whose parent
environment is the Frame ENV. A lambda expression containing multiple
expressions, such as (lambda (x) (display x) (+ x 1)) can be handled by
using (begin (display x) (+ x 1)) as the body."""
self.formals = formals
self.body = body
self.env = env
def __str__(self):
return "(lambda {0} {1})".format(str(self.formals), str(self.body))
def __repr__(self):
args = (self.formals, self.body, self.env)
return "LambdaProcedure({0}, {1}, {2})".format(*(repr(a) for a in args))
class MuProcedure(object):
"""A procedure defined by a mu expression, which has dynamic scope.
_________________
< Scheme is cool! >
-----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
"""
def __init__(self, formals, body):
"""A procedure whose formal parameter list is FORMALS (a Scheme list),
whose body is the single Scheme expression BODY. A mu expression
containing multiple expressions, such as (mu (x) (display x) (+ x 1))
can be handled by using (begin (display x) (+ x 1)) as the body."""
self.formals = formals
self.body = body
def __str__(self):
return "(mu {0} {1})".format(str(self.formals), str(self.body))
def __repr__(self):
args = (self.formals, self.body)
return "MuProcedure({0}, {1})".format(*(repr(a) for a in args))
#################
# Special forms #
#################
def do_lambda_form(vals, env):
"""Evaluate a lambda form with parameters VALS in environment ENV."""
check_form(vals, 2)
formals = vals[0]
check_formals(formals)
if len(vals.second) > 1:
lam_body = Pair("begin", vals.second)
return LambdaProcedure(formals, lam_body, env)
else:
lam_body = vals[1]
return LambdaProcedure(formals, lam_body, env)
def do_mu_form(vals):
"""Evaluate a mu form with parameters VALS."""
check_form(vals, 2)
formals = vals[0]
check_formals(formals)
if len(vals.second) > 1:
mu_body = Pair("begin", vals.second)
return MuProcedure(formals, mu_body)
else:
mu_body = vals[1]
return MuProcedure(formals, mu_body)
def do_define_form(vals, env):
"""Evaluate a define form with parameters VALS in environment ENV.
#Problem5
>>> global_frame = create_global_frame()
>>> frame1 = Frame(global_frame)
>>> global_frame.define('a', 1)
>>> global_frame.define('b', 4)
>>> frame1.define('a', 2)
>>> frame1.define('c', 3)
>>> frame1.lookup('a')
2
>>> frame1.lookup('b')
4
>>> frame1.lookup('c')
3
>>> global_frame.lookup('a')
1
>>> global_frame.lookup('c') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
#Problem A9
>>> env = create_global_frame()
>>> vals = read_line('( (pow x y) (* x y) )')
>>> do_define_form(vals, env)
>>> print(env.lookup('pow'))
(lambda (x y) (* x y))
>>> vals = read_line('( (0 x y) (* x y) )')
>>> do_define_form(vals, env) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
"""
check_form(vals, 2)
target = vals[0]
check_formals(target)
if scheme_symbolp(target):
check_form(vals, 2, 2)
value = scheme_eval(vals[1], env)
env.define(target, value)
elif isinstance(target, Pair):
lam_name = target.first
#print(lam_name) #for testing
lam_formals = target.second
#print(lam_formals) #fot testing
lam_body = vals.second
#print(lam_body) #for testing
value = do_lambda_form(Pair(lam_formals, lam_body), env)
env.define(lam_name, value)
else:
raise SchemeError("bad argument to define")
def do_quote_form(vals):
"""Evaluate a quote form with parameters VALS."""
check_form(vals, 1, 1)
return vals[0]
def do_let_form(vals, env):
"""Evaluate a let form with parameters VALS in environment ENV."""
check_form(vals, 2)
bindings = vals[0]
exprs = vals.second
if not scheme_listp(bindings):
raise SchemeError("bad bindings list in let form")
# Add a frame containing bindings
names, vals = nil, nil
for binding in bindings:
check_form(binding,2,2)
if len(binding) != 2:
raise SchemeError("let binding error")
name = binding[0]
val = scheme_eval(binding[1], env)
names = Pair(name, names)
vals = Pair(val, vals)
check_formals(names)
new_env = env.make_call_frame(names, vals)
# Evaluate all but the last expression after bindings, and return the last
last = len(exprs)-1
for i in range(0, last):
scheme_eval(exprs[i], new_env)
return exprs[last], new_env
#########################
# Logical Special Forms #
#########################
def do_if_form(vals, env):
"""Evaluate if form with parameters VALS in environment ENV."""
check_form(vals, 3, 3)
eval_cond = scheme_eval(vals.first, env)
if scheme_true(eval_cond):
return vals.second.first
else:
return vals.second.second.first
def do_and_form(vals, env):
"""Evaluate short-circuited and with parameters VALS in environment ENV."""
if len(vals) == 0:
return True
for val in vals:
eval = scheme_eval(val, env)
if not scheme_true(eval):
return False
return val
def do_or_form(vals, env):
"""Evaluate short-circuited or with parameters VALS in environment ENV."""
for val in vals:
eval = scheme_eval(val, env)
if scheme_true(eval):
return val
return False
def do_cond_form(vals, env):
"""Evaluate cond form with parameters VALS in environment ENV."""
num_clauses = len(vals)
for i, clause in enumerate(vals):
check_form(clause, 1)
if clause.first == "else":
if i < num_clauses-1:
raise SchemeError("else must be last")
test = True
if clause.second is nil:
raise SchemeError("badly formed else clause")
else:
test = scheme_eval(clause.first, env)
if test:
if len(clause) == 1:
if scheme_numberp(clause.first) or scheme_symbolp(clause.first):
return clause.first
return True
if len(clause) == 2:
return clause.second.first
if len(clause) > 2:
return do_begin_form(clause.second, env)
def do_begin_form(vals, env):
"""Evaluate begin form with parameters VALS in environment ENV."""
check_form(vals, 1)
if vals.second is nil:
return vals.first
else:
scheme_eval(vals.first, env)
return do_begin_form(vals.second, env)
LOGIC_FORMS = {
"and": do_and_form,
"or": do_or_form,
"if": do_if_form,
"cond": do_cond_form,
"begin": do_begin_form,
}
# Utility methods for checking the structure of Scheme programs
def check_form(expr, min, max = None):
"""Check EXPR (default SELF.expr) is a proper list whose length is
at least MIN and no more than MAX (default: no maximum). Raises
a SchemeError if this is not the case."""
if not scheme_listp(expr):
raise SchemeError("badly formed expression: " + str(expr))
length = len(expr)
if length < min:
raise SchemeError("too few operands in form")
elif max is not None and length > max:
raise SchemeError("too many operands in form")
def check_formals(formals):
"""Check that FORMALS is a valid parameter list, a Scheme list of symbols
in which each symbol is distinct.
>>> check_formals(read_line("(a b c)"))
>>> check_formals(read_line("(a . b)")) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
>>> check_formals(read_line("(a b c a)")) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
>>> check_formals(read_line("(a b 0 c)")) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SchemeError:
>>> check_formals(read_line("()"))
"""
formal_symbols = []
def check_sym_helper(symbol):
if not scheme_symbolp(symbol):
raise SchemeError('Invaild symbol')
elif symbol in formal_symbols:
raise SchemeError('Repeated symbol')
elif not scheme_listp(formals):
raise SchemeError('Not a well-formed list')
formal_symbols.append(symbol)
while isinstance(formals, Pair) and formals != nil:
check_sym_helper(formals.first)
formals = formals.second
##################
# Tail Recursion #
##################
def scheme_optimized_eval(expr, env):
"""Evaluate Scheme expression EXPR in environment ENV."""
while True:
if expr is None:
raise SchemeError("Cannot evaluate an undefined expression.")
# Evaluate Atoms
if scheme_symbolp(expr):
return env.lookup(expr)
elif scheme_atomp(expr):
return expr
# All non-atomic expressions are lists.
if not scheme_listp(expr):
raise SchemeError("malformed list: {0}".format(str(expr)))
first, rest = expr.first, expr.second
# Evaluate Combinations
if first in LOGIC_FORMS:
expr = LOGIC_FORMS[first](rest, env)
elif first == "lambda":
return do_lambda_form(rest, env)
elif first == "mu":
return do_mu_form(rest)
elif first == "define":
return do_define_form(rest, env)
elif first == "quote":
return do_quote_form(rest)
elif first == "let":
expr, env = do_let_form(rest, env)
else:
new_procedure = scheme_optimized_eval(first, env)
args = rest.map(lambda operand: scheme_optimized_eval(operand, env))
if isinstance(new_procedure, LambdaProcedure):
expr = new_procedure.body
env = new_procedure.env.make_call_frame(new_procedure.formals, args)
elif isinstance(new_procedure, MuProcedure):
expr = new_procedure.body
env = env.make_call_frame(new_procedure.formals, args)
elif isinstance(new_procedure, PrimitiveProcedure):
return scheme_apply(new_procedure, args, env)
else:
raise SchemeError("not alble to call {0}".format(str(new_procedure)))
################################################################
# Uncomment the following line to apply tail call optimization #
################################################################
scheme_eval = scheme_optimized_eval
################
# Input/Output #
################
def read_eval_print_loop(next_line, env):
"""Read and evaluate input until an end of file or keyboard interrupt."""
while True:
try:
src = next_line()
while src.more_on_line:
expression = scheme_read(src)
result = scheme_eval(expression, env)
if result is not None:
print(result)
except (SchemeError, SyntaxError, ValueError) as err:
print("Error:", err)
except (KeyboardInterrupt, EOFError): # <Control>-D, etc.
return
def scheme_load(sym, env):
"""Load Scheme source file named SYM in environment ENV."""
check_type(sym, scheme_symbolp, 0, "load")
with scheme_open(sym) as infile:
lines = infile.readlines()
def next_line():
return buffer_lines(lines)
read_eval_print_loop(next_line, env.global_frame())
def scheme_open(filename):
"""If either FILENAME or FILENAME.scm is the name of a valid file,
return a Python file opened to it. Otherwise, raise an error."""
try:
return open(filename)
except IOError as exc:
if filename.endswith('.scm'):
raise SchemeError(str(exc))
try:
return open(filename + '.scm')
except IOError as exc:
raise SchemeError(str(exc))
def create_global_frame():
"""Initialize and return a single-frame environment with built-in names."""
env = Frame(None)
env.define("eval", PrimitiveProcedure(scheme_eval, True))
env.define("apply", PrimitiveProcedure(scheme_apply, True))
env.define("load", PrimitiveProcedure(scheme_load, True))
add_primitives(env)
return env
@main
def run(*argv):
next_line = buffer_input
if argv:
try:
filename = argv[0]
input_file = open(argv[0])
lines = input_file.readlines()
def next_line():
return buffer_lines(lines)
except IOError as err:
print(err)
sys.exit(1)
read_eval_print_loop(next_line, create_global_frame())