X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=Interpret.hs;fp=Interpret.hs;h=948903ebd9416e6de394f62cc4c27772eacd96b0;hp=5afcefb10b28cd6ac4a390e71958499b5cfdc956;hb=e4867d1f76874882921ec9501d20834a4d96eb5a;hpb=f6be9dc6266331e920134c67072fde3d5a2cfdf2 diff --git a/Interpret.hs b/Interpret.hs index 5afcefb..948903e 100644 --- a/Interpret.hs +++ b/Interpret.hs @@ -7,7 +7,7 @@ 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 @@ -19,16 +19,17 @@ instance Show Value where type Variables = [Map Ident Value] type Function = ([Decl],[Stm]) -data State = State {variables::Variables,functions::(Map Ident Function),ret::(MVar Value)} +data State = State {variables::Variables,functions::(Map Ident Function),ret::Value} + +type EvalM = ErrorT String (StateT State IO) interpret :: [Func] -> [Stm] -> IO () interpret fun st = do - mv <- newEmptyMVar - runStateT (do mapM Interpret.addFunction fun; mapM execute st) emptyState{ret=mv} + 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 @@ -49,10 +50,10 @@ eval (ENot e) = do (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 @@ -61,9 +62,9 @@ eval (EFunc i as) = do 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 ()) - v <- lift $ takeMVar $ ret state + s <- get put state - return v + return $ ret s getWord :: IO String getWord = do @@ -95,10 +96,7 @@ findVariable :: (MonadState State m) => Ident -> Variables -> m Value 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 @@ -116,7 +114,7 @@ pushAndPop s = do 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 @@ -124,7 +122,7 @@ 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 @@ -138,9 +136,8 @@ execute (SDecl t i e) =do let (m:ms) = variables state in modify (\s -> s{variables=insert i v m:ms }) execute (SReturn e) = do v <- eval e - s <- get - lift $ putMVar (ret s) v - throwError $ userError "Returning.." + 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) })