-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.hs
429 lines (399 loc) · 13.4 KB
/
Interpreter.hs
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
module Interpreter where
-- interpreter for a calculator with "memory" (variable assignments)
import AbsAssignment
import PrintAssignment
import Control.Monad.State
import qualified Data.Map.Strict as Map
-- the interpreter
data InternalType =
IInteger Integer
| IDouble Double
| IString String
| IVoid
deriving (Eq, Ord, Read, Show)
execProgram :: Program -> Action Void
execProgram p = do
case p of
PBlocks bs -> do
recExecBlock bs
funcs <- gets functions
case Map.lookup (Ident "main") funcs of
Just (Func t i p b) -> do
modify (\s -> s{variables = Map.empty : (variables s)})
v <- execBlock b
modify (\s -> s{variables = tail(variables s)})
return ()
Nothing -> return ()
recExecBlock :: [Block] -> Action Void
recExecBlock bs = do
case bs of
(b:lbs) -> do
execBlock b
recExecBlock lbs
return ()
[] -> do
return ()
execBlock :: Block -> Action (Maybe InternalType)
execBlock b = case b of
BStm1 s-> do
case s of
SReturn e -> do
v <- eval e
return (Just v)
SReturnE -> do
return (Just IVoid)
_ -> do
execStm s
return Nothing
BStms ss-> do
recExecStms ss
recExecStms :: [Stm] -> Action (Maybe InternalType)
recExecStms ss = do
case ss of
s: lss -> do
case s of
SReturn e -> do
v <- eval e
return (Just v)
SReturnE -> do
modify (\s -> s{variables = tail(variables s)})
return (Just IVoid)
_ -> do
es <- execStm s
case es of
Nothing -> do
v <- recExecStms lss
return v
(Just x) -> do
return es
[] -> do
return Nothing
execStm :: Stm -> Action (Maybe InternalType)
execStm s = case s of
SExp e -> do
eval e
return Nothing
SValDec t ids -> do
forEach ids updateVarInit
return Nothing
SValInit t initExps -> do
forEach initExps updateVarWithExp
return Nothing
SWhile e b -> do
v <- eval e
case v of
IInteger 0 -> do return Nothing
IInteger 1 -> do execBlock b
execStm s
SIfElse e b1 b2 -> do
v <- eval e
case v of
IInteger 0 -> do
b <- execBlock b2
return b
_ -> do
b <- execBlock b1
return b
SPrint e -> do
v <- eval e
output(showI v)
return Nothing
SFunc f -> do
v <- setEntry f
return Nothing
SReturn e -> do
v <- eval e
return (Just v)
SReturnE -> do
return (Just IVoid)
setEntry :: Fun -> Action Void
setEntry f = case f of
(Func t i p b) -> do
updateFunc i f
return ()
eval :: Exp -> Action InternalType
eval e = case e of
EAssign i e -> do
v <- eval e
updateVar i v
return v
EDisjunction exp1 exp2 -> do
v1 <- eval exp1
case v1 of
IInteger 1 -> do return v1
IInteger 0 -> do v2 <- eval exp2
return v2
EConjunction exp1 exp2 -> do
v1 <- eval exp1
case v1 of
IInteger 0 -> do return v1
IInteger 1 -> do v2 <- eval exp2
return v2
EEq exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do if v1 == v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IDouble vn2) -> do if v1 == v2
then do return $IInteger 1
else do return $IInteger 0
(IInteger vn1, IDouble vn2) -> do if v1 == v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IInteger vn2) -> do if v1 == v2
then do return $IInteger 1
else do return $IInteger 0
ENeq exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do if v1 /= v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IDouble vn2) -> do if v1 /= v2
then do return $IInteger 1
else do return $IInteger 0
(IInteger vn1, IDouble vn2) -> do if v1 /= v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IInteger vn2) -> do if v1 /= v2
then do return $IInteger 1
else do return $IInteger 0
EGt exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do if v1 > v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IDouble vn2) -> do if v1 > v2
then do return $IInteger 1
else do return $IInteger 0
(IInteger vn1, IDouble vn2) -> do if v1 > v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IInteger vn2) -> do if v1 > v2
then do return $IInteger 1
else do return $IInteger 0
ELt exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do if v1 < v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IDouble vn2) -> do if v1 < v2
then do return $IInteger 1
else do return $IInteger 0
(IInteger vn1, IDouble vn2) -> do if v1 < v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IInteger vn2) -> do if v1 < v2
then do return $IInteger 1
else do return $IInteger 0
EGte exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do if v1 >= v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IDouble vn2) -> do if v1 >= v2
then do return $IInteger 1
else do return $IInteger 0
(IInteger vn1, IDouble vn2) -> do if v1 >= v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IInteger vn2) -> do if v1 >= v2
then do return $IInteger 1
else do return $IInteger 0
ELte exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do if v1 <= v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IDouble vn2) -> do if v1 <= v2
then do return $IInteger 1
else do return $IInteger 0
(IInteger vn1, IDouble vn2) -> do if v1 <= v2
then do return $IInteger 1
else do return $IInteger 0
(IDouble vn1, IInteger vn2) -> do if v1 <= v2
then do return $IInteger 1
else do return $IInteger 0
EAdd exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do return $IInteger (vn1 + vn2)
(IDouble vn1, IDouble vn2) -> do return $IDouble (vn1 + vn2)
(IInteger vn1, IDouble vn2) -> do return $IDouble(fromInteger vn1 + vn2)
(IDouble vn1, IInteger vn2) -> do return $IDouble (vn1 + fromInteger vn2)
(IString vn1, IString vn2) -> do return $IString (vn1 ++ vn2)
ESub exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do return $IInteger (vn1 - vn2)
(IDouble vn1, IDouble vn2) -> do return $IDouble (vn1 - vn2)
(IInteger vn1, IDouble vn2) -> do return $IDouble (fromInteger vn1 - vn2)
EMul exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do return $IInteger (vn1 * vn2)
(IDouble vn1, IDouble vn2) -> do return $IDouble (vn1 * vn2)
(IInteger vn1, IDouble vn2) -> do return $IDouble (fromInteger vn1 * vn2)
EDiv exp1 exp2 -> do
v1 <- eval exp1
v2 <- eval exp2
case (v1,v2) of
(IInteger vn1, IInteger vn2) -> do return $IInteger (vn1 `div` vn2)
(IDouble vn1 , IDouble vn2) -> do return $IDouble(vn1 / vn2)
(IInteger vn1, IDouble vn2) -> do
return $IInteger(vn1 `div` round vn2)
(IDouble vn1, IInteger vn2) -> do return $IDouble(vn1 / fromInteger vn2)
ENeg e -> do
v <- eval e
case v of
IInteger 0 -> do return $IInteger 1
IInteger 1 -> do return $IInteger 0;
EPreInc e -> do
case e of
EId i -> do
env <- gets variables
v <- lookupVar i env
case v of
IInteger vv ->
let vn = vv + 1
in do updateVar i $IInteger vn
return $IInteger vn
EPostInc e -> do
case e of
EId i -> do
env <- gets variables
v <- lookupVar i env
case v of
IInteger vv ->
let vn = vv + 1
in do updateVar i $IInteger vn
return v
EPreDec e -> do
case e of
EId i -> do
env <- gets variables
v <- lookupVar i env
case v of
IInteger vv ->
let vn = vv - 1
in do updateVar i $IInteger vn
return $IInteger vn
EPostDec e -> do
case e of
EId i -> do
env <- gets variables
v <- lookupVar i env
case v of
IInteger vv ->
let vn = vv - 1
in do updateVar i $IInteger vn
return v
EInt n -> do return $IInteger n
EId x -> do
env <- gets variables
v1 <- lookupVar x env
return v1
ETrue-> do return $IInteger 1
EFalse-> do return $IInteger 0
EDouble n -> do return $IDouble n
EString n -> do return $IString n
EFunc i exps -> do
f <- lookupFunc i
case f of
(Func t i p b) -> do
modify (\s -> s{variables = Map.empty : (variables s)})
initNewVarEnv p exps
v <- execBlock b
modify (\s -> s{variables = tail(variables s)})
case v of
(Just vv) -> do return vv
_ -> do return IVoid
-- Actions: functions with side effects on a state
-- an Action is a State monad on Environment
type Action a = State Env a
-- a familiar name for Action whose return value is uninteresting
type Void = ()
-- iterate over a list of elementes
forEach :: [x] -> (x -> Action Void) -> Action Void
forEach ss comp = mapM_ comp ss
-- the environment
data Env = ENV {
variables :: [Map.Map Ident (Maybe InternalType)],
functions :: Map.Map Ident Fun,
outputs :: [String]
}
-- auxiliary functions
-- initial environment
initEnv :: Env
initEnv = ENV [Map.empty] Map.empty []
-- update the value of a variable
updateVar :: Ident -> InternalType -> Action Void
updateVar x v = modify (\s ->
s{variables = (Map.insert x (Just v) (head (variables s))) : (tail (variables s))}
)
updateFunc :: Ident -> Fun -> Action Void
updateFunc x f = modify (\s ->
s{functions = Map.insert x f (functions s)}
)
-- update with InitExp
updateVarWithExp :: Exp -> Action Void
updateVarWithExp ex = case ex of
EAssign i e -> do x <- eval e
updateVar i x
_ -> do return ()
updateVarInit :: Ident -> Action Void
updateVarInit x = modify (\s ->
s{variables = (Map.insert x Nothing (head (variables s))) : (tail (variables s))}
)
-- lookup the value of a variable
lookupVar :: Ident -> [(Map.Map Ident (Maybe InternalType))] -> Action InternalType
lookupVar key vars =
case (key,vars) of
(x,var1:vs) -> do
case Map.lookup x var1 of
Just (Just v) -> do return v
Nothing -> do
lookupVar x vs
(x, []) -> do error ("unknown variable " ++ printTree x)
lookupFunc :: Ident -> Action Fun
lookupFunc x = do
funcs <- gets functions
case Map.lookup x funcs of
Just v -> return v
Nothing -> error ("unknown function " ++ printTree x)
initNewVarEnv :: [Par] -> [Exp] -> Action Void
initNewVarEnv pars exps= do
case pars of
(Para t i):lpars -> do
case exps of
exp:lexps -> do
v <- eval exp
updateVar i v
m <- initNewVarEnv lpars lexps
return ()
[] -> do return ()
-- generate output
output :: String -> Action Void
output m = modify (\s ->
s{outputs = outputs s ++ [m]}
)
showI :: InternalType -> String
showI i = case i of
IInteger x -> show(x)
IDouble x -> show(x)
IString x -> show(x)
_ -> show(i)