-
Notifications
You must be signed in to change notification settings - Fork 2
/
Interpreter.hs
359 lines (280 loc) · 11.7 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
module Interpreter where
import AbsGrammar
import Data.Map as Map
import Data.Maybe
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Except
import Types
import Memory
import TypeChecker
-- -- Expressions support
evalRelOp GTH e1 e2 = e1 > e2
evalRelOp GE e1 e2 = e1 >= e2
evalRelOp LTH e1 e2 = e1 < e2
evalRelOp EQU e1 e2 = e1 == e2
evalRelOp NE e1 e2 = e1 /= e2
evalRelOp LE e1 e2 = e1 <= e2
evalMulOp Div e1 e2 = div e1 e2
evalMulOp Times e1 e2 = e1 * e2
evalMulOp Mod e1 e2 = e1 `mod` e2
evalAddOp Minus e1 e2 = e1 - e2
evalAddOp Plus e1 e2 = e1 + e2
evalExpression :: Expr -> RR MemVal
evalExpression (EVar ident) = readFromMemory ident
evalExpression (EApp ident vars) = do
(env2, Just ret) <- runFunc ident vars
return ret
-- STRUCTS
-- evalExpression (EStructField2 expr field) = do
-- StructVal val <- evalExpression expr
-- extractFieldFromStructHelper val field
-- -- int
evalExpression (ELitInt x) = return $ IntVal x
evalExpression (EAdd e1 op e2) = do
IntVal r1 <- evalExpression e1
IntVal r2 <- evalExpression e2
return $ IntVal $ evalAddOp op r1 r2
evalExpression (EMul e1 op e2) = do
IntVal r1 <- evalExpression e1
IntVal r2 <- evalExpression e2
case op of
Div -> if r2 == 0 then throwError DivisionByZeroException else return $ IntVal $ evalMulOp op r1 r2
Mod -> if r2 == 0 then throwError ModulusByZeroException else return $ IntVal $ evalMulOp op r1 r2
_ -> return $ IntVal $ evalMulOp op r1 r2
evalExpression (Neg e) = do
IntVal r <- evalExpression e
return $ IntVal $ -r
-- -- bool
evalExpression ELitFalse = return $ BoolVal False
evalExpression ELitTrue = return $ BoolVal True
evalExpression (Not e) = do
BoolVal r <- evalExpression e
return $ BoolVal $ not r
evalExpression (EAnd e1 e2) = do
BoolVal r1 <- evalExpression e1
BoolVal r2 <- evalExpression e2
return $ BoolVal $ r1 && r2
evalExpression (EOr e1 e2) = do
BoolVal r1 <- evalExpression e1
BoolVal r2 <- evalExpression e2
return $ BoolVal $ r1 || r2
evalExpression (ERel e1 op e2) = do
IntVal r1 <- evalExpression e1
IntVal r2 <- evalExpression e2
return $ BoolVal $ evalRelOp op r1 r2
-- -- string
evalExpression (EString e) = return $ StringVal e
-- -- lambda
evalExpression (ELambda capture args returnType (Block stmts)) = do
argsList <- mapM argToFunArg args
captureGroup <- mapM constructCaptureGroup capture
return $ FunVal (stmts, Map.empty, argsList, returnType, captureGroup)
-- -- list
evalExpression (EEmptyList t) = return $ ListVal (t, [])
evalExpression (ListAt listExpr index) = do
IntVal i <- evalExpression index
ListVal (typ, elems) <- evalExpression listExpr
if i < 0 || i >= fromIntegral (length elems) then
throwError $ OutOfRangeExeption i
else
return $ elems !! fromIntegral i
evalExpression (ListLength listExpr) = do
ListVal (typ, elems) <- evalExpression listExpr
return $ IntVal $ toInteger $ length elems
-- STRUCTS
-- evalExpression EAnonStruct = return $ StructVal Map.empty
argToFunArg :: Arg -> RR FunArg
argToFunArg (Arg t (Ident i)) = return (i, t, ByValue)
argToFunArg (RefArg t (Ident i)) = return (i, t, ByRef)
constructCaptureGroup :: Ident -> RR CaptureGroupElement
constructCaptureGroup ident = do
val <- readFromMemory ident
return (ident, val)
-- -- Functions support
-- arguments list, list of passed expressions or references, current environemnt
-- returns changed environment
applyVars :: FunArgList -> [ExprOrRef] -> MyEnv -> RR MyEnv
applyVars [] [] _ = ask
applyVars ((ident, typ, ByValue):funArgs) (ERExpr e:vars) curEnv = do
env <- applyVars funArgs vars curEnv
val <- local (const curEnv) (evalExpression e)
local (const env) (declareVar (Ident ident) val)
applyVars ((ident, typ, ByRef):funArgs) (ERRef ident2:vars) curEnv = do
env <- applyVars funArgs vars curEnv
loc <- local (const curEnv) (extractIdentFromMemory ident2)
let env2 = Map.insert ident loc env
return env2
applyCaptureGroup :: CaptureGroup -> RR MyEnv
applyCaptureGroup [] = ask
applyCaptureGroup ((ident, val):rest) = do
env <- applyCaptureGroup rest
local (const env) (declareVar ident val)
runFunc :: Expr -> [ExprOrRef] -> RR (MyEnv, ReturnResult)
runFunc expr vars = do
curEnv <- ask
FunVal (stmts, env, funVars, retType, captureGroup) <- evalExpression expr
env <- local (const env) (applyCaptureGroup captureGroup)
env <- local (const env) (applyVars funVars vars curEnv)
(env2, ret) <- local (const env) (interpretMany stmts)
if retType == Void then
return (env2, Just VoidVal)
else
if isNothing ret then
throwError NoReturnException
else
return (env2, ret)
loopInList :: Ident -> [MemVal] -> Stmt -> RR (MyEnv, ReturnResult)
loopInList id [] stmt = returnNothing
loopInList id (val:rest) stmt = do
putToMemory id val
execStmt stmt
loopInList id rest stmt
defaultValueForType :: Type -> Expr
defaultValueForType TInt = ELitInt 0
defaultValueForType TBool = ELitFalse
defaultValueForType TString = EString ""
defaultValueForType (TList t) = EEmptyList t
-- defaultValueForType (TStruct (Ident i)) = EAnonStruct
-- Statements
returnNothing :: RR (MyEnv, ReturnResult)
returnNothing = do
env <- ask
return (env, Nothing)
memValToString :: MemVal -> String
memValToString (IntVal i) = show i
memValToString (BoolVal b) = show b
memValToString (StringVal s) = s
memValToString (FunVal (stmt, env, arg, typ, capture)) = "Function (" ++ show arg ++ ") -> " ++ show typ
memValToString (ListVal (typ, elems)) = "[" ++ (Prelude.foldl (\a b -> a ++ b ++ ", " ) "" (Prelude.map memValToString elems)) ++ "]"
memValToString e = show e
formatPrint :: String -> [MemVal] -> RR ()
formatPrint x [] = liftIO $ putStr x
formatPrint [] _ = return ()
formatPrint (chr:chrs) (v:val) =
if chr == '_' then
do
liftIO $ putStr $ memValToString v
formatPrint chrs val
else
do
liftIO $ putStr [chr]
formatPrint chrs (v:val)
-- STRUCTS
-- recursiveAssignmentStruct :: StructDef -> Fields -> MemVal -> RR StructDef
-- recursiveAssignmentStruct struct (Field (Ident fieldName)) val = return $ Map.insert fieldName val struct
-- recursiveAssignmentStruct struct (Fields (Ident field) fields) val = do
-- let Just (StructVal subStruct) = Map.lookup field struct
-- changed <- recursiveAssignmentStruct subStruct fields val
-- return $ Map.insert field (StructVal changed) struct
-- -- EXECUTE STATEMENT -- --
execStmt :: Stmt -> RR (MyEnv, ReturnResult)
-- -- Declarations
execStmt (Decl t (NoInit ident)) = execStmt (Decl t (Init ident (defaultValueForType t)))
execStmt (Decl (AnonFun retType argsTypes) (Init ident e)) = do
func <- evalExpression e
env2 <- declareVar ident func
return (env2, Nothing)
execStmt (Decl typ (Init ident e)) = do
val <- evalExpression e
env <- declareVar ident val
return (env, Nothing)
execStmt (FnDef ident args returnType (Block stmts)) = do
argsList <- mapM argToFunArg args
env <- declareVar ident VoidVal
local (const env) (putToMemory ident (FunVal (stmts, env, argsList, returnType, [])))
return (env, Nothing)
-- -- assigment
execStmt (Ass ident e) = do
newVal <- evalExpression e
putToMemory ident newVal
returnNothing
-- STRUCTS
-- execStmt (StructAss ident fields expr) = do
-- val <- evalExpression expr
-- StructVal struct <- readFromMemory ident
-- newStruct <- recursiveAssignmentStruct struct fields val
-- putToMemory ident (StructVal newStruct)
-- returnNothing
-- -- return
execStmt VRet = do
env <- ask
return (env, Just VoidVal)
execStmt (Ret expr) = do
env <- ask
val <- evalExpression expr
return (env, Just val)
-- -- expr
execStmt (SExp e) = do
res <- evalExpression e
isResVoid <- isVoid res
isInteractive <- isInterativeMode
unless (not isInteractive || isResVoid) (do
let str = memValToString res
liftIO $ putStr str)
returnNothing
-- -- lists
execStmt (ListPush ident e) = do
(typ, elems) <- readListFromMemory ident
val <- evalExpression e
putToMemory ident (ListVal (typ, elems ++ [val]))
returnNothing
execStmt (ForIn ident list stmt) = do
ListVal (typ, elems) <- evalExpression list
loopInList ident elems stmt
-- -- if, while
execStmt (While expr stmt) = do
BoolVal res <- evalExpression expr
if res then
do
(env, ret) <- execStmt stmt
if isNothing ret then
execStmt (While expr stmt)
else
return (env, ret)
else
returnNothing
execStmt (For ident start end body) = do
IntVal s <- evalExpression start
IntVal e <- evalExpression end
execStmt $ BStmt $ Block [Ass ident (ELitInt s), While (ERel (EVar ident) LE (ELitInt e)) (BStmt $ Block [body, Ass ident (EAdd (EVar ident) Plus (ELitInt 1))])]
-- STRUCTS
-- execStmt (ForField ident fields start end body) = do
-- IntVal s <- evalExpression start
-- IntVal e <- evalExpression end
-- execStmt $ BStmt $ Block [StructAss ident fields (ELitInt s), While (ERel (EStructField2 (EVar ident) fields) LE (ELitInt e)) (BStmt $ Block [body, StructAss ident fields (EAdd (EStructField2 (EVar ident) fields) Plus (ELitInt 1))])]
execStmt (Cond expr (Block stmts)) = do
BoolVal res <- evalExpression expr
if res then
interpretMany stmts
else
returnNothing
execStmt (CondElse expr (Block stmts) (Block stmtsElse)) = do
BoolVal res <- evalExpression expr
if res then interpretMany stmts else interpretMany stmtsElse
-- -- structs
-- execStmt (StructDef (Ident i) fields) = do
-- -- print
execStmt (Print e) = do
res <- evalExpression e
let str = memValToString res
liftIO $ putStr str
returnNothing
execStmt (SFormat format exprs) = do
values <- mapM evalExpression exprs
formatPrint format values
returnNothing
-- -- block
execStmt (BStmt (Block stmts)) = do
env <- ask
(env2, ret) <- interpretMany stmts
return (env, ret)
interpretMany :: [Stmt] -> RR (MyEnv, ReturnResult)
interpretMany (s:xs) = do
(env, ret) <- execStmt s
if isNothing ret then
local (const env) (interpretMany xs)
else
return (env, ret)
interpretMany [] = returnNothing
runMyMonad prog mode = runExceptT $ runStateT (runReaderT (interpretMany prog) Map.empty) (Map.empty, 0, mode)