-module Interpret (eval, execute,addFunction, emptyState, Value(..), State(..)) where
+module Interpret (interpret, eval, execute,addFunction, emptyState, Value(..), State(..)) where
import Abssyntax
import Control.Monad.State hiding (State)
+import Control.Monad.Error
+import Control.Concurrent.MVar
import Data.Map as Map
import Prelude hiding (lookup)
-emptyState = State{variables=[empty], functions=(empty)}
+emptyState = State{variables=[empty], functions=(empty), ret=(VInt 0)}
data Value = VInt Integer | VBool Bool deriving Eq
type Variables = [Map Ident Value]
type Function = ([Decl],[Stm])
-data State = State {variables::Variables,functions::(Map Ident Function)}
+data State = State {variables::Variables,functions::(Map Ident Function),ret::Value}
-inList :: Eq a => a -> [a] -> Bool
-inList _ [] = False
-inList a (x:xs) = if a == x then True else inList a xs
+type EvalM = ErrorT String (StateT State IO)
+
+interpret :: [Func] -> [Stm] -> IO ()
+interpret fun st = do
+ runStateT (runErrorT (do mapM Interpret.addFunction fun; mapM_ execute st)) emptyState
+ return ()
--eval :: (MonadState Variables m) => Exp -> m Value
-eval :: Exp -> StateT State IO Value
+eval :: Exp -> EvalM Value
eval (EBool b) = return (VBool b)
eval (EInt n) = return (VInt n)
eval (EVar i) = getVariableValue i
eval (BiOpExp e o e') = do
v <- eval e
v'<- eval e'
- if inList o [Eq,NEq] then return $ opE o v v'
+ if elem o [Eq,NEq] then return $ opE o v v'
else let (VInt n1) = v in let (VInt n2) = v' in return $ op o n1 n2
eval (EPost i o) = do
(VInt n) <- getVariableValue i
(VBool b) <- eval e
return $ VBool $ not b
eval EReadI = do
- s <- lift $ getWord
+ s <- liftIO $ getWord
return $ VInt $ read s
eval EReadB = do
- s <- lift $ getWord
+ s <- liftIO $ getWord
return $ VBool $ if (read s == 0) then False else True
+eval (EFunc i as) = do
+ vs <- mapM eval as
+ state <- get
+ (ds,ss) <- lookup i $ functions state
+ let m = foldr (\((Decl t i),v) m -> insert i v m) empty $ zip ds vs
+ in modify (\s -> s{variables=[m]})
+ mapM_ execute ss `catchError` (\_ -> return ())
+ s <- get
+ put state
+ return $ ret s
getWord :: IO String
getWord = do
c <- getChar
- if inList c [' ', '\n', '\t', '\r']
+ if elem c [' ', '\n', '\t', '\r']
then return ""
else do
l <- getWord
findVariable i [] = fail $ "Variable "++show i++" not found in any scope."
findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms
---setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value
---setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value
-
-setVariableValue :: Ident -> Exp -> StateT State IO Value
+setVariableValue :: Ident -> Exp -> EvalM Value
setVariableValue i e = do
v <- eval e
state <- get
modify (\s -> s { variables = tail $ variables s})
-- execute :: (MonadState Variables m) => Stm -> m ()
-execute :: Stm -> StateT State IO ()
+execute :: Stm -> EvalM ()
execute (SNoop) = return ()
execute (SExp e) = eval e >> return ()
execute (SIf b s s') = do
pushAndPop $ if b' then execute s else execute s'
execute (SPrint e) = do
e' <- eval e
- lift $ print e'
+ liftIO $ print e'
execute (SBlock ss) = pushAndPop $ mapM execute ss
execute (SWhile e s) = do
(VBool b) <- eval e
v <- eval e
state <- get
let (m:ms) = variables state in modify (\s -> s{variables=insert i v m:ms })
+execute (SReturn e) = do
+ v <- eval e
+ modify (\s -> s{ret=v})
+ throwError "Returning.."
addFunction :: (MonadState State m) => Func -> m ()
addFunction (Func _ i d ss) = modify (\s -> s{functions=insert i (d,ss) (functions s) })