-
Notifications
You must be signed in to change notification settings - Fork 1
/
python-interp.rkt
241 lines (220 loc) · 15.6 KB
/
python-interp.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
#lang plai-typed
(require "python-core-syntax.rkt"
"python-primitives.rkt"
"python-lib.rkt")
(require (typed-in racket/base (string<? : (string string -> boolean)))
(typed-in racket/base (string<=? : (string string -> boolean)))
(typed-in racket/base (string>=? : (string string -> boolean)))
(typed-in racket/base (string>? : (string string -> boolean)))
(typed-in racket/base (string->list : (string -> (listof string)))))
(define new-loc
(let ([n (box 0)])
(lambda ()
(begin
(set-box! n (add1 (unbox n)))
(unbox n)))))
(define (interp-env expr env store)
(type-case CExp expr
[CNum (n) (ValueA (VNum n) store)]
[CStr (s) (ValueA (VStr s) store)]
[CTrue () (ValueA (VTrue) store)]
[CFalse () (ValueA (VFalse) store)]
[CError (e) (type-case CAnswer (interp-env e env store)
[ValueA (e-v s-v) (ValueA (VException e-v) s-v)])]
[CNone () (ValueA (VNone) store)]
[CIf (i t e) (type-case CAnswer (interp-env i env store)
[ValueA (i-v i-s)
(if (VTrue? i-v)
(type-case CAnswer (interp-env t env i-s)
[ValueA (t-v t-s)
(ValueA t-v t-s)])
(type-case CAnswer (interp-env e env i-s)
[ValueA (e-v e-s)
(ValueA e-v e-s)]))])]
[CId (x) (type-case (optionof Loc) (hash-ref env x)
[some (v) (type-case (optionof CVal) (hash-ref store v)
[some (v) (ValueA v store)]
[none () (ValueA (VException (VStr (string-append "No value in store for this location: " (to-string v)))) store)])]
[none () (type-case (optionof CExp) (hash-ref python-lib x)
[some (v) (type-case CAnswer (interp-env v env store)
[ValueA (v-v s-v)
(ValueA v-v s-v)])]
[none () (ValueA (VException (VStr (string-append "Unbound identifier: " (to-string x)))) store)])])]
[CLet (x bind body)
(type-case CAnswer (interp-env bind env store)
[ValueA (b-v b-s)
(let ([loc (new-loc)])
(type-case CAnswer (interp-env body (hash-set env x loc) (hash-set store loc b-v))
[ValueA (bod-v bod-s)
(ValueA bod-v bod-s)]))])]
[CSeq (e1 e2)
(type-case CAnswer (interp-env e1 env store)
[ValueA (e1-v e1-s)
(type-case CAnswer (interp-env e2 env e1-s)
[ValueA (e2-v e2-s)
(ValueA e2-v e2-s)])])]
[CSet! (x v)
(type-case (optionof Loc) (hash-ref env x)
[some (val) (type-case CAnswer (interp-env v env store)
[ValueA (v-v s-v)
(ValueA v-v (hash-set s-v val v-v))])]
[none () (let ([loc (new-loc)])
(type-case CAnswer (interp-env v (hash-set env x loc) store)
[ValueA (v-v s-v)
(ValueA v-v (hash-set s-v loc v-v))]))])]
[CList (l)
(ValueA (VList (map (lambda (e)
(type-case CAnswer (interp-env e env store)
[ValueA (e-v e-s)
e-v])) l)) store)]
[CApp (fun arges)
(type-case CAnswer (interp-env fun env store)
[ValueA (fun-v fun-s)
(if (VClosure? fun-v)
(local [(define argvs (map (lambda (e)
(type-case CAnswer (interp-env e env store)
[ValueA (e-v e-s)
e-v])) arges))]
(bind-args (VClosure-args fun-v) argvs (VClosure-env fun-v) fun-s (VClosure-body fun-v)))
(ValueA (VException (VStr (string-append "Not a closure: " (to-string fun-v)))) fun-s))])]
[CFunc (args body) (ValueA (VClosure env args body) store)]
[CPrim1 (prim args) (ValueA (python-prim1 prim (map (lambda (e)
(type-case CAnswer (interp-env e env store)
[ValueA (e-v e-s)
e-v])) args)) store)]
[CPrimP (l o r)
(type-case CAnswer (interp-env l env store)
[ValueA (l-v l-s)
(type-case CVal l-v
[VNum (l-n) (type-case CAnswer (interp-env r env l-s)
[ValueA (r-v r-s)
(type-case CVal r-v
[VNum (r-n) (case o
[(+) (ValueA (VNum (+ l-n r-n)) r-s)]
[(-) (ValueA (VNum (- l-n r-n)) r-s)]
[(*) (ValueA (VNum (* l-n r-n)) r-s)]
[(==) (if (= l-n r-n)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(is) (if (= l-n r-n)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(<) (if (< l-n r-n)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(<=) (if (<= l-n r-n)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(>) (if (> l-n r-n)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(>=) (if (>= l-n r-n)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(!=) (if (= l-n r-n)
(ValueA (VFalse) r-s)
(ValueA (VTrue) r-s))])]
[else (case o
[(==) (ValueA (VFalse) r-s)]
[(is) (ValueA (VFalse) r-s)]
[(!=) (ValueA (VTrue) r-s)]
[else (ValueA (VException (VStr "Unsupported operand")) r-s)])])])]
[VStr (l-str) (type-case CAnswer (interp-env r env l-s)
[ValueA (r-v r-s)
(type-case CVal r-v
[VStr (r-str) (case o
[(+) (ValueA (VStr (string-append l-str r-str)) r-s)]
[(==) (if (string=? l-str r-str)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(is) (if (string=? l-str r-str)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(<) (if (string<? l-str r-str)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(<=) (if (string<=? l-str r-str)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(>) (if (string>? l-str r-str)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(>=) (if (string>=? l-str r-str)
(ValueA (VTrue) r-s)
(ValueA (VFalse) r-s))]
[(!=) (if (string=? l-str r-str)
(ValueA (VFalse) r-s)
(ValueA (VTrue) r-s))]
[(in) (let ([chars (string->list r-str)])
(if (member l-str chars)
(ValueA (VTrue) r-s)
(begin (display (to-string l-str))
(display (to-string chars))
(ValueA (VFalse) r-s))))]
[(not-in) (let ([chars (string->list r-str)])
(if (member l-str chars)
(ValueA (VFalse) r-s)
(ValueA (VTrue) r-s)))])]
[else (case o
[(==) (ValueA (VFalse) r-s)]
[(is) (ValueA (VFalse) r-s)]
[else (ValueA (VStr "TypeError: unsupported operand type(s)") r-s)])])])]
[VTrue () (type-case CAnswer (interp-env r env l-s)
[ValueA (r-v r-s)
(type-case CVal r-v
[VTrue () (case o
[(==) (ValueA (VTrue) r-s)]
[(is) (ValueA (VTrue) r-s)]
[else (ValueA (VException (VStr "Unsupported operand")) r-s)])]
[else (case o
[(==) (ValueA (VFalse) r-s)]
[(is) (ValueA (VFalse) r-s)]
[else (ValueA (VException (VStr "TypeError: unsupported operand type(s)")) r-s)])])])]
[VFalse () (type-case CAnswer (interp-env r env l-s)
[ValueA (r-v r-s)
(type-case CVal r-v
[VFalse () (case o
[(==) (ValueA (VTrue) r-s)]
[(is) (ValueA (VTrue) r-s)]
[else (ValueA (VException (VStr "Unsupported operand")) r-s)])]
[else (case o
[(==) (ValueA (VFalse) r-s)]
[(is) (ValueA (VFalse) r-s)]
[else (ValueA (VException (VStr "TypeError: unsupported operand type(s)")) r-s)])])])]
[VNone () (type-case CAnswer (interp-env r env l-s)
[ValueA (r-v r-s)
(type-case CVal r-v
[VNone () (case o
[(==) (ValueA (VTrue) r-s)]
[(is) (ValueA (VTrue) r-s)]
[else (ValueA (VException (VStr "Unsupported operand")) r-s)])]
[else (case o
[(==) (ValueA (VFalse) r-s)]
[(is) (ValueA (VFalse) r-s)]
[else (ValueA (VException (VStr "TypeError: unsupported operand type(s)")) r-s)])])])]
;[VException (e) (type-case CAnswer (interp-env r env l-s)
; [ValueA (r-v r-s)
; (type-case CVal r-v
; [VException (r-e)
; (if (not (equal? e r-e))
[else (ValueA (VException (VStr "TypeError: unsupported operand type(s)")) l-s)])])]
[else (error 'interp (string-append "no case yet: " (to-string expr)))]))
(define (bind-args args vals env store expr)
(cond [(and (empty? args) (empty? vals)) (type-case CAnswer (interp-env expr env store)
[ValueA (expr-v expr-s)
(ValueA expr-v expr-s)])]
[(or (empty? args) (empty? vals))
(ValueA (VException (VStr "Arity mismatch")) store)]
[(and (cons? args) (cons? vals))
(let ([loc (new-loc)])
(bind-args (rest args)
(rest vals)
(hash-set env (first args) loc)
(hash-set store loc (first vals))
expr))]))
(define (interp expr)
(type-case CAnswer (interp-env expr (hash (list)) (hash (list)))
[ValueA (arg-v arg-s)
(type-case CVal arg-v
[VException (e) (error 'interp (VStr-s e))] ; might need to change this so Exception is an Answer
[else (void)])]))