X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=Interpret.hs;h=62cc7fd9d5f839978692359779b16ecc9c465e92;hp=f1c820682806950a7912115488eb40be0f5fa4fc;hb=HEAD;hpb=2316d61f350f8b39c9a3f76b91d1d14796d5183b diff --git a/Interpret.hs b/Interpret.hs index f1c8206..62cc7fd 100644 --- a/Interpret.hs +++ b/Interpret.hs @@ -1,83 +1,153 @@ -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), ret=(VInt 0)} + data Value = VInt Integer | VBool Bool deriving Eq instance Show Value where show (VInt n) = show n - show (VBool b) = show b + show (VBool True) = "1" + show (VBool False) = "0" type Variables = [Map Ident Value] +type Function = ([Decl],[Stm]) + +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 m = ErrorT String m ---eval :: (MonadState Variables m) => Exp -> m Value -eval :: Exp -> StateT Variables IO Value +interpret :: [Func] -> [Stm] -> IO () +interpret fun st = do + runStateT (runErrorT (do mapM addFunction fun; mapM_ execute st)) emptyState + return () + +eval :: (MonadState State m, MonadIO m) => Exp -> EvalM m Value eval (EBool b) = return (VBool b) eval (EInt n) = return (VInt n) eval (EVar i) = getVariableValue i -eval (EAss i e) = setVariableValue i e -eval EDefault = return (VInt 0) --FIXME!!! +eval (EAss i e) = do + v <- eval e + setVariableValue i v eval (BiOpExp e o e') = do v <- eval e v'<- eval e' - if inList 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 -typeCheckExp (EPost i op) = do + 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 - setVariableValue i $ EInt $ n+1 + setVariableValue i $ op o n 1 return $ VInt n +eval (ENeg e) = do + (VInt n) <- eval e + return $ VInt $ -n +eval (ENot e) = do + (VBool b) <- eval e + return $ VBool $ not b +eval EReadI = do + s <- liftIO $ getNumber + return $ VInt $ read s +eval EReadB = do + s <- liftIO $ getNumber + 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 >> (fail $ "Function "++show i++" didn't return anything.")) + `catchError` (\_ ->return ()) --Only errors thrown in ErrorT can be caught here, runtime errors pass through, so no need to check the error + v <- liftM ret get + put state + return v + +--Stricter evaluation of the input +getNumber :: IO String +getNumber = do + c <- getChar + if elem c $ '-':['0'..'9'] + then do + l <- getNumber2 + return (c:l) + else fail "Non integer input." + +getNumber2 :: IO String +getNumber2 = do + c <- getChar + if elem c ['0'..'9'] + then do + l <- getNumber2 + return (c:l) + else return "" -- op :: Op -> (a -> a -> Value) -opE Eq = \e e' -> VBool $ e == e' -opE NEq = \e e' -> VBool $ not (e == e') -op Plus = \e e' -> VInt $ e + e' -op Minus = \e e' -> VInt $ e - e' -op Times = \e e' -> VInt $ e * e' -op Div = \e e' -> VInt $ e `div` e' - -getVariableValue :: (MonadState Variables m) => Ident -> m Value -getVariableValue i = do - ms <- get - findVariable i ms +opE Eq e e' = VBool $ e == e' +opE NEq e e' = VBool $ not (e == e') +op Plus e e' =VInt $ e + e' +op Minus e e' = VInt $ e - e' +op Times e e' = VInt $ e * e' +op Div _ 0 = error "Division by zero" +op Div e e' = VInt $ e `div` e' +op Lt e e' = VBool $ e < e' +op ELt e e' = VBool $ e <= e' +op Gt e e' = VBool $ e > e' +op EGt e e' = VBool $ e >= e' ---setVariableValue :: (MonadState Variables m) => Ident -> Exp -> m Value -setVariableValue :: Ident -> Exp -> StateT Variables IO Value -setVariableValue i e = do - e' <- eval e - (m:ms) <- get - put $ (insert i e' m):ms - return e' +getVariableValue :: (MonadState State m) => Ident -> m Value +getVariableValue i = do + s <- get + findVariable i $ variables s -findVariable :: (MonadState Variables m) => Ident -> Variables -> m Value +findVariable :: (Monad 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 --- execute :: (MonadState Variables m) => Stm -> m () -execute :: Stm -> StateT Variables IO () +setVariableValue :: (MonadState State m) => Ident -> Value -> EvalM m Value +setVariableValue i v = do + modify (\s -> s{variables= updateVariable i v $ variables s} ) + 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 State m) => m a -> m () +pushAndPop s = do + modify (\s -> s { variables = empty:variables s}) + s + modify (\s -> s { variables = tail $ variables s}) + +execute :: (MonadState State m, MonadIO m) => Stm -> EvalM m () +execute (SNoop) = return () +execute (SExp e) = eval e >> return () execute (SIf b s s') = do (VBool b') <- eval b - if b' then execute s else execute s' + pushAndPop $ if b' then execute s else execute s' execute (SPrint e) = do e' <- eval e - lift $ print e' -execute (SNoop) = return () -execute (SBlock ss) = do - modify (empty:) - mapM execute ss - modify tail + liftIO $ print e' +execute (SBlock ss) = pushAndPop $ mapM execute ss execute (SWhile e s) = do (VBool b) <- eval e - if b then execute (SWhile e s) else return () -execute (SDecl t i EDefault) = do - case t of - TInt -> setVariableValue i (EInt 0) - TBool -> setVariableValue i (EBool False) - return () -execute (SDecl t i e) = setVariableValue i e >> return () + if b then pushAndPop (execute s) >> execute (SWhile e s) else return () +execute (SDeclD t i) = execute $ SDecl t i $ case t of + TInt -> EInt 0 + TBool -> EBool False +execute (SDecl t i e) =do + v <- eval e + (m:ms) <- liftM variables get + 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) })