-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.rkt
262 lines (212 loc) · 5.76 KB
/
interpreter.rkt
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
#lang racket
(define atom?
(lambda (x)
(not (or (pair? x)(null? x)))))
(define first
(lambda (x)
(car x)))
(define second
(lambda (x)
(car (cdr x))))
(define third
(lambda (x)
(car (cdr (cdr x)))))
(second '(a b))
(atom? 'a)
(atom? null)
(atom? '(a b))
(define build
(lambda (s1 s2)
(cons s1 (cons s2 '()))))
(define new-entry build)
(define e1 (new-entry '(apples oranges bread)
'(pie juice crumbs)))
(define lookup-in-entry
(lambda (name entry entry-f)
(lookup-in-entry-help name
(first entry)
(second entry)
entry-f)))
(define lookup-in-entry-help
(lambda (name names values entry-f)
(cond
((null? names) (entry-f name))
((eq? (car names) name) (car values))
(else (lookup-in-entry-help name (cdr names)(cdr values) entry-f)))))
(lookup-in-entry 'arg e1 (lambda(x)(list "name not found: " x)))
(lookup-in-entry 'apples e1 (lambda(x)(list "name not found: " x)))
(lookup-in-entry 'bread e1 (lambda(x)(list "name not found: " x)))
(define extend-table cons)
(define e2 (new-entry '(appetizer entree beverage)
'(pate boeuf vin)))
e1
e2
(define e3 (extend-table e1 '()))
(define e4 (extend-table e2 e3))
e4
(define lookup-in-table
(lambda (name table table-f)
(cond
((null? table) (table-f name))
(else (lookup-in-entry name (car table)
(lambda(name)(lookup-in-table name (cdr table) table-f)))))))
(lookup-in-table 'apples e4 (lambda (x)x))
(define expression-to-action
(lambda (e)
(cond
((atom? e) (atom-to-action e))
(else (list-to-action e)))))
(define atom-to-action
(lambda (e)
(cond
((number? e) *const)
((eq? e #t) *const)
((eq? e #f) *const)
((eq? e (quote cons)) *const)
((eq? e (quote car)) *const)
((eq? e (quote cdr)) *const)
((eq? e (quote null?)) *const)
((eq? e (quote eq?)) *const)
((eq? e (quote atom?)) *const)
((eq? e (quote zero?)) *const)
((eq? e (quote add1)) *const)
((eq? e (quote sub1)) *const)
((eq? e (quote number?)) *const)
(else *identifier))))
(define list-to-action
(lambda (e)
(cond
((atom? ( car e))
(cond
((eq? (car e) (quote quote))*quote)
((eq? (car e) (quote lambda))*lambda)
((eq? (car e) (quote cond))*cond)
(else *application)))
(else *application))))
(define value
(lambda (e)
(meaning e (quote ()))))
(define meaning
(lambda (e table)
((expression-to-action e) e table)))
(define *const
(lambda ( e table)
(cond
((number? e) e)
(( eq? e #t) #t)
((eq? e #f) #f)
(else (build (quote primitive) e)))))
(define *quote
(lambda (e table)
(text-of e)))
(define text-of second)
(define *identifier
(lambda (e table)
(lookup-in-table e table initial-table)))
(define initial-table
(lambda (name)
(car (quote ()))))
(define *lambda
(lambda (e table)
(build (quote non-primitive)
(cons table (cdr e)))))
(define table-of first)
(define formals-of second)
(define body-of third)
(define question-of first)
(define answer-of second)
(define *cond
(lambda (e table)
(evcon (cond-lines-of e) table)))
(define cond-lines-of cdr)
(define evcon
(lambda (lines table)
(cond
((else? (question-of (car lines)))
(meaning (answer-of (car lines))
table))
((meaning (question-of (car lines))
table)
(meaning (answer-of (car lines))
table))
(else (evcon (cdr lines) table)))))
(define else?
(lambda (question)
(cond
((atom? question)(eq? question (quote else)))
(else #f))))
(define cond-lines-of cdr)
<<<<<<< HEAD
(define *cond
(lambda (e table)
(evcon (cond-lines-of e) table)))
=======
(define answer-of second)
(define evlis
(lambda ( args table)
(cond
(( null? args) (quote ()))
(else
(cons (meaning (car args) table)
(evlis (cdr args) table))))))
(define function-of
(lambda (e)(car e)))
(define arguments-of
(lambda (e)(cdr e)))
(define *application
(lambda (e table)
(apply
(meaning (function-of e) table))
(evlis (arguments-of e) table)))
(define primitive? (lambda (f)(eq? (first f) 'primitive)))
(define non-primitive? (lambda (f) (eq? (first f) 'non-primitive)))
(define apply
(lambda (func vals)
(cond
((primitive? func)
(apply-primitive
(second func) vals))
((non-primitive? func)
(apply-closure
(second func) vals)))))
(define apply-primitive
(lambda (name vals)
(cond
((eq? name (quote cons))
(cons (first vals) (second vals)))
((eq? name (quote car))
(car (first vals)))
((eq? name (quote cdr))
(cdr (first vals)))
((eq? name (quote null?))
(null? (first vals)))
((eq? name (quote eq?))
(eq? (first vals)(second vals )))
((eq? name (quote atom?))
(:atom? (first vals)))
((eq? name (quote zero?))
(zero? (first vals)))
((eq? name (quote add1))
(add1 (first vals)))
((eq? name (quote sub1))
(sub1 (first vals)))
((eq? name (quote number?))
(number? (first vals))))))
(define :atom?
(lambda (x)
(cond
((atom? x) #t)
((null? x) #f)
((eq? (car x) (quote primitive))#t)
((eq? (car x) (quote non-primitive))#t)
(else #f))))
(define apply-closure
(lambda (closure vals)
(meaning (body-of closure)
(extend-table
(new-entry
(formals-of closure)
vals)
(table-of closure)))))
(apply '(primitive cons) '(a b))
(apply '(non-primitive (lambda (x y)(* x y))) '(5 4))