-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw8.rkt
391 lines (363 loc) · 12.3 KB
/
hw8.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
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
#lang plai
;;(print-only-errors #t)
(require (for-syntax racket/base) racket/match racket/list racket/string
(only-in mzlib/string read-from-string-all))
;; build a regexp that matches restricted character expressions, can use only
;; {}s for lists, and limited strings that use '...' (normal racket escapes
;; like \n, and '' for a single ')
(define good-char "(?:[ \t\r\na-zA-Z0-9_{}!?*/<=>:+-]|[.][.][.])")
;; this would make it awkward for students to use \" for strings
;; (define good-string "\"[^\"\\]*(?:\\\\.[^\"\\]*)*\"")
(define good-string "[^\"\\']*(?:''[^\"\\']*)*")
(define expr-re
(regexp (string-append "^"
good-char"*"
"(?:'"good-string"'"good-char"*)*"
"$")))
(define string-re
(regexp (string-append "'("good-string")'")))
(define (string->sexpr str)
(unless (string? str)
(error 'string->sexpr "expects argument of type <string>"))
(unless (regexp-match expr-re str)
(error 'string->sexpr "syntax error (bad contents)"))
(let ([sexprs (read-from-string-all
(regexp-replace*
"''" (regexp-replace* string-re str "\"\\1\"") "'"))])
(if (= 1 (length sexprs))
(car sexprs)
(error 'string->sexpr "bad syntax (multiple expressions)"))))
;;----------Set debug mode-----------
(define debug 0)
;print: a helper function to print things nicely
;(define (print comment x)
; (cond
; [(= 1 debug)
; (begin
; (display comment)
; (display x)
; (display "\n"))]
; [else (void)]))
;;--------------------------------------------------------
(define-type WFAE
[num (n number?)]
[add (lhs WFAE?)
(rhs WFAE?)]
[sub (lhs WFAE?)
(rhs WFAE?)]
[id (name symbol?)]
[fun (param symbol?)
(body WFAE?)]
[with (name symbol?)
(named-expr WFAE?)
(body-expr WFAE?)]
[app (fun-expr WFAE?)
(arg-expr WFAE?)]
)
(define-type CWFAE
[cnum (n number?)]
[cadd (lhs CWFAE?) (rhs CWFAE?)]
[csub (lhs CWFAE?) (rhs CWFAE?)]
[cwith (named-expr CWFAE?) (body-expr CWFAE?)]
[cid (pos number?)]
[cfun (body CWFAE?)]
[capp (fun-expr CWFAE?) (arg-expr CWFAE?)]
)
;;
(define-type CWFAE-Value
[numV (n number?)]
[closureV (body CWFAE?) (ds DefrdSub?)])
;;
(define-type CDefrdSub
[mtCSub]
[aCSub (name symbol?)
(rest CDefrdSub?)])
(define-type CWFAE-Cont
[mtK]
[addSecondK (r CWFAE?)
(ds DefrdSub?)
(k CWFAE-Cont?)]
[doAddK (v1 CWFAE-Value?)
(k CWFAE-Cont?)]
[subSecondK (r CWFAE?)
(ds DefrdSub?)
(k CWFAE-Cont?)]
[doSubK (v1 CWFAE-Value?)
(k CWFAE-Cont?)]
[appArgK (arg-expr CWFAE?)
(ds DefrdSub?)
(k CWFAE-Cont?)]
[appNamedExprK (named-expr CWFAE?)
(ds DefrdSub?)
(k CWFAE-Cont?)]
[doAppK (fun-val CWFAE-Value?)
(k CWFAE-Cont?)]
)
(define (DefrdSub? x) true)
;;TODO: add with
;; parse-sexpr : S-expr -> WFAE
(define (parse-sexpr sexp)
(cond
[(number? sexp) (num sexp)]
[(symbol? sexp) (id sexp)]
[(pair? sexp)
(case (car sexp)
[(+) (add (parse-sexpr (second sexp))
(parse-sexpr (third sexp)))]
[(-) (sub (parse-sexpr (second sexp))
(parse-sexpr (third sexp)))]
[(fun) (fun (first (second sexp))
(parse-sexpr (third sexp)))]
[(with)
(begin
;(print "sexp" sexp)
;(print "first-sexp" (second sexp))
(with (first (second sexp))
(parse-sexpr (second (second sexp)))
(parse-sexpr (third sexp))
)
)]
[else (app (parse-sexpr (first sexp))
(parse-sexpr (second sexp)))])]))
;; parse: string -> WFAE
;; parses a string containing a WFAE expression to a WFAE AST
(define (parse str)
(parse-sexpr (string->sexpr str)))
;; compile : WFAE CDefrdSub -> CWFAE
(define (compile wfae ds)
(type-case WFAE wfae
[num (n) (cnum n)]
[add (l r) (cadd (compile l ds) (compile r ds))]
[sub (l r) (csub (compile l ds) (compile r ds))]
[id (name) (cid (locate name ds))]
[with (id named-expr body-expr)
;(print "with:cnamed-expr:" named-expr)
(cwith (compile named-expr ds)
(compile body-expr (aCSub id ds)) )
]
[fun (param body)
(cfun (compile body (aCSub param ds)))]
[app (fun-expr arg-expr)
(capp (compile fun-expr ds) (compile arg-expr ds))]
)
)
;; locate : symbol CDefrdSub -> number
;; return the position of a symbol in the list
(define (locate name ds)
(type-case CDefrdSub ds
[mtCSub () (error 'locate "free variable")]
[aCSub (sub-name rest-ds)
(if (symbol=? sub-name name)
0
(+ 1 (locate name rest-ds)))]))
;;helper function to deal with num op bullshit
;; num-op : (number number -> number) -> (KCFAE-Value KCFAE-Value -> KCFAE-Value)
(define (num-op op)
(lambda (x y)
(numV (op (numV-n x) (numV-n y)))))
(define num+ (num-op +))
(define num- (num-op -))
;;--------------------------------------------------------------------
;;global register
(define cwfae-reg (cnum 0))
(define k-reg (mtK))
(define v-reg (numV 0))
(define ds-reg empty)
;;------something something dark side--------
;;temp register for search function
(define ds2-reg 0)
;;search: Search the value at v-reg from the ds2-reg
(define (search)
(if (zero? v-reg)
(begin
;(print "ds2" ds2-reg)
(set! v-reg (first ds2-reg))
(continue))
(begin
(set! ds2-reg (rest ds2-reg))
(set! v-reg (- v-reg 1))
(search))))
;;-----------------------------------------------
;;helper function to help debugging
; (define (print-debug)
; (begin
; (print "----------interp" (void))
; (print "cwfae" cwfae-reg)
; (print "k-reg" k-reg)
; (print "ds-reg" ds-reg)
; (print "v-reg" v-reg)
; )
; )
;; interp : -> CWFAE-Value
(define (interp)
(begin
;(print "ds" ds-reg)
;(print-debug)
(type-case CWFAE cwfae-reg
[cnum (n)
(begin
;(print "cwfae" cwfae-reg)
(set! v-reg (numV n))
(continue))]
[cadd (l r)
(begin
(set! cwfae-reg l)
(set! k-reg (addSecondK r ds-reg k-reg))
(interp)
)
]
[csub (l r)
(begin
(set! cwfae-reg l)
(set! k-reg (subSecondK r ds-reg k-reg))
(interp)
)
]
[cwith ( body-expr named-expr)
(begin
;...
(set! k-reg (appNamedExprK named-expr ds-reg k-reg))
(set! cwfae-reg body-expr)
(interp))]
[cid (pos)
(begin
(set! ds2-reg ds-reg)
(set! v-reg pos)
(search))]
[cfun (body-expr)
(begin
(set! v-reg (closureV body-expr ds-reg))
(continue))]
[capp (fun-expr arg-expr)
(begin
;(print "fun-expr" fun-expr)
;(print "arg-expr" arg-expr)
(set! k-reg (appArgK arg-expr ds-reg k-reg))
(set! cwfae-reg fun-expr)
(interp))]
)
)
)
;; continue : -> CWFAE-Value
(define (continue)
(begin
;(display "continue\n")
;(print-debug)
(type-case CWFAE-Cont k-reg
[mtK ()
v-reg]
[addSecondK (r ds k)
(begin
(set! cwfae-reg r)
(set! ds-reg ds)
(set! k-reg (doAddK v-reg k))
(interp))]
[doAddK (v1 k)
(begin
(set! v-reg (num+ v1 v-reg))
(set! k-reg k)
(continue))]
[subSecondK (r ds k)
(begin
(set! cwfae-reg r)
(set! ds-reg ds)
(set! k-reg (doSubK v-reg k))
(interp))]
[doSubK (v1 k)
(begin
(set! v-reg (num- v1 v-reg))
(set! k-reg k)
(continue))]
[appArgK (arg-expr ds k)
(begin
;...
(set! cwfae-reg arg-expr)
(set! ds-reg ds)
(set! k-reg (doAppK v-reg k))
(interp))]
[appNamedExprK (named-expr ds k)
(begin
;...
;(print "appNamedExprK" 0)
(set! cwfae-reg named-expr)
(set! ds-reg (cons v-reg ds))
(set! k-reg k)
(interp))]
[doAppK (fun-val k)
(begin
;...
;(print "fun-val" fun-val)
(set! cwfae-reg (closureV-body fun-val ))
(set! ds-reg (cons v-reg (closureV-ds fun-val)))
(set! k-reg k)
(interp))]
)
))
;; init : void -> void
(define (init)
(set! k-reg (mtK))
(set! v-reg (numV 0))
(set! ds-reg empty))
;; helper function to help running some tests
(define (interp-with-default-init smt)
(begin
(init)
(set! cwfae-reg smt)
(interp)
)
)
;; run : string -> CWFAE-Value
;; evaluate a WFAE program contained in a string
(define (run str)
(begin
(set! cwfae-reg (compile (parse str) (mtCSub)))
(init)
(interp)))
;;;...
;;passed tests
(test (run "10") (numV 10))
(test (run "{+ 10 7}") (numV 17))
(test (run "{- 10 7}") (numV 3))
(test (run "{{fun {x} {+ x 12}} {+ 1 17}}") (numV 30))
(test (run "{{fun {x} {{fun {f} {+ {f 1} {{fun {x} {f 2}} 3}}}
{fun {y} {+ x y}}}} 0}") (numV 3))
(test (interp-with-default-init (compile
(app (fun 'x (add (id 'x) (num 12)))
(add (num 1) (num 17)))
(mtCSub))
)
(numV 30))
(test (run "{with {x 5} {+ x x}}") (numV 10))
(test (run "{with {x {with {a 1} {+ a 4}}} {+ x x}}" ) (numV 10))
(test (run "{with {x 5} {+ x {with {x 3} x}}}") (numV 8))
(test (run "{with {y 5} {+ 3 {with {x 3} y}}}") (numV 8))
(test (run "5" ) (numV 5))
(test (run "{+ 5 5}" ) (numV 10))
(test (run "{with {x {+ 5 5}} {+ x x}}" ) (numV 20))
(test (run "{with {x 5} {+ x x}}" ) (numV 10))
(test (run "{with {x {+ 5 5}} {with {y {- x 3}} {+ y y}}}" ) (numV 14))
(test (run "{with {x 5} {with {y {- x 3}} {+ y y}}}" ) (numV 4))
(test (run "{with {x 5} {+ x {with {x 3} 10}}}" ) (numV 15))
(test (run "{with {x 5} {+ x {with {x 3} x}}}" ) (numV 8))
(test (run "{with {x 5} {+ x {with {y 3} x}}}" ) (numV 10))
(test (run "{with {x 5} {with {y x} y}}" ) (numV 5))
(test (run "{with {x 5} {with {x x} x}}" ) (numV 5))
(test (run "{{fun {x} {- x 12}} {+ 1 17}}")
(numV 6))
(test (run "{with {x 5} {+ x {with {x 3} x}}}") (numV 8))
(test (run "{with {y 5} {+ 3 {with {x 3} y}}}") (numV 8))
(test (run "{with {x {+ 5 5}} {+ x x}}") (numV 20))
(test (run "{with {x 5} {+ x x}}") (numV 10))
(test (run "{with {x {+ 5 5}} {with {y {- x 3}} {+ y y}}}") (numV 14))
(test (run "{with {x 5} {with {y {- x 3}} {+ y y}}}") (numV 4))
(test (run "{with {x 5} {+ x {with {x 3} 10}}}") (numV 15))
(test (run "{with {x 5} {+ x {with {x 3} x}}}") (numV 8))
(test (run "{with {x 5} {+ x {with {y 3} x}}}") (numV 10))
(test (run "{with {x 5} {with {y x} y}}") (numV 5))
(test (run "{with {x 5} {with {x x} x}}") (numV 5))
(test (run "{{fun {x} {+ {with {x 32} x} 10}} 42}") (numV 42))
;(test (run "{{{fun {x} {fun {y} x}} 13}}") (numV 13))
(test/exn (run "{with {x 5} {with {x y} x}}") "free variable")
(test/exn (run 1) "ring->sexpr: expects argument of type <string>")
(test/exn (run "'") "syntax error (bad contents)")
(test/exn (run "{+ 1 1} {+ 2 2}") "bad syntax (multiple expressions)")