X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Interpret.hs;h=5afcefb10b28cd6ac4a390e71958499b5cfdc956;hb=04f0a9566794cf761b7bcf83190051a400ec3653;hp=8cf3c1b2657270d016c8f28256b993adbd566c96;hpb=3e277a6a62728085cd8ba735b50bdac0b6d68808;p=proglang.git diff --git a/Interpret.hs b/Interpret.hs index 8cf3c1b..5afcefb 100644 --- a/Interpret.hs +++ b/Interpret.hs @@ -1,10 +1,14 @@ -module Interpret (eval, execute, Value(VInt, VBool)) where +module Interpret (interpret, eval, execute,addFunction, emptyState, Value(..), State(..)) where import Abssyntax -import Control.Monad.State +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)} + data Value = VInt Integer | VBool Bool deriving Eq instance Show Value where @@ -13,13 +17,18 @@ instance Show Value where show (VBool False) = "0" type Variables = [Map Ident Value] +type Function = ([Decl],[Stm]) + +data State = State {variables::Variables,functions::(Map Ident Function),ret::(MVar Value)} -inList :: Eq a => a -> [a] -> Bool -inList _ [] = False -inList a (x:xs) = if a == x then True else inList a xs +interpret :: [Func] -> [Stm] -> IO () +interpret fun st = do + mv <- newEmptyMVar + runStateT (do mapM Interpret.addFunction fun; mapM execute st) emptyState{ret=mv} + return () --eval :: (MonadState Variables m) => Exp -> m Value -eval :: Exp -> StateT Variables IO Value +eval :: Exp -> StateT State IO Value eval (EBool b) = return (VBool b) eval (EInt n) = return (VInt n) eval (EVar i) = getVariableValue i @@ -27,7 +36,7 @@ eval (EAss i e) = setVariableValue i e 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 @@ -45,11 +54,21 @@ eval EReadI = do eval EReadB = do s <- lift $ 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 ()) + v <- lift $ takeMVar $ ret state + put state + return v 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 @@ -67,37 +86,37 @@ op ELt = \e e' -> VBool $ e <= e' op Gt = \e e' -> VBool $ e > e' op EGt = \e e' -> VBool $ e >= e' -getVariableValue :: (MonadState Variables m) => Ident -> m Value +getVariableValue :: (MonadState State m) => Ident -> m Value getVariableValue i = do - ms <- get - findVariable i ms + s <- get + findVariable i $ variables s -findVariable :: (MonadState Variables m) => Ident -> Variables -> m Value +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 Variables IO Value +setVariableValue :: Ident -> Exp -> StateT State IO Value setVariableValue i e = do v <- eval e - ms <- get - put $ updateVariable i v ms + state <- get + modify (\s -> s{variables= updateVariable i v $ variables state} ) return v updateVariable :: Ident -> Value -> Variables -> Variables updateVariable _ _ [] = [] updateVariable i v (m:ms) = if member i m then insert i v m:ms else m:updateVariable i v ms -pushAndPop :: (MonadState Variables m) => m a -> m () +pushAndPop :: (MonadState State m) => m a -> m () pushAndPop s = do - modify (empty:) + modify (\s -> s { variables = empty:variables s}) s - modify tail + modify (\s -> s { variables = tail $ variables s}) -- execute :: (MonadState Variables m) => Stm -> m () -execute :: Stm -> StateT Variables IO () +execute :: Stm -> StateT State IO () execute (SNoop) = return () execute (SExp e) = eval e >> return () execute (SIf b s s') = do @@ -115,5 +134,13 @@ execute (SDeclD t i) = execute $ SDecl t i $ case t of TBool -> EBool False execute (SDecl t i e) =do v <- eval e - (m:ms) <- get - put $ (insert i v m):ms + state <- get + 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.." + +addFunction :: (MonadState State m) => Func -> m () +addFunction (Func _ i d ss) = modify (\s -> s{functions=insert i (d,ss) (functions s) })