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 let m = foldr (\((Decl t i),v) m -> insert i v m) empty $ zip ds vs
56 in modify (\s -> s{variables=[m]})
57 mapM_ execute ss `catchError` (\_ -> return ())
58 v <- lift $ takeMVar $ ret state
65 if elem c [' ', '\n', '\t', '\r']
71 -- op :: Op -> (a -> a -> Value)
72 opE Eq = \e e' -> VBool $ e == e'
73 opE NEq = \e e' -> VBool $ not (e == e')
74 op Plus = \e e' -> VInt $ e + e'
75 op Minus = \e e' -> VInt $ e - e'
76 op Times = \e e' -> VInt $ e * e'
77 op Div = \e e' -> VInt $ e `div` e'
78 op Lt = \e e' -> VBool $ e < e'
79 op ELt = \e e' -> VBool $ e <= e'
80 op Gt = \e e' -> VBool $ e > e'
81 op EGt = \e e' -> VBool $ e >= e'
83 getVariableValue :: (MonadState State m) => Ident -> m Value
84 getVariableValue i = do
86 findVariable i $ variables s
88 findVariable :: (MonadState State m) => Ident -> Variables -> m Value
89 findVariable i [] = fail $ "Variable "++show i++" not found in any scope."
90 findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms
92 --setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value
93 --setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value
95 setVariableValue :: Ident -> Exp -> StateT State IO Value
96 setVariableValue i e = do
99 modify (\s -> s{variables= updateVariable i v $ variables state} )
102 updateVariable :: Ident -> Value -> Variables -> Variables
103 updateVariable _ _ [] = []
104 updateVariable i v (m:ms) = if member i m then insert i v m:ms else m:updateVariable i v ms
106 pushAndPop :: (MonadState State m) => m a -> m ()
108 modify (\s -> s { variables = empty:variables s})
110 modify (\s -> s { variables = tail $ variables s})
112 -- execute :: (MonadState Variables m) => Stm -> m ()
113 execute :: Stm -> StateT State IO ()
114 execute (SNoop) = return ()
115 execute (SExp e) = eval e >> return ()
116 execute (SIf b s s') = do
118 pushAndPop $ if b' then execute s else execute s'
119 execute (SPrint e) = do
122 execute (SBlock ss) = pushAndPop $ mapM execute ss
123 execute (SWhile e s) = do
125 if b then pushAndPop (execute s) >> execute (SWhile e s) else return ()
126 execute (SDeclD t i) = execute $ SDecl t i $ case t of
129 execute (SDecl t i e) =do
132 let (m:ms) = variables state in modify (\s -> s{variables=insert i v m:ms })
133 execute (SReturn e) = do
136 lift $ putMVar (ret s) v
137 throwError $ userError "Returning.."
139 addFunction :: (MonadState State m) => Func -> m ()
140 addFunction (Func _ i d ss) = modify (\s -> s{functions=insert i (d,ss) (functions s) })