1 module 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 --eval :: (MonadState Variables m) => Exp -> m Value
25 eval :: Exp -> StateT State IO Value
26 eval (EBool b) = return (VBool b)
27 eval (EInt n) = return (VInt n)
28 eval (EVar i) = getVariableValue i
29 eval (EAss i e) = setVariableValue i e
30 eval (BiOpExp e o e') = do
33 if elem o [Eq,NEq] then return $ opE o v v'
34 else let (VInt n1) = v in let (VInt n2) = v' in return $ op o n1 n2
36 (VInt n) <- getVariableValue i
37 let (VInt n') = op o n 1 in setVariableValue i $ EInt n'
44 return $ VBool $ not b
47 return $ VInt $ read s
50 return $ VBool $ if (read s == 0) then False else True
51 eval (EFunc i as) = do
54 (ds,ss) <- lookup i $ functions state
55 modify (\s -> s{variables=[empty]})
57 mapM_ execute ss `catchError` (\_ -> return ())
59 v <- lift $ takeMVar $ ret state
62 addParams :: [Value] -> [Decl] -> StateT State IO ()
63 addParams [] [] = return ()
64 addParams (v:vs) ((Decl t i):ds) = do
66 let (m:ms) = variables state in modify (\s -> s{variables=insert i v m:ms })
73 if elem c [' ', '\n', '\t', '\r']
79 -- op :: Op -> (a -> a -> Value)
80 opE Eq = \e e' -> VBool $ e == e'
81 opE NEq = \e e' -> VBool $ not (e == e')
82 op Plus = \e e' -> VInt $ e + e'
83 op Minus = \e e' -> VInt $ e - e'
84 op Times = \e e' -> VInt $ e * e'
85 op Div = \e e' -> VInt $ e `div` e'
86 op Lt = \e e' -> VBool $ e < e'
87 op ELt = \e e' -> VBool $ e <= e'
88 op Gt = \e e' -> VBool $ e > e'
89 op EGt = \e e' -> VBool $ e >= e'
91 getVariableValue :: (MonadState State m) => Ident -> m Value
92 getVariableValue i = do
94 findVariable i $ variables s
96 findVariable :: (MonadState State m) => Ident -> Variables -> m Value
97 findVariable i [] = fail $ "Variable "++show i++" not found in any scope."
98 findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms
100 --setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value
101 --setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value
103 setVariableValue :: Ident -> Exp -> StateT State IO Value
104 setVariableValue i e = do
107 modify (\s -> s{variables= updateVariable i v $ variables state} )
110 updateVariable :: Ident -> Value -> Variables -> Variables
111 updateVariable _ _ [] = []
112 updateVariable i v (m:ms) = if member i m then insert i v m:ms else m:updateVariable i v ms
114 pushAndPop :: (MonadState State m) => m a -> m ()
116 modify (\s -> s { variables = empty:variables s})
118 modify (\s -> s { variables = tail $ variables s})
120 -- execute :: (MonadState Variables m) => Stm -> m ()
121 execute :: Stm -> StateT State IO ()
122 execute (SNoop) = return ()
123 execute (SExp e) = eval e >> return ()
124 execute (SIf b s s') = do
126 pushAndPop $ if b' then execute s else execute s'
127 execute (SPrint e) = do
130 execute (SBlock ss) = pushAndPop $ mapM execute ss
131 execute (SWhile e s) = do
133 if b then pushAndPop (execute s) >> execute (SWhile e s) else return ()
134 execute (SDeclD t i) = execute $ SDecl t i $ case t of
137 execute (SDecl t i e) =do
140 let (m:ms) = variables state in modify (\s -> s{variables=insert i v m:ms })
141 execute (SReturn e) = do
144 lift $ putMVar (ret s) v
145 throwError $ userError "Returning.."
147 addFunction :: (MonadState State m) => Func -> m ()
148 addFunction (Func _ i d ss) = modify (\s -> s{functions=insert i (d,ss) (functions s) })