-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheme.py
748 lines (643 loc) · 24.7 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
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
"""A Scheme interpreter and its read-eval-print loop."""
from __future__ import print_function # Python 2 compatibility
from scheme_builtins import *
from scheme_reader import *
from ucb import main, trace
##############
# Eval/Apply #
##############
def scheme_eval(expr, env, tail=False): # Optional third argument is ignored
"""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
"""
# Evaluate atoms
if scheme_symbolp(expr):
return env.lookup(expr)
elif self_evaluating(expr):
return expr
# All non-atomic expressions are lists (combinations)
if not scheme_listp(expr):
raise SchemeError('malformed list: {0}'.format(repl_str(expr)))
first, rest = expr.first, expr.rest
if scheme_symbolp(first) and first in SPECIAL_FORMS:
return SPECIAL_FORMS[first](rest, env)
else:
# BEGIN PROBLEM 4
procedure = scheme_eval(first, env, tail)
if isinstance(procedure, MacroProcedure):
return scheme_eval(MacroProcedure.apply_macro(procedure, rest, env), env, tail)
check_procedure(procedure)
return scheme_apply(procedure, rest.map(lambda x: scheme_eval(x, env, tail)), env)
# END PROBLEM 4
def self_evaluating(expr):
"""Return whether EXPR evaluates to itself."""
return (scheme_atomp(expr) and not scheme_symbolp(expr)) or expr is None
def scheme_apply(procedure, args, env):
"""Apply Scheme PROCEDURE to argument values ARGS (a Scheme list) in
environment ENV."""
check_procedure(procedure)
if isinstance(procedure, BuiltinProcedure):
return procedure.apply(args, env)
else:
new_env = procedure.make_call_frame(args, env)
return eval_all(procedure.body, new_env)
def eval_all(expressions, env):
"""Evaluate each expression in the Scheme list EXPRESSIONS in
environment ENV and return the value of the last."""
# BEGIN PROBLEM 7
if expressions is nil:
return None
a = expressions
while a.rest is not nil:
scheme_eval(a.first,env)
a = a.rest
return scheme_eval(a.first, env, True)
# END PROBLEM 7
################
# Environments #
################
class Frame(object):
"""An environment frame binds Scheme symbols to Scheme values."""
def __init__(self, parent):
"""An empty frame with parent frame PARENT (which may be None)."""
self.bindings = {}
self.parent = parent
def __repr__(self):
if self.parent is None:
return '<Global Frame>'
s = sorted(['{0}: {1}'.format(k, v) for k, v in self.bindings.items()])
return '<{{{0}}} -> {1}>'.format(', '.join(s), repr(self.parent))
def define(self, symbol, value):
"""Define Scheme SYMBOL to have VALUE."""
# BEGIN PROBLEM 2
self.bindings[symbol] = value
# END PROBLEM 2
def lookup(self, symbol):
"""Return the value bound to SYMBOL. Errors if SYMBOL is not found."""
# BEGIN PROBLEM 2
if symbol in self.bindings:
return self.bindings[symbol]
if self.parent:
return self.parent.lookup(symbol)
raise SchemeError('unknown identifier: {0}'.format(symbol))
def make_child_frame(self, formals, vals):
"""Return a new local frame whose parent is SELF, in which the symbols
in a Scheme list of formal parameters FORMALS are bound to the Scheme
values in the Scheme list VALS. Raise an error if too many or too few
vals are given.
>>> env = create_global_frame()
>>> formals, expressions = read_line('(a b c)'), read_line('(1 2 3)')
>>> env.make_child_frame(formals, expressions)
<{a: 1, b: 2, c: 3} -> <Global Frame>>
"""
# BEGIN PROBLEM 10
a, b = formals, vals
f = Frame(self)
while a != nil or b != nil:
if a == nil or b == nil:
raise SchemeError('Improper bindings provided.')
f.bindings[a.first] = b.first
a, b = a.rest, b.rest
return f
# END PROBLEM 10
##############
# Procedures #
##############
class Procedure(object):
"""The supertype of all Scheme procedures."""
def scheme_procedurep(x):
return isinstance(x, Procedure)
class BuiltinProcedure(Procedure):
"""A Scheme procedure defined as a Python function."""
def __init__(self, fn, use_env=False, name='builtin'):
self.name = name
self.fn = fn
self.use_env = use_env
def __str__(self):
return '#[{0}]'.format(self.name)
def apply(self, args, env):
"""Apply SELF to ARGS in ENV, where ARGS is a Scheme list.
>>> env = create_global_frame()
>>> plus = env.bindings['+']
>>> twos = Pair(2, Pair(2, nil))
>>> plus.apply(twos, env)
4
"""
if not scheme_listp(args):
raise SchemeError('arguments are not in a list: {0}'.format(args))
# Convert a Scheme list to a Python list
python_args = []
while args is not nil:
python_args.append(args.first)
args = args.rest
# BEGIN PROBLEM 3
if self.use_env:
python_args.append(env)
try:
return self.fn(*python_args)
except TypeError as e:
raise SchemeError(f"{e}: {len(python_args)} arguments passed in.")
# END PROBLEM 3
class LambdaProcedure(Procedure):
"""A procedure defined by a lambda expression or a define form."""
def __init__(self, formals, body, env):
"""A procedure with formal parameter list FORMALS (a Scheme list),
whose body is the Scheme list BODY, and whose parent environment
starts with Frame ENV."""
self.formals = formals
self.body = body
self.env = env
def make_call_frame(self, args, env):
"""Make a frame that binds my formal parameters to ARGS, a Scheme list
of values, for a lexically-scoped call evaluated in environment ENV."""
# BEGIN PROBLEM 11
"*** YOUR CODE HERE ***"
return self.env.make_child_frame(self.formals, args)
# END PROBLEM 11
def __str__(self):
return str(Pair('lambda', Pair(self.formals, self.body)))
def __repr__(self):
return 'LambdaProcedure({0}, {1}, {2})'.format(
repr(self.formals), repr(self.body), repr(self.env))
class MacroProcedure(LambdaProcedure):
"""A macro: a special form that operates on its unevaluated operands to
create an expression that is evaluated in place of a call."""
def apply_macro(self, operands, env):
"""Apply this macro to the operand expressions."""
return complete_apply(self, operands, env)
def add_builtins(frame, funcs_and_names):
"""Enter bindings in FUNCS_AND_NAMES into FRAME, an environment frame,
as built-in procedures. Each item in FUNCS_AND_NAMES has the form
(NAME, PYTHON-FUNCTION, INTERNAL-NAME)."""
for name, fn, proc_name in funcs_and_names:
frame.define(name, BuiltinProcedure(fn, name=proc_name))
#################
# Special Forms #
#################
# Each of the following do_xxx_form functions takes the cdr of a special form as
# its first argument---a Scheme list representing a special form without the
# initial identifying symbol (if, lambda, quote, ...). Its second argument is
# the environment in which the form is to be evaluated.
def do_define_form(expressions, env):
"""Evaluate a define form."""
check_form(expressions, 2)
target = expressions.first
if scheme_symbolp(target):
check_form(expressions, 2, 2)
# BEGIN PROBLEM 5
if self_evaluating(expressions.rest.first):
env.define(target, expressions.rest.first)
else:
env.define(target, scheme_eval(expressions.rest.first,env))
return target
# END PROBLEM 5
elif isinstance(target, Pair) and scheme_symbolp(target.first):
# BEGIN PROBLEM 9
env.define(target.first, do_lambda_form(Pair(target.rest, expressions.rest), env))
return target.first
# END PROBLEM 9
else:
bad_target = target.first if isinstance(target, Pair) else target
raise SchemeError('non-symbol: {0}'.format(bad_target))
def do_quote_form(expressions, env):
"""Evaluate a quote form."""
check_form(expressions, 1, 1)
# BEGIN PROBLEM 6
return expressions.first
# END PROBLEM 6
def do_begin_form(expressions, env):
"""Evaluate a begin form."""
check_form(expressions, 1)
return eval_all(expressions, env)
def do_lambda_form(expressions, env):
"""Evaluate a lambda form."""
check_form(expressions, 2)
formals = expressions.first
check_formals(formals)
# BEGIN PROBLEM 8
"*** YOUR CODE HERE ***"
return LambdaProcedure(formals, expressions.rest, env)
# END PROBLEM 8
def do_if_form(expressions, env):
"""Evaluate an if form."""
check_form(expressions, 2, 3)
if scheme_truep(scheme_eval(expressions.first, env)):
return scheme_eval(expressions.rest.first, env, True)
elif len(expressions) == 3:
return scheme_eval(expressions.rest.rest.first, env, True)
def do_and_form(expressions, env):
"""Evaluate a (short-circuited) and form."""
# BEGIN PROBLEM 12
if expressions is nil:
return True
if expressions.rest == nil:
return scheme_eval(expressions.first,env, True)
evaluatedExpression = scheme_eval(expressions.first,env)
if scheme_truep(evaluatedExpression):
if expressions.rest == nil:
return evaluatedExpression
return do_and_form(expressions.rest, env)
return evaluatedExpression
# END PROBLEM 12
def do_or_form(expressions, env):
"""Evaluate a (short-circuited) or form."""
# BEGIN PROBLEM 12
if expressions is nil:
return False
if expressions.rest == nil:
return scheme_eval(expressions.first,env, True)
evaluatedExpression = scheme_eval(expressions.first,env)
if scheme_truep(evaluatedExpression):
return evaluatedExpression
return do_or_form(expressions.rest, env)
# END PROBLEM 12
def do_cond_form(expressions, env):
"""Evaluate a cond form."""
while expressions is not nil:
clause = expressions.first
check_form(clause, 1)
if clause.first == 'else':
test = True
if expressions.rest != nil:
raise SchemeError('else must be last')
else:
test = scheme_eval(clause.first, env)
if scheme_truep(test):
# BEGIN PROBLEM 13
if clause.rest is nil:
return test
return eval_all(clause.rest, env)
# END PROBLEM 13
expressions = expressions.rest
def do_let_form(expressions, env):
"""Evaluate a let form."""
check_form(expressions, 2)
let_env = make_let_frame(expressions.first, env)
return eval_all(expressions.rest, let_env)
def make_let_frame(bindings, env):
"""Create a child frame of ENV that contains the definitions given in
BINDINGS. The Scheme list BINDINGS must have the form of a proper bindings
list in a let expression: each item must be a list containing a symbol
and a Scheme expression."""
if not scheme_listp(bindings):
raise SchemeError('bad bindings list in let form')
# BEGIN PROBLEM 14
copy = bindings
formals, bindings = nil, nil
while copy is not nil:
check_form(copy.first, 2, 2)
formals = Pair(copy.first.first, formals)
bindings = Pair(scheme_eval(copy.first.rest.first,env), bindings)
copy = copy.rest
check_formals(formals)
return env.make_child_frame(formals, bindings)
# END PROBLEM 14
def do_define_macro(expressions, env):
"""Evaluate a define-macro form."""
# BEGIN Problem 20
check_form(expressions, 2)
target = expressions.first
if isinstance(target, Pair) and scheme_symbolp(target.first):
env.define(target.first, MacroProcedure(target.rest, expressions.rest, env))
return target.first
else:
bad_target = target.first if isinstance(target, Pair) else target
raise SchemeError('non-symbol: {0}'.format(bad_target))
# END Problem 20
def do_quasiquote_form(expressions, env):
"""Evaluate a quasiquote form with parameters EXPRESSIONS in
environment ENV."""
def quasiquote_item(val, env, level):
"""Evaluate Scheme expression VAL that is nested at depth LEVEL in
a quasiquote form in environment ENV."""
if not scheme_pairp(val):
return val
if val.first == 'unquote':
level -= 1
if level == 0:
expressions = val.rest
check_form(expressions, 1, 1)
return scheme_eval(expressions.first, env)
elif val.first == 'quasiquote':
level += 1
return val.map(lambda elem: quasiquote_item(elem, env, level))
check_form(expressions, 1, 1)
return quasiquote_item(expressions.first, env, 1)
def do_unquote(expressions, env):
raise SchemeError('unquote outside of quasiquote')
SPECIAL_FORMS = {
'and': do_and_form,
'begin': do_begin_form,
'cond': do_cond_form,
'define': do_define_form,
'if': do_if_form,
'lambda': do_lambda_form,
'let': do_let_form,
'or': do_or_form,
'quote': do_quote_form,
'define-macro': do_define_macro,
'quasiquote': do_quasiquote_form,
'unquote': do_unquote,
}
# Utility methods for checking the structure of Scheme programs
def check_form(expr, min, max=float('inf')):
"""Check 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.
>>> check_form(read_line('(a b)'), 2)
"""
if not scheme_listp(expr):
raise SchemeError('badly formed expression: ' + repl_str(expr))
length = len(expr)
if length < min:
raise SchemeError('too few operands in form')
elif 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. Raise a SchemeError if the list of
formals is not a list of symbols or if any symbol is repeated.
>>> check_formals(read_line('(a b c)'))
"""
symbols = set()
def check_and_add(symbol, is_last):
if not scheme_symbolp(symbol):
raise SchemeError('non-symbol: {0}'.format(symbol))
if symbol in symbols:
raise SchemeError('duplicate symbol: {0}'.format(symbol))
symbols.add(symbol)
while isinstance(formals, Pair):
check_and_add(formals.first, formals.rest is nil)
formals = formals.rest
def check_procedure(procedure):
"""Check that PROCEDURE is a valid Scheme procedure."""
if not scheme_procedurep(procedure):
raise SchemeError('{0} is not callable: {1}'.format(
type(procedure).__name__.lower(), repl_str(procedure)))
#################
# Dynamic Scope #
#################
class MuProcedure(Procedure):
"""A procedure defined by a mu expression, which has dynamic scope.
_________________
< Scheme is cool! >
-----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
"""
def __init__(self, formals, body):
"""A procedure with formal parameter list FORMALS (a Scheme list) and
Scheme list BODY as its definition."""
self.formals = formals
self.body = body
# BEGIN PROBLEM 15
def make_call_frame(self, args, env):
return env.make_child_frame(self.formals, args)
# END PROBLEM 15
def __str__(self):
return str(Pair('mu', Pair(self.formals, self.body)))
def __repr__(self):
return 'MuProcedure({0}, {1})'.format(
repr(self.formals), repr(self.body))
def do_mu_form(expressions, env):
"""Evaluate a mu form."""
check_form(expressions, 2)
formals = expressions.first
check_formals(formals)
# BEGIN PROBLEM 15
return MuProcedure(formals, expressions.rest)
# END PROBLEM 15
SPECIAL_FORMS['mu'] = do_mu_form
###########
# Streams #
###########
class Promise(object):
"""A promise."""
def __init__(self, expression, env):
self.expression = expression
self.env = env
def evaluate(self):
if self.expression is not None:
value = scheme_eval(self.expression, self.env)
if not (value is nil or isinstance(value, Pair)):
raise SchemeError("result of forcing a promise should be a pair or nil, but was %s" % value)
self.value = value
self.expression = None
return self.value
def __str__(self):
return '#[promise ({0}forced)]'.format(
'not ' if self.expression is not None else '')
def do_delay_form(expressions, env):
"""Evaluates a delay form."""
check_form(expressions, 1, 1)
return Promise(expressions.first, env)
def do_cons_stream_form(expressions, env):
"""Evaluate a cons-stream form."""
check_form(expressions, 2, 2)
return Pair(scheme_eval(expressions.first, env),
do_delay_form(expressions.rest, env))
SPECIAL_FORMS['cons-stream'] = do_cons_stream_form
SPECIAL_FORMS['delay'] = do_delay_form
##################
# Tail Recursion #
##################
class Thunk(object):
"""An expression EXPR to be evaluated in environment ENV."""
def __init__(self, expr, env):
self.expr = expr
self.env = env
def __repr__(self):
return str(self.expr)
def complete_apply(procedure, args, env):
"""Apply procedure to args in env; ensure the result is not a Thunk."""
val = scheme_apply(procedure, args, env)
if isinstance(val, Thunk):
return scheme_eval(val.expr, val.env)
else:
return val
def optimize_tail_calls(original_scheme_eval):
"""Return a properly tail recursive version of an eval function."""
def optimized_eval(expr, env, tail=False):
"""Evaluate Scheme expression EXPR in environment ENV. If TAIL,
return a Thunk containing an expression for further evaluation.
"""
if tail and not scheme_symbolp(expr) and not self_evaluating(expr):
return Thunk(expr, env)
result = Thunk(expr, env)
# BEGIN
while isinstance(result, Thunk):
#print("DEBUG:", result)
result = original_scheme_eval(result.expr, result.env)
return result
# END
return optimized_eval
################################################################
# Uncomment the following line to apply tail call optimization #
################################################################
scheme_eval = optimize_tail_calls(scheme_eval)
####################
# Extra Procedures #
####################
def scheme_map(fn, s, env):
check_type(fn, scheme_procedurep, 0, 'map')
check_type(s, scheme_listp, 1, 'map')
return s.map(lambda x: complete_apply(fn, Pair(x, nil), env))
def scheme_filter(fn, s, env):
check_type(fn, scheme_procedurep, 0, 'filter')
check_type(s, scheme_listp, 1, 'filter')
head, current = nil, nil
while s is not nil:
item, s = s.first, s.rest
if complete_apply(fn, Pair(item, nil), env):
if head is nil:
head = Pair(item, nil)
current = head
else:
current.rest = Pair(item, nil)
current = current.rest
return head
def scheme_reduce(fn, s, env):
check_type(fn, scheme_procedurep, 0, 'reduce')
check_type(s, lambda x: x is not nil, 1, 'reduce')
check_type(s, scheme_listp, 1, 'reduce')
value, s = s.first, s.rest
while s is not nil:
value = complete_apply(fn, scheme_list(value, s.first), env)
s = s.rest
return value
################
# Input/Output #
################
def read_eval_print_loop(next_line, env, interactive=False, quiet=False,
startup=False, load_files=(), report_errors=False):
"""Read and evaluate input until an end of file or keyboard interrupt."""
if startup:
for filename in load_files:
scheme_load(filename, True, env)
while True:
try:
src = next_line()
while src.more_on_line:
expression = scheme_read(src)
result = scheme_eval(expression, env)
if not quiet and result is not None:
print(repl_str(result))
except (SchemeError, SyntaxError, ValueError, RuntimeError) as err:
if report_errors:
if isinstance(err, SyntaxError):
err = SchemeError(err)
raise err
if (isinstance(err, RuntimeError) and
'maximum recursion depth exceeded' not in getattr(err, 'args')[0]):
raise
elif isinstance(err, RuntimeError):
print('Error: maximum recursion depth exceeded')
else:
print('Error:', err)
except KeyboardInterrupt: # <Control>-C
if not startup:
raise
print()
print('KeyboardInterrupt')
if not interactive:
return
except EOFError: # <Control>-D, etc.
print()
return
def scheme_load(*args):
"""Load a Scheme source file. ARGS should be of the form (SYM, ENV) or
(SYM, QUIET, ENV). The file named SYM is loaded into environment ENV,
with verbosity determined by QUIET (default true)."""
if not (2 <= len(args) <= 3):
expressions = args[:-1]
raise SchemeError('"load" given incorrect number of arguments: '
'{0}'.format(len(expressions)))
sym = args[0]
quiet = args[1] if len(args) > 2 else True
env = args[-1]
if (scheme_stringp(sym)):
sym = eval(sym)
check_type(sym, scheme_symbolp, 0, 'load')
with scheme_open(sym) as infile:
lines = infile.readlines()
args = (lines, None) if quiet else (lines,)
def next_line():
return buffer_lines(*args)
read_eval_print_loop(next_line, env, quiet=quiet, report_errors=True)
def scheme_load_all(directory, env):
"""
Loads all .scm files in the given directory, alphabetically. Used only
in tests/ code.
"""
assert scheme_stringp(directory)
directory = eval(directory)
import os
for x in sorted(os.listdir(".")):
if not x.endswith(".scm"):
continue
scheme_load(x, env)
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',
BuiltinProcedure(scheme_eval, True, 'eval'))
env.define('apply',
BuiltinProcedure(complete_apply, True, 'apply'))
env.define('load',
BuiltinProcedure(scheme_load, True, 'load'))
env.define('load-all',
BuiltinProcedure(scheme_load_all, True, 'load-all'))
env.define('procedure?',
BuiltinProcedure(scheme_procedurep, False, 'procedure?'))
env.define('map',
BuiltinProcedure(scheme_map, True, 'map'))
env.define('filter',
BuiltinProcedure(scheme_filter, True, 'filter'))
env.define('reduce',
BuiltinProcedure(scheme_reduce, True, 'reduce'))
env.define('undefined', None)
add_builtins(env, BUILTINS)
return env
@main
def run(*argv):
import argparse
parser = argparse.ArgumentParser(description='CS 61A Scheme Interpreter')
parser.add_argument('-load', '-i', action='store_true',
help='run file interactively')
parser.add_argument('file', nargs='?',
type=argparse.FileType('r'), default=None,
help='Scheme file to run')
args = parser.parse_args()
next_line = buffer_input
interactive = True
load_files = []
if args.file is not None:
if args.load:
load_files.append(getattr(args.file, 'name'))
else:
lines = args.file.readlines()
def next_line():
return buffer_lines(lines)
interactive = False
read_eval_print_loop(next_line, create_global_frame(), startup=True,
interactive=interactive, load_files=load_files)
tscheme_exitonclick()