-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheme.py.html
469 lines (401 loc) · 57.8 KB
/
scheme.py.html
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
<html>
<head>
<title>scheme.py</title>
<link href="css/assignments.css" rel="stylesheet" type="text/css">
</head>
<body>
<h3>scheme.py (<a href="scheme.py">plain text</a>)</h3>
<hr>
<pre>
<span style="color: darkred">"""This module implements the core Scheme interpreter functions, including the
eval/apply mutual recurrence, environment model, and read-eval-print loop.
"""
</span><span style="color: blue; font-weight: bold">from </span>scheme_primitives <span style="color: blue; font-weight: bold">import </span><span style="font-weight: bold">*
</span><span style="color: blue; font-weight: bold">from </span>scheme_reader <span style="color: blue; font-weight: bold">import </span><span style="font-weight: bold">*
</span><span style="color: blue; font-weight: bold">from </span>ucb <span style="color: blue; font-weight: bold">import </span>main<span style="font-weight: bold">, </span>trace
<span style="color: green; font-style: italic">##############
# Eval/Apply #
##############
</span><span style="color: blue; font-weight: bold">def </span>scheme_eval<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""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
"""
</span><span style="color: blue; font-weight: bold">if </span>expr <span style="color: blue; font-weight: bold">is </span><span style="color: blue">None</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"Cannot evaluate an undefined expression."</span><span style="font-weight: bold">)
</span><span style="color: green; font-style: italic"># Evaluate Atoms
</span><span style="color: blue; font-weight: bold">if </span>scheme_symbolp<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span>env<span style="font-weight: bold">.</span>lookup<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>scheme_atomp<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">or </span>scheme_stringp<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">or </span>expr <span style="color: blue; font-weight: bold">is </span>okay<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>expr
<span style="color: green; font-style: italic"># All non-atomic expressions are lists.
</span><span style="color: blue; font-weight: bold">if not </span>scheme_listp<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"malformed list: {0}"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">)))
</span>first<span style="font-weight: bold">, </span>rest <span style="font-weight: bold">= </span>expr<span style="font-weight: bold">.</span>first<span style="font-weight: bold">, </span>expr<span style="font-weight: bold">.</span>second
<span style="color: green; font-style: italic"># Evaluate Combinations
</span><span style="color: blue; font-weight: bold">if </span><span style="font-weight: bold">(</span>scheme_symbolp<span style="font-weight: bold">(</span>first<span style="font-weight: bold">) </span><span style="color: green; font-style: italic"># first might be unhashable
</span><span style="color: blue; font-weight: bold">and </span>first <span style="color: blue; font-weight: bold">in </span>LOGIC_FORMS<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span>scheme_eval<span style="font-weight: bold">(</span>LOGIC_FORMS<span style="font-weight: bold">[</span>first<span style="font-weight: bold">](</span>rest<span style="font-weight: bold">, </span>env<span style="font-weight: bold">), </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"lambda"</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>do_lambda_form<span style="font-weight: bold">(</span>rest<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"mu"</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>do_mu_form<span style="font-weight: bold">(</span>rest<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"define"</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>do_define_form<span style="font-weight: bold">(</span>rest<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"quote"</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>do_quote_form<span style="font-weight: bold">(</span>rest<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"let"</span><span style="font-weight: bold">:
</span>expr<span style="font-weight: bold">, </span>env <span style="font-weight: bold">= </span>do_let_form<span style="font-weight: bold">(</span>rest<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span>scheme_eval<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span>procedure <span style="font-weight: bold">= </span>scheme_eval<span style="font-weight: bold">(</span>first<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span>args <span style="font-weight: bold">= </span>rest<span style="font-weight: bold">.</span>map<span style="font-weight: bold">(</span><span style="color: blue; font-weight: bold">lambda </span>operand<span style="font-weight: bold">: </span>scheme_eval<span style="font-weight: bold">(</span>operand<span style="font-weight: bold">, </span>env<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">return </span>scheme_apply<span style="font-weight: bold">(</span>procedure<span style="font-weight: bold">, </span>args<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>scheme_apply<span style="font-weight: bold">(</span>procedure<span style="font-weight: bold">, </span>args<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Apply Scheme PROCEDURE to argument values ARGS in environment ENV."""
</span><span style="color: blue; font-weight: bold">if </span>isinstance<span style="font-weight: bold">(</span>procedure<span style="font-weight: bold">, </span>PrimitiveProcedure<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span>apply_primitive<span style="font-weight: bold">(</span>procedure<span style="font-weight: bold">, </span>args<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>isinstance<span style="font-weight: bold">(</span>procedure<span style="font-weight: bold">, </span>LambdaProcedure<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">elif </span>isinstance<span style="font-weight: bold">(</span>procedure<span style="font-weight: bold">, </span>MuProcedure<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"Cannot call {0}"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span>procedure<span style="font-weight: bold">)))
</span><span style="color: blue; font-weight: bold">def </span>apply_primitive<span style="font-weight: bold">(</span>procedure<span style="font-weight: bold">, </span>args<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""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
"""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: green; font-style: italic">################
# Environments #
################
</span><span style="color: blue; font-weight: bold">class </span>Frame<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):
</span><span style="color: darkred">"""An environment frame binds Scheme symbols to Scheme values."""
</span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>parent<span style="font-weight: bold">):
</span><span style="color: darkred">"""An empty frame with a PARENT frame (that may be None)."""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bindings <span style="font-weight: bold">= {}
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>parent <span style="font-weight: bold">= </span>parent
<span style="color: blue; font-weight: bold">def </span>__repr__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>parent <span style="color: blue; font-weight: bold">is </span><span style="color: blue">None</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span><span style="color: red">"<Global Frame>"
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span>s <span style="font-weight: bold">= </span>sorted<span style="font-weight: bold">(</span><span style="color: red">'{0}: {1}'</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>k<span style="font-weight: bold">,</span>v<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>k<span style="font-weight: bold">,</span>v <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bindings<span style="font-weight: bold">.</span>items<span style="font-weight: bold">())
</span><span style="color: blue; font-weight: bold">return </span><span style="color: red">"<{{{0}}} -> {1}>"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span><span style="color: red">', '</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>s<span style="font-weight: bold">), </span>repr<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>parent<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">def </span>lookup<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>symbol<span style="font-weight: bold">):
</span><span style="color: darkred">"""Return the value bound to SYMBOL. Errors if SYMBOL is not found."""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"unknown identifier: {0}"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span>symbol<span style="font-weight: bold">)))
</span><span style="color: blue; font-weight: bold">def </span>global_frame<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""The global environment at the root of the parent chain."""
</span>e <span style="font-weight: bold">= </span><span style="color: blue">self
</span><span style="color: blue; font-weight: bold">while </span>e<span style="font-weight: bold">.</span>parent <span style="color: blue; font-weight: bold">is not </span><span style="color: blue">None</span><span style="font-weight: bold">:
</span>e <span style="font-weight: bold">= </span>e<span style="font-weight: bold">.</span>parent
<span style="color: blue; font-weight: bold">return </span>e
<span style="color: blue; font-weight: bold">def </span>make_call_frame<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>formals<span style="font-weight: bold">, </span>vals<span style="font-weight: bold">):
</span><span style="color: darkred">"""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. Raise an error if too many or too
few arguments are given.
>>> 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>>
"""
</span>frame <span style="font-weight: bold">= </span>Frame<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">return </span>frame
<span style="color: blue; font-weight: bold">def </span>define<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>sym<span style="font-weight: bold">, </span>val<span style="font-weight: bold">):
</span><span style="color: darkred">"""Define Scheme symbol SYM to have value VAL in SELF."""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bindings<span style="font-weight: bold">[</span>sym<span style="font-weight: bold">] = </span>val
<span style="color: blue; font-weight: bold">class </span>LambdaProcedure<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):
</span><span style="color: darkred">"""A procedure defined by a lambda expression or the complex define form."""
</span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>formals<span style="font-weight: bold">, </span>body<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""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."""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>formals <span style="font-weight: bold">= </span>formals
<span style="color: blue">self</span><span style="font-weight: bold">.</span>body <span style="font-weight: bold">= </span>body
<span style="color: blue">self</span><span style="font-weight: bold">.</span>env <span style="font-weight: bold">= </span>env
<span style="color: blue; font-weight: bold">def </span>__str__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span><span style="color: red">"(lambda {0} {1})"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>formals<span style="font-weight: bold">), </span>str<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>body<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">def </span>__repr__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span>args <span style="font-weight: bold">= (</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>formals<span style="font-weight: bold">, </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>body<span style="font-weight: bold">, </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span><span style="color: red">"LambdaProcedure({0}, {1}, {2})"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(*(</span>repr<span style="font-weight: bold">(</span>a<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>a <span style="color: blue; font-weight: bold">in </span>args<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">class </span>MuProcedure<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):
</span><span style="color: darkred">"""A procedure defined by a mu expression, which has dynamic scope.
_________________
< Scheme is cool! >
-----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
"""
</span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>formals<span style="font-weight: bold">, </span>body<span style="font-weight: bold">):
</span><span style="color: darkred">"""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."""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>formals <span style="font-weight: bold">= </span>formals
<span style="color: blue">self</span><span style="font-weight: bold">.</span>body <span style="font-weight: bold">= </span>body
<span style="color: blue; font-weight: bold">def </span>__str__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span><span style="color: red">"(mu {0} {1})"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>formals<span style="font-weight: bold">), </span>str<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>body<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">def </span>__repr__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span>args <span style="font-weight: bold">= (</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>formals<span style="font-weight: bold">, </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>body<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span><span style="color: red">"MuProcedure({0}, {1})"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(*(</span>repr<span style="font-weight: bold">(</span>a<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>a <span style="color: blue; font-weight: bold">in </span>args<span style="font-weight: bold">))
</span><span style="color: green; font-style: italic">#################
# Special forms #
#################
</span><span style="color: blue; font-weight: bold">def </span>do_lambda_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate a lambda form with parameters VALS in environment ENV."""
</span>check_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">)
</span>formals <span style="font-weight: bold">= </span>vals<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
</span>check_formals<span style="font-weight: bold">(</span>formals<span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>do_mu_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate a mu form with parameters VALS."""
</span>check_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">)
</span>formals <span style="font-weight: bold">= </span>vals<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
</span>check_formals<span style="font-weight: bold">(</span>formals<span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>do_define_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate a define form with parameters VALS in environment ENV."""
</span>check_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">)
</span>target <span style="font-weight: bold">= </span>vals<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">if </span>scheme_symbolp<span style="font-weight: bold">(</span>target<span style="font-weight: bold">):
</span>check_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">elif </span>isinstance<span style="font-weight: bold">(</span>target<span style="font-weight: bold">, </span>Pair<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"bad argument to define"</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>do_quote_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate a quote form with parameters VALS."""
</span>check_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>do_let_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate a let form with parameters VALS in environment ENV."""
</span>check_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">)
</span>bindings <span style="font-weight: bold">= </span>vals<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
</span>exprs <span style="font-weight: bold">= </span>vals<span style="font-weight: bold">.</span>second
<span style="color: blue; font-weight: bold">if not </span>scheme_listp<span style="font-weight: bold">(</span>bindings<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"bad bindings list in let form"</span><span style="font-weight: bold">)
</span><span style="color: green; font-style: italic"># Add a frame containing bindings
</span>names<span style="font-weight: bold">, </span>values <span style="font-weight: bold">= </span>nil<span style="font-weight: bold">, </span>nil
<span style="color: red">"*** YOUR CODE HERE ***"
</span>new_env <span style="font-weight: bold">= </span>env<span style="font-weight: bold">.</span>make_call_frame<span style="font-weight: bold">(</span>names<span style="font-weight: bold">, </span>values<span style="font-weight: bold">)
</span><span style="color: green; font-style: italic"># Evaluate all but the last expression after bindings, and return the last
</span>last <span style="font-weight: bold">= </span>len<span style="font-weight: bold">(</span>exprs<span style="font-weight: bold">)-</span><span style="color: red">1
</span><span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">, </span>last<span style="font-weight: bold">):
</span>scheme_eval<span style="font-weight: bold">(</span>exprs<span style="font-weight: bold">[</span>i<span style="font-weight: bold">], </span>new_env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span>exprs<span style="font-weight: bold">[</span>last<span style="font-weight: bold">], </span>new_env
<span style="color: green; font-style: italic">#########################
# Logical Special Forms #
#########################
</span><span style="color: blue; font-weight: bold">def </span>do_if_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate if form with parameters VALS in environment ENV."""
</span>check_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">3</span><span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>do_and_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate short-circuited and with parameters VALS in environment ENV."""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>quote<span style="font-weight: bold">(</span>value<span style="font-weight: bold">):
</span><span style="color: darkred">"""Return a Scheme expression quoting the Scheme VALUE.
>>> s = quote('hello')
>>> print(s)
(quote hello)
>>> scheme_eval(s, Frame(None)) # "hello" is undefined in this frame.
'hello'
"""
</span><span style="color: blue; font-weight: bold">return </span>Pair<span style="font-weight: bold">(</span><span style="color: red">"quote"</span><span style="font-weight: bold">, </span>Pair<span style="font-weight: bold">(</span>value<span style="font-weight: bold">, </span>nil<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">def </span>do_or_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate short-circuited or with parameters VALS in environment ENV."""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>do_cond_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate cond form with parameters VALS in environment ENV."""
</span>num_clauses <span style="font-weight: bold">= </span>len<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">for </span>i<span style="font-weight: bold">, </span>clause <span style="color: blue; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">):
</span>check_form<span style="font-weight: bold">(</span>clause<span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">if </span>clause<span style="font-weight: bold">.</span>first <span style="font-weight: bold">== </span><span style="color: red">"else"</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">if </span>i <span style="font-weight: bold">< </span>num_clauses<span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"else must be last"</span><span style="font-weight: bold">)
</span>test <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">True
if </span>clause<span style="font-weight: bold">.</span>second <span style="color: blue; font-weight: bold">is </span>nil<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"badly formed else clause"</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span>test <span style="font-weight: bold">= </span>scheme_eval<span style="font-weight: bold">(</span>clause<span style="font-weight: bold">.</span>first<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">if </span>scheme_true<span style="font-weight: bold">(</span>test<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">return </span>okay
<span style="color: blue; font-weight: bold">def </span>do_begin_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate begin form with parameters VALS in environment ENV."""
</span>check_form<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span>LOGIC_FORMS <span style="font-weight: bold">= {
</span><span style="color: red">"and"</span><span style="font-weight: bold">: </span>do_and_form<span style="font-weight: bold">,
</span><span style="color: red">"or"</span><span style="font-weight: bold">: </span>do_or_form<span style="font-weight: bold">,
</span><span style="color: red">"if"</span><span style="font-weight: bold">: </span>do_if_form<span style="font-weight: bold">,
</span><span style="color: red">"cond"</span><span style="font-weight: bold">: </span>do_cond_form<span style="font-weight: bold">,
</span><span style="color: red">"begin"</span><span style="font-weight: bold">: </span>do_begin_form<span style="font-weight: bold">,
}
</span><span style="color: green; font-style: italic"># Utility methods for checking the structure of Scheme programs
</span><span style="color: blue; font-weight: bold">def </span>check_form<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">, </span>min<span style="font-weight: bold">, </span>max <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""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."""
</span><span style="color: blue; font-weight: bold">if not </span>scheme_listp<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"badly formed expression: " </span><span style="font-weight: bold">+ </span>str<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">))
</span>length <span style="font-weight: bold">= </span>len<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">if </span>length <span style="font-weight: bold">< </span>min<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"too few operands in form"</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>max <span style="color: blue; font-weight: bold">is not </span><span style="color: blue">None </span><span style="color: blue; font-weight: bold">and </span>length <span style="font-weight: bold">> </span>max<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"too many operands in form"</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>check_formals<span style="font-weight: bold">(</span>formals<span style="font-weight: bold">):
</span><span style="color: darkred">"""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)"))
"""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: green; font-style: italic">##################
# Tail Recursion #
##################
</span><span style="color: blue; font-weight: bold">def </span>scheme_optimized_eval<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">, </span>env<span style="font-weight: bold">):
</span><span style="color: darkred">"""Evaluate Scheme expression EXPR in environment ENV."""
</span><span style="color: blue; font-weight: bold">while True</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">if </span>expr <span style="color: blue; font-weight: bold">is </span><span style="color: blue">None</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"Cannot evaluate an undefined expression."</span><span style="font-weight: bold">)
</span><span style="color: green; font-style: italic"># Evaluate Atoms
</span><span style="color: blue; font-weight: bold">if </span>scheme_symbolp<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span>env<span style="font-weight: bold">.</span>lookup<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>scheme_atomp<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">or </span>scheme_stringp<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">or </span>expr <span style="color: blue; font-weight: bold">is </span>okay<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>expr
<span style="color: green; font-style: italic"># All non-atomic expressions are lists.
</span><span style="color: blue; font-weight: bold">if not </span>scheme_listp<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"malformed list: {0}"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span>expr<span style="font-weight: bold">)))
</span>first<span style="font-weight: bold">, </span>rest <span style="font-weight: bold">= </span>expr<span style="font-weight: bold">.</span>first<span style="font-weight: bold">, </span>expr<span style="font-weight: bold">.</span>second
<span style="color: green; font-style: italic"># Evaluate Combinations
</span><span style="color: blue; font-weight: bold">if </span><span style="font-weight: bold">(</span>scheme_symbolp<span style="font-weight: bold">(</span>first<span style="font-weight: bold">) </span><span style="color: green; font-style: italic"># first might be unhashable
</span><span style="color: blue; font-weight: bold">and </span>first <span style="color: blue; font-weight: bold">in </span>LOGIC_FORMS<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"lambda"</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>do_lambda_form<span style="font-weight: bold">(</span>rest<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"mu"</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>do_mu_form<span style="font-weight: bold">(</span>rest<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"define"</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>do_define_form<span style="font-weight: bold">(</span>rest<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"quote"</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>do_quote_form<span style="font-weight: bold">(</span>rest<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">elif </span>first <span style="font-weight: bold">== </span><span style="color: red">"let"</span><span style="font-weight: bold">:
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: green; font-style: italic">################################################################
# Uncomment the following line to apply tail call optimization #
################################################################
# scheme_eval = scheme_optimized_eval
################
# Input/Output #
################
</span><span style="color: blue; font-weight: bold">def </span>read_eval_print_loop<span style="font-weight: bold">(</span>next_line<span style="font-weight: bold">, </span>env<span style="font-weight: bold">, </span>quiet<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>startup<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">,
</span>interactive<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>load_files<span style="font-weight: bold">=()):
</span><span style="color: darkred">"""Read and evaluate input until an end of file or keyboard interrupt."""
</span><span style="color: blue; font-weight: bold">if </span>startup<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">for </span>filename <span style="color: blue; font-weight: bold">in </span>load_files<span style="font-weight: bold">:
</span>scheme_load<span style="font-weight: bold">(</span>filename<span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">while True</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">:
</span>src <span style="font-weight: bold">= </span>next_line<span style="font-weight: bold">()
</span><span style="color: blue; font-weight: bold">while </span>src<span style="font-weight: bold">.</span>more_on_line<span style="font-weight: bold">:
</span>expression <span style="font-weight: bold">= </span>scheme_read<span style="font-weight: bold">(</span>src<span style="font-weight: bold">)
</span>result <span style="font-weight: bold">= </span>scheme_eval<span style="font-weight: bold">(</span>expression<span style="font-weight: bold">, </span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">if not </span>quiet<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">print</span><span style="font-weight: bold">(</span>result<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">except </span><span style="font-weight: bold">(</span>SchemeError<span style="font-weight: bold">, </span>SyntaxError<span style="font-weight: bold">, </span>ValueError<span style="font-weight: bold">, </span>RuntimeError<span style="font-weight: bold">) </span>as err<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">if </span><span style="font-weight: bold">(</span>isinstance<span style="font-weight: bold">(</span>err<span style="font-weight: bold">, </span>RuntimeError<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">and
</span><span style="color: red">'maximum recursion depth exceeded' </span><span style="color: blue; font-weight: bold">not in </span>err<span style="font-weight: bold">.</span>args<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]):
</span><span style="color: blue; font-weight: bold">raise
print</span><span style="font-weight: bold">(</span><span style="color: red">"Error:"</span><span style="font-weight: bold">, </span>err<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">except </span>KeyboardInterrupt<span style="font-weight: bold">: </span><span style="color: green; font-style: italic"># <Control>-C
</span><span style="color: blue; font-weight: bold">if not </span>startup<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise
print</span><span style="font-weight: bold">(</span><span style="color: red">"\nKeyboardInterrupt"</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">if not </span>interactive<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return
except </span>EOFError<span style="font-weight: bold">: </span><span style="color: green; font-style: italic"># <Control>-D, etc.
</span><span style="color: blue; font-weight: bold">return
def </span>scheme_load<span style="font-weight: bold">(*</span>args<span style="font-weight: bold">):
</span><span style="color: darkred">"""Load a Scheme source file. ARGS should be of the form (SYM, ENV) or (SYM,
QUIET, ENV). The file named SYM is loaded in environment ENV, with verbosity
determined by QUIET (default true)."""
</span><span style="color: blue; font-weight: bold">if not </span><span style="font-weight: bold">(</span><span style="color: red">2 </span><span style="font-weight: bold"><= </span>len<span style="font-weight: bold">(</span>args<span style="font-weight: bold">) <= </span><span style="color: red">3</span><span style="font-weight: bold">):
</span>vals <span style="font-weight: bold">= </span>args<span style="font-weight: bold">[:-</span><span style="color: red">1</span><span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span><span style="color: red">"wrong number of arguments to load: {0}"</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>vals<span style="font-weight: bold">))
</span>sym <span style="font-weight: bold">= </span>args<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
</span>quiet <span style="font-weight: bold">= </span>args<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] </span><span style="color: blue; font-weight: bold">if </span>len<span style="font-weight: bold">(</span>args<span style="font-weight: bold">) > </span><span style="color: red">2 </span><span style="color: blue; font-weight: bold">else True
</span>env <span style="font-weight: bold">= </span>args<span style="font-weight: bold">[-</span><span style="color: red">1</span><span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">if </span><span style="font-weight: bold">(</span>scheme_stringp<span style="font-weight: bold">(</span>sym<span style="font-weight: bold">)):
</span>sym <span style="font-weight: bold">= </span>eval<span style="font-weight: bold">(</span>sym<span style="font-weight: bold">)
</span>check_type<span style="font-weight: bold">(</span>sym<span style="font-weight: bold">, </span>scheme_symbolp<span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">"load"</span><span style="font-weight: bold">)
</span>with scheme_open<span style="font-weight: bold">(</span>sym<span style="font-weight: bold">) </span>as infile<span style="font-weight: bold">:
</span>lines <span style="font-weight: bold">= </span>infile<span style="font-weight: bold">.</span>readlines<span style="font-weight: bold">()
</span>args <span style="font-weight: bold">= (</span>lines<span style="font-weight: bold">, </span><span style="color: blue">None</span><span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">if </span>quiet <span style="color: blue; font-weight: bold">else </span><span style="font-weight: bold">(</span>lines<span style="font-weight: bold">,)
</span><span style="color: blue; font-weight: bold">def </span>next_line<span style="font-weight: bold">():
</span><span style="color: blue; font-weight: bold">return </span>buffer_lines<span style="font-weight: bold">(*</span>args<span style="font-weight: bold">)
</span>read_eval_print_loop<span style="font-weight: bold">(</span>next_line<span style="font-weight: bold">, </span>env<span style="font-weight: bold">.</span>global_frame<span style="font-weight: bold">(), </span>quiet<span style="font-weight: bold">=</span>quiet<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span>okay
<span style="color: blue; font-weight: bold">def </span>scheme_open<span style="font-weight: bold">(</span>filename<span style="font-weight: bold">):
</span><span style="color: darkred">"""If either FILENAME or FILENAME.scm is the name of a valid file,
return a Python file opened to it. Otherwise, raise an error."""
</span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>open<span style="font-weight: bold">(</span>filename<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">except </span>IOError as exc<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">if </span>filename<span style="font-weight: bold">.</span>endswith<span style="font-weight: bold">(</span><span style="color: red">'.scm'</span><span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span>exc<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">return </span>open<span style="font-weight: bold">(</span>filename <span style="font-weight: bold">+ </span><span style="color: red">'.scm'</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">except </span>IOError as exc<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">raise </span>SchemeError<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span>exc<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">def </span>create_global_frame<span style="font-weight: bold">():
</span><span style="color: darkred">"""Initialize and return a single-frame environment with built-in names."""
</span>env <span style="font-weight: bold">= </span>Frame<span style="font-weight: bold">(</span><span style="color: blue">None</span><span style="font-weight: bold">)
</span>env<span style="font-weight: bold">.</span>define<span style="font-weight: bold">(</span><span style="color: red">"eval"</span><span style="font-weight: bold">, </span>PrimitiveProcedure<span style="font-weight: bold">(</span>scheme_eval<span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">))
</span>env<span style="font-weight: bold">.</span>define<span style="font-weight: bold">(</span><span style="color: red">"apply"</span><span style="font-weight: bold">, </span>PrimitiveProcedure<span style="font-weight: bold">(</span>scheme_apply<span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">))
</span>env<span style="font-weight: bold">.</span>define<span style="font-weight: bold">(</span><span style="color: red">"load"</span><span style="font-weight: bold">, </span>PrimitiveProcedure<span style="font-weight: bold">(</span>scheme_load<span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">))
</span>add_primitives<span style="font-weight: bold">(</span>env<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span>env
@main
<span style="color: blue; font-weight: bold">def </span>run<span style="font-weight: bold">(*</span>argv<span style="font-weight: bold">):
</span>next_line <span style="font-weight: bold">= </span>buffer_input
interactive <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">True
</span>load_files <span style="font-weight: bold">= ()
</span><span style="color: blue; font-weight: bold">if </span>argv<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">:
</span>filename <span style="font-weight: bold">= </span>argv<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">if </span>filename <span style="font-weight: bold">== </span><span style="color: red">'-load'</span><span style="font-weight: bold">:
</span>load_files <span style="font-weight: bold">= </span>argv<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">:]
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span>input_file <span style="font-weight: bold">= </span>open<span style="font-weight: bold">(</span>argv<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">])
</span>lines <span style="font-weight: bold">= </span>input_file<span style="font-weight: bold">.</span>readlines<span style="font-weight: bold">()
</span><span style="color: blue; font-weight: bold">def </span>next_line<span style="font-weight: bold">():
</span><span style="color: blue; font-weight: bold">return </span>buffer_lines<span style="font-weight: bold">(</span>lines<span style="font-weight: bold">)
</span>interactive <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
except </span>IOError as err<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">print</span><span style="font-weight: bold">(</span>err<span style="font-weight: bold">)
</span>sys<span style="font-weight: bold">.</span>exit<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">)
</span>read_eval_print_loop<span style="font-weight: bold">(</span>next_line<span style="font-weight: bold">, </span>create_global_frame<span style="font-weight: bold">(), </span>startup<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">,
</span>interactive<span style="font-weight: bold">=</span>interactive<span style="font-weight: bold">, </span>load_files<span style="font-weight: bold">=</span>load_files<span style="font-weight: bold">)
</span>tscheme_exitonclick<span style="font-weight: bold">()
</span>
</pre>
</body>
</html>