-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.hs
56 lines (46 loc) · 1.47 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
module Interpreter where
import Data.Map
import Control.Monad.State
import AbsGrammar
import Definitions
import Semantic
import Utils
interpret :: Program (Maybe (Int, Int)) -> IO ()
interpret (Program _ decls funs) = do
evalStateT (process (Prelude.map (fmap getFirst) decls) (Prelude.map (fmap getFirst) funs)) (emptyEnv, emptyStore)
defineFunctions :: [TopDef Int] -> StateMonad ()
defineFunctions [] = return ()
defineFunctions (h:t) = do
semFunc h
defineFunctions t
defineGlobalVars :: [Decl Int] -> StateMonad ()
defineGlobalVars [] = return ()
defineGlobalVars (h:t) = do
semDecl h
defineGlobalVars t
process :: [Decl Int] -> [TopDef Int] -> StateMonad ()
process decls funs = do
defineGlobalVars decls
defineFunctions funs
semExpr (EApp 1 (Ident "main") [])
(env, st) <- get
if (Data.Map.null (cst st)) == False
then printCosts
else return ()
printCost :: (Ident, Cost) -> StateMonad ()
printCost (ident, cost) = do
lift $ putStrLn $ "Cost of " ++ show ident
lift $ putStrLn $ "Arithmetic :\t" ++ show (arit cost) ++ " ops"
lift $ putStrLn $ "Logical ops:\t" ++ show (logic cost) ++ " ops"
lift $ putStrLn $ "Memory alloc:\t" ++ show (mem cost) ++ " B"
printCostRec :: [(Ident, Cost)] -> StateMonad ()
printCostRec [] = return ()
printCostRec (h:t) = do
lift $ putStrLn ""
printCost h
printCostRec t
printCosts :: StateMonad ()
printCosts = do
lift $ putStr "\n======================"
(env, st) <- get
printCostRec (assocs (cst st))