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 m = ErrorT String m
26 interpret :: [Func] -> [Stm] -> IO ()
28 runStateT (runErrorT (do mapM addFunction fun; mapM_ execute st)) emptyState
31 --eval :: (MonadState State m, MonadError String m, MonadIO m) => Exp -> m Value
32 eval :: (MonadState State m, MonadIO m) => Exp -> EvalM m Value
33 eval (EBool b) = return (VBool b)
34 eval (EInt n) = return (VInt n)
35 eval (EVar i) = getVariableValue i
39 eval (BiOpExp e o e') = do
42 if elem o [Eq,NEq] then return $ opE o v v'
43 else let (VInt n1) = v in let (VInt n2) = v' in return $ op o n1 n2
45 (VInt n) <- getVariableValue i
46 setVariableValue i $ op o n 1
53 return $ VBool $ not b
55 s <- liftIO $ getNumber
56 return $ VInt $ read s
58 s <- liftIO $ getNumber
59 return $ VBool $ if (read s == 0) then False else True
60 eval (EFunc i as) = do
63 (ds,ss) <- lookup i $ functions state
64 let m = foldr (\((Decl t i),v) m -> insert i v m) empty $ zip ds vs
65 in modify (\s -> s{variables=[m]})
66 (mapM_ execute ss >> (fail $ "Function "++show i++" didn't return anything."))
67 `catchError` (\_ ->return ()) --Only errors thrown in ErrorT can be caught here, runtime errors pass through, so no need to check the error
72 --Stricter evaluation of the input
73 getNumber :: IO String
76 if elem c ['-','0'..'9']
80 else fail "Non integer input."
82 getNumber2 :: IO String
91 -- op :: Op -> (a -> a -> Value)
92 opE Eq = \e e' -> VBool $ e == e'
93 opE NEq = \e e' -> VBool $ not (e == e')
94 op Plus = \e e' -> VInt $ e + e'
95 op Minus = \e e' -> VInt $ e - e'
96 op Times = \e e' -> VInt $ e * e'
97 op Div = \e e' -> VInt $ e `div` e'
98 op Lt = \e e' -> VBool $ e < e'
99 op ELt = \e e' -> VBool $ e <= e'
100 op Gt = \e e' -> VBool $ e > e'
101 op EGt = \e e' -> VBool $ e >= e'
103 getVariableValue :: (MonadState State m) => Ident -> m Value
104 getVariableValue i = do
106 findVariable i $ variables s
108 findVariable :: (Monad m) => Ident -> Variables -> m Value
109 findVariable i [] = fail $ "Variable "++show i++" not found in any scope."
110 findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms
112 setVariableValue :: (MonadState State m) => Ident -> Value -> EvalM m Value
113 setVariableValue i v = do
114 modify (\s -> s{variables= updateVariable i v $ variables s} )
117 updateVariable :: Ident -> Value -> Variables -> Variables
118 updateVariable _ _ [] = []
119 updateVariable i v (m:ms) = if member i m then insert i v m:ms else m:updateVariable i v ms
121 pushAndPop :: (MonadState State m) => m a -> m ()
123 modify (\s -> s { variables = empty:variables s})
125 modify (\s -> s { variables = tail $ variables s})
127 -- execute :: (MonadState Variables m) => Stm -> m ()
128 execute :: (MonadState State m, MonadIO m) => Stm -> EvalM m ()
129 execute (SNoop) = return ()
130 execute (SExp e) = eval e >> return ()
131 execute (SIf b s s') = do
133 pushAndPop $ if b' then execute s else execute s'
134 execute (SPrint e) = do
137 execute (SBlock ss) = pushAndPop $ mapM execute ss
138 execute (SWhile e s) = do
140 if b then pushAndPop (execute s) >> execute (SWhile e s) else return ()
141 execute (SDeclD t i) = execute $ SDecl t i $ case t of
144 execute (SDecl t i e) =do
146 (m:ms) <- liftM variables get
147 modify (\s -> s{variables=insert i v m:ms })
148 execute (SReturn e) = do
150 modify (\s -> s{ret=v})
151 throwError "Returning.."
153 addFunction :: (MonadState State m) => Func -> m ()
154 addFunction (Func _ i d ss) = modify (\s -> s{functions=insert i (d,ss) (functions s) })