-
Notifications
You must be signed in to change notification settings - Fork 0
/
l1.rkt
320 lines (281 loc) · 9.78 KB
/
l1.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
#lang typed/racket/base
(provide Opt lookup sort LocValue Loc printreduce printexpr updates BoolValue IntValue None Some Seq Deref Assign)
;; Macro by Alexis King
(require (for-syntax racket/base
racket/sequence
racket/syntax
syntax/parse
syntax/stx)
racket/match)
(begin-for-syntax
(define-syntax-class type
(pattern name:id
#:attr [param 1] '()
#:attr [field-id 1] '())
(pattern (name:id param ...+)
#:attr [field-id 1] (generate-temporaries #'(param ...)))))
(define-syntax define-datatype
(syntax-parser
[(_ type-name:type data-constructor:type ...)
(define/with-syntax [data-type ...]
(for/list ([name (in-syntax #'(data-constructor.name ...))])
(if (stx-null? #'(type-name.param ...))
name
#`(#,name type-name.param ...))))
#'(begin
(struct (type-name.param ...) data-constructor.name
([data-constructor.field-id : data-constructor.param] ...)) ...
(define-type type-name (U data-type ...)))]))
;; End of Macro by Alexis King
(define-datatype Store
( Loc String)
( LocValue Number )
)
(struct Plus())
(struct GTEQ())
(define-type (Operator c) (U Plus GTEQ))
(: operator : (U Plus GTEQ)-> Char)
(define (operator c )
(match c
['Plus #\+]
['GTEQ #\>=]))
(define-datatype Expr
( BoolValue Boolean )
( IntValue Number )
( Op Expr (U Plus GTEQ) Expr)
( If Expr Expr Expr)
( Assign String Expr)
( Deref String)
( Seq Expr Expr)
( While Expr Expr)
Skip
)
(: printexpr (Expr -> String))
(define (printexpr expr)
(match expr
[(IntValue n) (printf( string-append (format "~a" n))) ""]
[(BoolValue b) (string-append (format "~a" b))]
[(Deref l) (string-append (format "( ~a ~a )" "!" l))]
[(If e1 e2 e3)
(string-append (format "if ~a then ~a else ~a" (printexpr e1 ) (printexpr e2)
(printexpr e3)))]
[( Op e1 operate e2 )
(string-append (format "( ~a ~c ~a)" (printexpr e1) (operator operate)
(printexpr e2 )))]
[( If e1 e2 e3 )
(string-append (format "( ~a ~a ~a)" (printexpr e1) (printexpr e2 )(printexpr e2 )))]
[ (Assign l e ) = (printf (format "Assign ~a := ~a" l (printexpr e ))) ""]
[ Skip ( string-append "skip")]
[ (Seq e1 e2 ) (printf " ~a ; ~a" (printexpr e1 )
(printexpr e2)) ""]
[ (While e1 e2 ) (printf "while ~a do ~a " (printexpr e1 )
(printexpr e2)) ""]
))
(struct None ()
#:transparent)
(struct (i) Some ([v : i])
#:transparent)
(define-type (Opt a) (U None (Some a)))
(: lookup ((Listof (Pairof Loc LocValue)) String ->
(Pairof (Opt Number) (Listof (Pairof Loc LocValue)))))
(define (lookup ls l)
(match ls
['() (cons (None) ls)]
[(cons (cons (Loc s) (LocValue n)) rest)
(if (string=? s l)
(cons (Some n) ls)
(lookup rest l))]
))
(: updates ((Listof (Pairof Loc LocValue)) (Pairof Loc LocValue) ->
(Opt (Listof (Pairof Loc LocValue)))))
(define (updates ls l)
(match ls
['() (None)]
[(cons (cons (Loc s) (LocValue n)) rest)
(if (string=? s (match (car l) [(cons (Loc x) _) x]))
(Some (cons l (cdr ls)))
(match (updates rest l)
[(Some updated) (Some (cons (cons (Loc s) (LocValue n)) updated))]
[(None) (None)]))]))
(: reduce ((Pairof (Opt Expr) (Listof (Pairof Loc LocValue))) ->
(Pairof (Opt Expr ) (Listof (Pairof Loc LocValue)))))
(define (reduce expr)
(match expr
[ (cons (Some (Op (? integer? n1) Plus (? integer? n2)))
store)
(cons (Some (IntValue (+ n1 n2))) store)]
[ (cons (Some ( Op (? integer? n1) GTEQ (? integer? n2 ))) store)
(cons (Some (BoolValue (>= n1 n2))) store)]
[ (cons (Some (Op (? integer? n1) Skip (? boolean? n2))) store)
(match (reduce (cons (Some n2) store))
[ (cons (Some (IntValue nn2)) store) (cons (Some ((Op n1 Skip nn2))) store)]
[ (None) (None) ]
)
(match (reduce n1 store)
[ (cons (Some (IntValue nn1)) store) (cons (Some (Op nn1 Skip n2)) store)]
[ (None) (None) ]
)]
[ (cons (Some (IntValue n )) store) (cons (None) store )]
[ (cons (Some (If e1 e2 e3)) store)
(match e1
[#t (cons (Some e2) store) ]
[#f (cons (Some e3) store) ]
[_ (match (reduce (cons (Some e1) store ))
[ (cons (Some e1) store) ( cons (Some (If e1 e2 e3)) store) ]
[ (None) (None) ]
)]
)]
[ (cons (Some (Deref l)) store)
(match (lookup store l)
[ (cons (Some n ) store ) (cons (Some (IntValue n)) store)]
[ (cons (None) store) (cons (None) store )]
)]
[ (cons (Some (Assign l e )) store)
(match e
[(IntValue n)
(match (updates store (cons (Loc l) (LocValue n)))
[ (Some store ) (cons (Some (Skip)) store)]
[ (None) (cons (None) store ) ]
[ _ (match (reduce (cons (Some e) store))
[(cons (Some e) store) (cons (Some (Assign l e)) store)]
[ (None) (None) ]
)
]
)]
)]
[ (cons (Some (Seq e1 e2)) store)
(match e1
[Skip (cons (Some e2) store) ]
[ _ ( match (reduce (cons (Some e1) store ))
[ (cons (Some e1) store)
(cons (Some (Seq e1 e2)) store ) ]
[ (None) (None) ]
)]
)]
[ (cons (Some (Skip)) store) ( cons (None) store )]
[ (cons (Some (While e1 e2)) store)
(cons (Some ( If e1 (Seq e2 (While e1 e2)) (Skip))) store) ]
))
(: reduce1 (Expr (Listof (Pairof Loc LocValue)) -> String))
(define (reduce1 e store)
(match (reduce (cons (Some e) store))
[ (cons (Some e) store)
( printf (format "~n --> ~a " (printconfig e store )))
( printf ( string-append (reduce1 e store )))
""
]
[ (cons (None) store)
(string-append "~n -/-> "
(match e
[Skip (string-append (format "(a value)~n"))]
[ _ (string-append (format "(stuck - not a value)"))]))
]
)
)
(: rawprintstore : ( (Listof (Pairof Loc LocValue)) -> String))
(define (rawprintstore ls)
(match ls
['() (string-append "")]
[(cons( cons l n) tail)
(match l [(cons (Loc x) (LocValue y))
(string-append (format " ~a = ~a " x y ))])
(rawprintstore tail )]
)
)
(: printconfig (Expr (Listof (Pairof Loc LocValue)) -> String))
(define (printconfig e store)
(string-append (format "< ~a , ~a >" (printexpr e)
(printstore store )))
)
(: printreduce (Expr (Listof (Pairof Loc LocValue)) -> Void))
(define (printreduce e store)
(printf (printconfig e store))
(printf (reduce1 e store))
)
(define (sort pairs)
(cond
['() pairs]
[(cons pairs) (insert (car pairs)
(sort (cdr pairs)))]))
(define (insert n pairs)
(cond
['() (cons n '())]
[else (cond
[(> n (car (car pairs))) (cons n pairs)]
[else (cons (car pairs)
(insert n (cdr pairs )))])]))
(define (printstore pairs)
(let* ([pairs (sort pairs )])
rawprintstore pairs )
)
(define-datatype type_L1
int
unit
bool
)
(define-datatype type_loc
intref
)
(define-type typeEnv
(U String type_loc)
)
(define-type Location
String
)
(: lookups ((Listof (Pairof String type_loc)) String ->
(Opt type_loc)))
(define (lookups ls l)
(match ls
['() ( None )]
[(cons (cons s n ) rest)
(if (string=? s l)
(Some n)
(lookups rest l))]
))
(: infertype ((Listof (Pairof Location type_loc)) Expr -> (Opt type_L1)))
(define (infertype gamma expr )
(match expr
[ (IntValue n ) (Some (int)) ]
[ (BoolValue b) (Some (bool))]
[ (Op e1 opr e2)
(match (list (infertype gamma e1) opr (infertype gamma e2 ))
[ (cons (Some (int)) (cons Plus (cons (Some (int)) '()))) (Some (int))]
[ (cons (Some (int)) (cons GTEQ (cons (Some (int)) '()))) (Some (bool))]
[ _ ( None ) ])
]
[ (If e1 e2 e3)
(match (list( infertype gamma e1) (infertype gamma e2)
(infertype gamma e3))
[(cons (Some (bool)) (cons (Some t2) (cons (Some t3) '())))
(if (eq? t2 t3)
(Some t2)
( None ))
]
[ _ ( None ) ])
]
[ (Deref l)
(match (lookups gamma l)
[ (Some intref) (Some (int))]
[ _ ( None ) ])
]
[ (Assign l e)
(match (list (lookups gamma l) (infertype gamma e))
[(list (Some (intref)) (Some (int))) (Some (unit))]
[ _ ( None ) ])
]
[ (Skip) (Some (unit)) ]
[ (Seq e1 e2)
(match (list (infertype gamma e1) (infertype gamma e2 ))
[(cons (Some (unit)) (Some t2)) (Some t2)]
[ _ ( None ) ])
]
[ (While e1 e2)
(match (list (infertype gamma e1) (infertype gamma e2))
[(cons (Some (bool)) (Some (unit)) ) (Some (unit))]
[ _ ( None ) ])
]
))
( infertype (list (cons "l1" (intref)))
(Seq( Assign "l1" (IntValue 3)) (Deref "l1")))
( printreduce (Seq( Assign "l1" (IntValue 3)) (Deref "l1"))
(list( cons( Loc "l1" )(LocValue 1))))