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)}
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::(MVar Value)}
24 interpret :: [Func] -> [Stm] -> IO ()
27 runStateT (do mapM Interpret.addFunction fun; mapM execute st) emptyState{ret=mv}
30 --eval :: (MonadState Variables m) => Exp -> m Value
31 eval :: Exp -> StateT State IO Value
32 eval (EBool b) = return (VBool b)
33 eval (EInt n) = return (VInt n)
34 eval (EVar i) = getVariableValue i
35 eval (EAss i e) = setVariableValue i e
36 eval (BiOpExp e o e') = do
39 if elem o [Eq,NEq] then return $ opE o v v'
40 else let (VInt n1) = v in let (VInt n2) = v' in return $ op o n1 n2
42 (VInt n) <- getVariableValue i
43 let (VInt n') = op o n 1 in setVariableValue i $ EInt n'
50 return $ VBool $ not b
53 return $ VInt $ read s
56 return $ VBool $ if (read s == 0) then False else True
57 eval (EFunc i as) = do
60 (ds,ss) <- lookup i $ functions state
61 let m = foldr (\((Decl t i),v) m -> insert i v m) empty $ zip ds vs
62 in modify (\s -> s{variables=[m]})
63 mapM_ execute ss `catchError` (\_ -> return ())
64 v <- lift $ takeMVar $ ret state
71 if elem c [' ', '\n', '\t', '\r']
77 -- op :: Op -> (a -> a -> Value)
78 opE Eq = \e e' -> VBool $ e == e'
79 opE NEq = \e e' -> VBool $ not (e == e')
80 op Plus = \e e' -> VInt $ e + e'
81 op Minus = \e e' -> VInt $ e - e'
82 op Times = \e e' -> VInt $ e * e'
83 op Div = \e e' -> VInt $ e `div` e'
84 op Lt = \e e' -> VBool $ e < e'
85 op ELt = \e e' -> VBool $ e <= e'
86 op Gt = \e e' -> VBool $ e > e'
87 op EGt = \e e' -> VBool $ e >= e'
89 getVariableValue :: (MonadState State m) => Ident -> m Value
90 getVariableValue i = do
92 findVariable i $ variables s
94 findVariable :: (MonadState State m) => Ident -> Variables -> m Value
95 findVariable i [] = fail $ "Variable "++show i++" not found in any scope."
96 findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms
98 --setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value
99 --setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value
101 setVariableValue :: Ident -> Exp -> StateT State IO Value
102 setVariableValue i e = do
105 modify (\s -> s{variables= updateVariable i v $ variables state} )
108 updateVariable :: Ident -> Value -> Variables -> Variables
109 updateVariable _ _ [] = []
110 updateVariable i v (m:ms) = if member i m then insert i v m:ms else m:updateVariable i v ms
112 pushAndPop :: (MonadState State m) => m a -> m ()
114 modify (\s -> s { variables = empty:variables s})
116 modify (\s -> s { variables = tail $ variables s})
118 -- execute :: (MonadState Variables m) => Stm -> m ()
119 execute :: Stm -> StateT State IO ()
120 execute (SNoop) = return ()
121 execute (SExp e) = eval e >> return ()
122 execute (SIf b s s') = do
124 pushAndPop $ if b' then execute s else execute s'
125 execute (SPrint e) = do
128 execute (SBlock ss) = pushAndPop $ mapM execute ss
129 execute (SWhile e s) = do
131 if b then pushAndPop (execute s) >> execute (SWhile e s) else return ()
132 execute (SDeclD t i) = execute $ SDecl t i $ case t of
135 execute (SDecl t i e) =do
138 let (m:ms) = variables state in modify (\s -> s{variables=insert i v m:ms })
139 execute (SReturn e) = do
142 lift $ putMVar (ret s) v
143 throwError $ userError "Returning.."
145 addFunction :: (MonadState State m) => Func -> m ()
146 addFunction (Func _ i d ss) = modify (\s -> s{functions=insert i (d,ss) (functions s) })