-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheme_forms.py
291 lines (247 loc) · 8.84 KB
/
scheme_forms.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
from scheme_eval_apply import *
from scheme_utils import *
from scheme_classes import *
from scheme_builtins import *
#################
# 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.
>>> env = create_global_frame()
>>> do_define_form(read_line("(x 2)"), env) # evaluating (define x 2)
'x'
>>> scheme_eval("x", env)
2
>>> do_define_form(read_line("(x (+ 2 8))"), env) # evaluating (define x (+ 2 8))
'x'
>>> scheme_eval("x", env)
10
>>> # problem 10
>>> env = create_global_frame()
>>> do_define_form(read_line("((f x) (+ x 2))"), env) # evaluating (define (f x) (+ x 8))
'f'
>>> scheme_eval(read_line("(f 3)"), env)
5
"""
validate_form(expressions, 2) # Checks that expressions is a list of length at least 2
signature = expressions.first
if scheme_symbolp(signature):
# assigning a name to a value e.g. (define x (+ 1 2))
validate_form(expressions, 2, 2) # Checks that expressions is a list of length exactly 2
# BEGIN PROBLEM 4
value = scheme_eval(expressions.rest.first,env)
env.define(signature,value)
return signature
# END PROBLEM 4
elif isinstance(signature, Pair) and scheme_symbolp(signature.first):
# defining a named procedure e.g. (define (f x y) (+ x y))
# BEGIN PROBLEM 10
name = signature.first
args = Pair(signature.rest,expressions.rest)
env.define(name,do_lambda_form(args,env))
return name
# END PROBLEM 10
else:
bad_signature = signature.first if isinstance(signature, Pair) else signature
raise SchemeError('non-symbol: {0}'.format(bad_signature))
def do_quote_form(expressions, env):
"""Evaluate a quote form.
>>> env = create_global_frame()
>>> do_quote_form(read_line("((+ x 2))"), env) # evaluating (quote (+ x 2))
Pair('+', Pair('x', Pair(2, nil)))
"""
validate_form(expressions, 1, 1)
# BEGIN PROBLEM 5
return expressions.first
# END PROBLEM 5
def do_begin_form(expressions, env):
"""Evaluate a begin form.
>>> env = create_global_frame()
>>> x = do_begin_form(read_line("((print 2) 3)"), env) # evaluating (begin (print 2) 3)
2
>>> x
3
"""
validate_form(expressions, 1)
return eval_all(expressions, env)
def do_lambda_form(expressions, env):
"""Evaluate a lambda form.
>>> env = create_global_frame()
>>> do_lambda_form(read_line("((x) (+ x 2))"), env) # evaluating (lambda (x) (+ x 2))
LambdaProcedure(Pair('x', nil), Pair(Pair('+', Pair('x', Pair(2, nil))), nil), <Global Frame>)
"""
validate_form(expressions, 2)
formals = expressions.first
validate_formals(formals)
# BEGIN PROBLEM 7
return LambdaProcedure(formals,expressions.rest,env)
# END PROBLEM 7
def do_if_form(expressions, env):
"""Evaluate an if form.
>>> env = create_global_frame()
>>> do_if_form(read_line("(#t (print 2) (print 3))"), env) # evaluating (if #t (print 2) (print 3))
2
>>> do_if_form(read_line("(#f (print 2) (print 3))"), env) # evaluating (if #f (print 2) (print 3))
3
"""
validate_form(expressions, 2, 3)
if is_scheme_true(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.
>>> env = create_global_frame()
>>> do_and_form(read_line("(#f (print 1))"), env) # evaluating (and #f (print 1))
False
>>> # evaluating (and (print 1) (print 2) (print 4) 3 #f)
>>> do_and_form(read_line("((print 1) (print 2) (print 3) (print 4) 3 #f)"), env)
1
2
3
4
False
"""
# BEGIN PROBLEM 12
if expressions == nil:
return True
while expressions.rest:
elem = scheme_eval(expressions.first,env)
if is_scheme_false(elem):
return elem
expressions = expressions.rest
return scheme_eval(expressions.first,env,True)
# END PROBLEM 12
def do_or_form(expressions, env):
"""Evaluate a (short-circuited) or form.
>>> env = create_global_frame()
>>> do_or_form(read_line("(10 (print 1))"), env) # evaluating (or 10 (print 1))
10
>>> do_or_form(read_line("(#f 2 3 #t #f)"), env) # evaluating (or #f 2 3 #t #f)
2
>>> # evaluating (or (begin (print 1) #f) (begin (print 2) #f) 6 (begin (print 3) 7))
>>> do_or_form(read_line("((begin (print 1) #f) (begin (print 2) #f) 6 (begin (print 3) 7))"), env)
1
2
6
"""
# BEGIN PROBLEM 12
if expressions == nil:
return False
while expressions.rest:
elem = scheme_eval(expressions.first,env)
if is_scheme_true(elem):
return elem
expressions = expressions.rest
return scheme_eval(expressions.first,env,True)
# END PROBLEM 12
def do_cond_form(expressions, env):
"""Evaluate a cond form.
>>> do_cond_form(read_line("((#f (print 2)) (#t 3))"), create_global_frame())
3
"""
while expressions is not nil:
clause = expressions.first
validate_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 is_scheme_true(test):
# BEGIN PROBLEM 13
if clause.rest:
return eval_all(clause.rest, env)
return test
# END PROBLEM 13
expressions = expressions.rest
def do_let_form(expressions, env):
"""Evaluate a let form.
>>> env = create_global_frame()
>>> do_let_form(read_line("(((x 2) (y 3)) (+ x y))"), env)
5
"""
validate_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 Frame 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')
names = values = nil
# BEGIN PROBLEM 14
while bindings:
clause = bindings.first
validate_form(clause,2,2)
names = Pair(clause.first,names)
values = Pair(scheme_eval(clause.rest.first,env),values)
bindings = bindings.rest
validate_formals(names)
# END PROBLEM 14
return env.make_child_frame(names, values)
def do_define_macro(expressions, env):
"""Evaluate a define-macro form.
>>> env = create_global_frame()
>>> do_define_macro(read_line("((f x) (car x))"), env)
'f'
>>> scheme_eval(read_line("(f (1 2))"), env)
1
"""
# BEGIN PROBLEM OPTIONAL_2
"*** YOUR CODE HERE ***"
# END PROBLEM OPTIONAL_2
def do_quasiquote_form(expressions, env):
"""Evaluate a quasiquote form with parameters EXPRESSIONS in
Frame ENV."""
def quasiquote_item(val, env, level):
"""Evaluate Scheme expression VAL that is nested at depth LEVEL in
a quasiquote form in Frame ENV."""
if not scheme_pairp(val):
return val
if val.first == 'unquote':
level -= 1
if level == 0:
expressions = val.rest
validate_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))
validate_form(expressions, 1, 1)
return quasiquote_item(expressions.first, env, 1)
def do_unquote(expressions, env):
raise SchemeError('unquote outside of quasiquote')
#################
# Dynamic Scope #
#################
def do_mu_form(expressions, env):
"""Evaluate a mu form."""
validate_form(expressions, 2)
formals = expressions.first
validate_formals(formals)
# BEGIN PROBLEM 11
return MuProcedure(expressions.first,expressions.rest)
# END PROBLEM 11
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,
'mu': do_mu_form,
}