1 module Interpret (interpret, eval, execute,addFunction, emptyState, Value(..), State(..)) where
4 import Control.Monad.State hiding (State)
5 import Control.Monad.Error
6 import Control.Concurrent.MVar
8 import Prelude hiding (lookup)
10 emptyState = State{variables=[empty], functions=(empty), ret=(VInt 0)}
12 data Value = VInt Integer | VBool Bool deriving Eq
14 instance Show Value where
15 show (VInt n) = show n
16 show (VBool True) = "1"
17 show (VBool False) = "0"
19 type Variables = [Map Ident Value]
20 type Function = ([Decl],[Stm])
22 data State = State {variables::Variables,functions::(Map Ident Function),ret::Value}
24 type EvalM = ErrorT String (StateT State IO)
26 interpret :: [Func] -> [Stm] -> IO ()
28 runStateT (runErrorT (do mapM Interpret.addFunction fun; mapM_ execute st)) emptyState
31 --eval :: (MonadState Variables m) => Exp -> m Value
32 eval :: Exp -> EvalM Value
33 eval (EBool b) = return (VBool b)
34 eval (EInt n) = return (VInt n)
35 eval (EVar i) = getVariableValue i
36 eval (EAss i e) = setVariableValue i e
37 eval (BiOpExp e o e') = do
40 if elem o [Eq,NEq] then return $ opE o v v'
41 else let (VInt n1) = v in let (VInt n2) = v' in return $ op o n1 n2
43 (VInt n) <- getVariableValue i
44 let (VInt n') = op o n 1 in setVariableValue i $ EInt n'
51 return $ VBool $ not b
54 return $ VInt $ read s
57 return $ VBool $ if (read s == 0) then False else True
58 eval (EFunc i as) = do
61 (ds,ss) <- lookup i $ functions state
62 let m = foldr (\((Decl t i),v) m -> insert i v m) empty $ zip ds vs
63 in modify (\s -> s{variables=[m]})
64 mapM_ execute ss `catchError` (\_ -> return ())
72 if elem c [' ', '\n', '\t', '\r']
78 -- op :: Op -> (a -> a -> Value)
79 opE Eq = \e e' -> VBool $ e == e'
80 opE NEq = \e e' -> VBool $ not (e == e')
81 op Plus = \e e' -> VInt $ e + e'
82 op Minus = \e e' -> VInt $ e - e'
83 op Times = \e e' -> VInt $ e * e'
84 op Div = \e e' -> VInt $ e `div` e'
85 op Lt = \e e' -> VBool $ e < e'
86 op ELt = \e e' -> VBool $ e <= e'
87 op Gt = \e e' -> VBool $ e > e'
88 op EGt = \e e' -> VBool $ e >= e'
90 getVariableValue :: (MonadState State m) => Ident -> m Value
91 getVariableValue i = do
93 findVariable i $ variables s
95 findVariable :: (MonadState State m) => Ident -> Variables -> m Value
96 findVariable i [] = fail $ "Variable "++show i++" not found in any scope."
97 findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms
99 setVariableValue :: Ident -> Exp -> EvalM Value
100 setVariableValue i e = do
103 modify (\s -> s{variables= updateVariable i v $ variables state} )
106 updateVariable :: Ident -> Value -> Variables -> Variables
107 updateVariable _ _ [] = []
108 updateVariable i v (m:ms) = if member i m then insert i v m:ms else m:updateVariable i v ms
110 pushAndPop :: (MonadState State m) => m a -> m ()
112 modify (\s -> s { variables = empty:variables s})
114 modify (\s -> s { variables = tail $ variables s})
116 -- execute :: (MonadState Variables m) => Stm -> m ()
117 execute :: Stm -> EvalM ()
118 execute (SNoop) = return ()
119 execute (SExp e) = eval e >> return ()
120 execute (SIf b s s') = do
122 pushAndPop $ if b' then execute s else execute s'
123 execute (SPrint e) = do
126 execute (SBlock ss) = pushAndPop $ mapM execute ss
127 execute (SWhile e s) = do
129 if b then pushAndPop (execute s) >> execute (SWhile e s) else return ()
130 execute (SDeclD t i) = execute $ SDecl t i $ case t of
133 execute (SDecl t i e) =do
136 let (m:ms) = variables state in modify (\s -> s{variables=insert i v m:ms })
137 execute (SReturn e) = do
139 modify (\s -> s{ret=v})
140 throwError "Returning.."
142 addFunction :: (MonadState State m) => Func -> m ()
143 addFunction (Func _ i d ss) = modify (\s -> s{functions=insert i (d,ss) (functions s) })