-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
399 lines (340 loc) · 13.6 KB
/
Main.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
import qualified Language.ECMAScript3.Parser as Parser
import Language.ECMAScript3.Syntax
import Control.Monad
import Control.Applicative
import Data.Map as Map (Map, insert, lookup, union, toList, empty)
import Debug.Trace
import Value
--
-- Evaluate functions
--
evalExpr :: StateT -> Expression -> StateTransformer Value
evalExpr env (VarRef (Id id)) = stateLookup env id
evalExpr env (IntLit int) = return $ Int int
evalExpr env (BoolLit bool) = return $ Bool bool
evalExpr env (StringLit string) = return $ String string -- our code avaliar string
evalExpr env (InfixExpr op expr1 expr2) = do
v1 <- evalExpr env expr1
v2 <- evalExpr env expr2
infixOp env op v1 v2
evalExpr env (AssignExpr OpAssign (LVar var) expr) = do -- x=0;
varScope <- stateLookup env var -- stores wether the variable will be global ou not
e <- evalExpr env expr
case varScope of
GlobalVar -> createGlobalVar var e
_ -> setVar var e -- sets it in the first scope it finds the variable
evalExpr env (DotRef expr id) = do -- bar.foo
var <- evalExpr env expr
case id of -- checks which is the called property
(Id "head") -> do
case var of -- checks the list format
(List []) -> return (List [])
(List (l:ls)) -> return l
_ -> return $ Error "Não é um tipo válido"
(Id "tail") -> do
case var of
(List []) -> return (List [])
(List (l:ls)) -> return $ List ls
_ -> return $ Error "Não é um tipo válido"
(Id "len") -> do
case var of
(List _) -> do
return $ Int $ count var
where
count (List []) = 0
count (List (l:ls)) = 1 + (count (List ls))
_ -> return $ Error "Não é um tipo válido"
_ -> return $ Error "Não é uma função válida"
-- Listas
evalExpr env (ArrayLit []) = return $ List []
evalExpr env (ArrayLit [expr]) = do
val <- evalExpr env expr
return $ List [val]
evalExpr env (ArrayLit (l:ls)) = do
cabeca <- evalExpr env l
(List cauda) <- evalExpr env (ArrayLit ls)
return $ List (cabeca:cauda)
-- Obter elementos de dentro de listas
evalExpr env (BracketRef expr indexExpr) = do
list <- evalExpr env expr
case list of
(List _) -> do
index <- evalExpr env indexExpr
getElementFromList env list index
-- Function calls
evalExpr env (CallExpr name argsExpr) = do
case name of
(DotRef list (Id "concat")) -> do
(List list1) <- evalExpr env list
res <- evalExpr env (head argsExpr)
case res of
(List list2) -> return $ List $ list1 ++ list2
val -> return $ List $ list1 ++ [val]
_ -> do -- General function case
func <- evalExpr env name
case func of
(Function idd args stmts) -> do
addState env
result <- mapM (evalExpr env) argsExpr
let vars = (zip (map (\(Id a) -> a) args) result)
createLocalVars vars
res <- evalStmt env (BlockStmt stmts)
removeState env
case res of
Return val -> return val
Nil -> return Nil
_ -> return res
res -> error $ "Deu isso aqui: " ++ (show res)
evalStmt :: StateT -> Statement -> StateTransformer Value
evalStmt env EmptyStmt = return Nil
evalStmt env (VarDeclStmt []) = return Nil
evalStmt env (VarDeclStmt (decl:ds)) =
varDecl env decl >> evalStmt env (VarDeclStmt ds)
evalStmt env (ExprStmt expr) = evalExpr env expr
-- blocos de statements
evalStmt env (BlockStmt []) = return Nil
evalStmt env (BlockStmt [stmt]) = evalStmt env stmt
evalStmt env (BlockStmt (stmt:stmts)) = do
cabeca <- evalStmt env stmt
case cabeca of
Break -> return Break
Return val -> return $ Return val
_ -> evalStmt env (BlockStmt stmts)
-- if com um único stmt --
evalStmt env (IfSingleStmt expr stmt) = do
v <- evalExpr env expr
case v of
(Bool b) -> if (b) then do
addState env
res <- evalStmt env stmt
removeState env
return res
else return Nil
_ -> error $ "Not a valid expression"
-- if com 2 stmts -- if x then stmt1 else stmt2
evalStmt env (IfStmt expr stmt1 stmt2) = do
v <- evalExpr env expr
case v of
(Bool b) -> if (b) then do
addState env
res <- evalStmt env stmt1
removeState env
return res
else evalStmt env stmt2
_ -> error $ "Not a valid expression"
-- While
evalStmt env (WhileStmt expr stmt) = do
Bool b <- evalExpr env expr
if b then do
addState env
res <- evalStmt env stmt
removeState env
case res of
Break -> return Nil
Continue -> evalStmt env (WhileStmt expr stmt)
_ -> evalStmt env (WhileStmt expr stmt)
else return Nil
--DoWhile
evalStmt env (DoWhileStmt stmt expr) = do
addState env
res <- evalStmt env stmt
removeState env
Bool b <- evalExpr env expr
case res of
Break -> return Nil
Continue -> if b then evalStmt env (DoWhileStmt stmt expr) else return Nil
_ -> if b then evalStmt env (DoWhileStmt stmt expr) else return Nil
-- BreakStmt
evalStmt env (BreakStmt Nothing) = return Break;
-- ContinueStmt
evalStmt env (ContinueStmt Nothing) = return Continue;
-- ReturnStmt
evalStmt env (ReturnStmt maybeExpr) = do
case maybeExpr of
(Just expr) -> do
val <- evalExpr env expr
return $ Return val
Nothing -> return Nil
-- ForStmt for normal (MODIFICADO?)
evalStmt env (ForStmt initial test inc stmt) = do
v <- evalForInit env initial
testRes <- evalForTest env test -- Asks if the loop should continue
case testRes of
(Bool True) -> do
addState env
d <- evalStmt env stmt
removeState env
evalForInc env inc
case d of
Break -> return Nil
Continue -> evalStmt env (ForStmt NoInit test inc stmt)
_ -> evalStmt env (ForStmt NoInit test inc stmt)
(Bool False) -> return Nil
-- TODO Error
_ -> error "Not a valid expression"
-- ForInStmt
evalStmt env (ForInStmt initial expr stmt) = do
list <- evalExpr env expr
case initial of
(ForInVar (Id id)) -> forLoop env id list stmt
(ForInLVal (LVar id)) -> forLoop env id list stmt
evalStmt env (FunctionStmt id@(Id funcId) args stmts) = createGlobalVar funcId (Function id args stmts) --Talvez nao precise ser global
-- Searches for an element from a list using an index
getElementFromList :: StateT -> Value -> Value -> StateTransformer Value
getElementFromList _ (List []) _ = error $ "Index out of bounds exception" -- ver msg melhor?
getElementFromList env (List (l:ls)) (Int index) = if index == 0 then return l else getElementFromList env (List ls) (Int (index-1))
-- Evaluates For increment expression
evalForInc :: StateT -> (Maybe Expression) -> StateTransformer Value
evalForInc _ Nothing = return Nil
evalForInc env (Just inc) = evalExpr env inc
-- Evaluates For test expression
evalForTest :: StateT -> (Maybe Expression) -> StateTransformer Value
evalForTest _ Nothing = return $ Bool True
evalForTest env (Just test) = evalExpr env test
-- Evaluates For initialization
evalForInit :: StateT -> ForInit -> StateTransformer Value
evalForInit env NoInit = return Nil
evalForInit env (VarInit list) = evalStmt env (VarDeclStmt list)
evalForInit env (ExprInit expr) = evalExpr env expr
-- Loop forIn
forLoop :: StateT -> String -> Value -> Statement -> StateTransformer Value
forLoop env idd (List []) stmt = return Break
forLoop env idd (List (l:ls)) stmt = do
addState env
setVar idd l
res <- evalStmt env stmt
removeState env
case res of
Break -> return Break
Continue -> forLoop env idd (List ls) stmt
_ -> forLoop env idd (List ls) stmt
-- Do not touch this one :)
evaluate :: StateT -> [Statement] -> StateTransformer Value
evaluate env [] = return Nil
evaluate env stmts = foldl1 (>>) $ map (evalStmt env) stmts
--
-- Operators
--
infixOp :: StateT -> InfixOp -> Value -> Value -> StateTransformer Value
infixOp env OpAdd (Int v1) (Int v2) = return $ Int $ v1 + v2
infixOp env OpSub (Int v1) (Int v2) = return $ Int $ v1 - v2
infixOp env OpMul (Int v1) (Int v2) = return $ Int $ v1 * v2
infixOp env OpDiv (Int v1) (Int v2) = return $ Int $ div v1 v2
infixOp env OpMod (Int v1) (Int v2) = return $ Int $ mod v1 v2
infixOp env OpLT (Int v1) (Int v2) = return $ Bool $ v1 < v2
infixOp env OpLEq (Int v1) (Int v2) = return $ Bool $ v1 <= v2
infixOp env OpGT (Int v1) (Int v2) = return $ Bool $ v1 > v2
infixOp env OpGEq (Int v1) (Int v2) = return $ Bool $ v1 >= v2
infixOp env OpEq (Int v1) (Int v2) = return $ Bool $ v1 == v2
infixOp env OpNEq (Int v1) (Int v2) = return $ Bool $ v1 /= v2
infixOp env OpEq (Bool v1) (Bool v2) = return $ Bool $ v1 == v2
infixOp env OpNEq (Bool v1) (Bool v2) = return $ Bool $ v1 /= v2
infixOp env OpLAnd (Bool v1) (Bool v2) = return $ Bool $ v1 && v2
infixOp env OpLOr (Bool v1) (Bool v2) = return $ Bool $ v1 || v2
-- Comparação de listas
-- Igualdade
infixOp env OpEq (List []) (List []) = return $ Bool True
infixOp env OpEq (List []) (List _) = return $ Bool False
infixOp env OpEq (List _) (List []) = return $ Bool False
infixOp env OpEq (List (l:ls)) (List (x:xs)) = do
result <- infixOp env OpEq l x
resto <- infixOp env OpEq (List ls) (List xs)
infixOp env OpLAnd result resto
-- Diferença
infixOp env OpNEq (List []) (List []) = return $ Bool False
infixOp env OpNEq (List []) (List _) = return $ Bool True
infixOp env OpNEq (List _) (List []) = return $ Bool True
infixOp env OpNEq (List (l:ls)) (List (x:xs)) = do
result <- infixOp env OpNEq l x
resto <- infixOp env OpNEq (List ls) (List xs)
infixOp env OpLOr result resto
infixOp _ _ val1 val2 = error $ "infixOp error with " ++ (show val1) ++ " and " ++ (show val2)
--
-- Environment and auxiliary functions
--
environment :: StateT
environment = [Map.empty]
-- look for state
stateLookup :: StateT -> String -> StateTransformer Value
stateLookup env var = ST $ \s ->
let pops [] _ = Nothing
pops (s:states) var =
case Map.lookup var s of
Nothing -> pops states var
Just v -> Just v
in case pops s var of
Nothing -> (GlobalVar, s) -- nao achou em nenhuma memoria
Just v -> (v, s) -- achou em alguma memoria
-- Declara uma variavel no escopo local, podendo ou não guardar um valor
varDecl :: StateT -> VarDecl -> StateTransformer Value
varDecl env (VarDecl (Id id) maybeExpr) = do
case maybeExpr of
Nothing -> createLocalVar id Nil
(Just expr) -> do
val <- evalExpr env expr
createLocalVar id val
-- Procura, a partir de um estado local até o global, uma variável chamada var para
-- setar seu valor como val.
setVar :: String -> Value -> StateTransformer Value
setVar var val = ST $ \s -> (val, setVarIntoState s)
where
setVarIntoState [] = error $ "Global is dead"
setVarIntoState (state:states) = case (Map.lookup var state) of
Nothing -> state:(setVarIntoState states)
Just v -> (insert var val state):states
setVars :: [(String, Value)] -> StateTransformer Value
setVars [] = return Nil
setVars ((id, val):vars) = do
setVar id val
setVars vars
-- Cria uma variavel APENAS no escopo local
createLocalVar :: String -> Value -> StateTransformer Value
createLocalVar var val = ST $ \(s:states) -> (val, (insert var val s):states)
createLocalVars :: [(String, Value)] -> StateTransformer Value
createLocalVars [] = return Nil -- var x;
createLocalVars ((id, val):vars) = do -- var y = 0;
createLocalVar id val
createLocalVars vars
-- Percorre os estados de memória até o global e cria uma variável APENAS no global
createGlobalVar :: String -> Value -> StateTransformer Value
createGlobalVar var val = ST $ \s -> (val, global var val s)
where
global var val (state:[]) = (insert var val state):[]
global var val (state:states) = state:(global var val states)
-- Adiciona uma camada (escopo) de memória mais local
addState :: StateT -> StateTransformer Value
addState env = ST $ \s -> (Nil, Map.empty:s)
-- Remove a camada (escopo) de memória mais local
removeState :: StateT -> StateTransformer Value
removeState env = ST $ \(s:states) -> (Nil, states)
--
-- Types and boilerplate
--
type StateT = [Map String Value]
data StateTransformer t = ST (StateT -> (t, StateT))
instance Monad StateTransformer where
return x = ST $ \s -> (x, s)
(>>=) (ST m) f = ST $ \s ->
let (v, newS) = m s
(ST resF) = f v
in resF newS
instance Functor StateTransformer where
fmap = liftM
instance Applicative StateTransformer where
pure = return
(<*>) = ap
--
-- Main and results functions
--
-- Imprime apenas a memória global
showResult :: (Value, StateT) -> String
showResult (val, defs) =
show val ++ "\n" ++ show (toList $ union (last defs) (last environment)) ++ "\n"
getResult :: StateTransformer Value -> (Value, StateT)
getResult (ST f) = f [Map.empty]
main :: IO ()
main = do
js <- Parser.parseFromFile "Main.js"
let statements = unJavaScript js
putStrLn $ "AST: " ++ (show $ statements) ++ "\n"
putStr $ showResult $ getResult $ evaluate environment statements