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)
type Variables = [Map Ident Value]
type Function = ([Decl],[Stm])
-data State = State {variables::Variables,functions::(Map Ident Function)}
-
-inList :: Eq a => a -> [a] -> Bool
-inList _ [] = False
-inList a (x:xs) = if a == x then True else inList a xs
+data State = State {variables::Variables,functions::(Map Ident Function),ret::(MVar Value)}
--eval :: (MonadState Variables m) => Exp -> m Value
eval :: Exp -> StateT State IO Value
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
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
+ modify (\s -> s{variables=[empty]})
+ addParams vs ds
+ mapM_ execute ss `catchError` (\_ -> return ())
+ put state
+ v <- lift $ takeMVar $ ret state
+ return v
+
+addParams :: [Value] -> [Decl] -> StateT State IO ()
+addParams [] [] = return ()
+addParams (v:vs) ((Decl t i):ds) = do
+ state <- get
+ let (m:ms) = variables state in modify (\s -> s{variables=insert i v m:ms })
+ addParams vs ds
+
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
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
+ 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) })
import Typecheck
import Interpret
import Control.Monad.State hiding (State)
+import Control.Concurrent.MVar
import Data.Map as Map hiding (showTree)
import ErrM
runStateT (do mapM Typecheck.addFunction fun; mapM typeCheckFunction fun; mapM typeCheckStm st) Typecheck.emptyState
print "The program is type-correct!!"
print "Running program:"
- runStateT (do mapM Interpret.addFunction fun; mapM execute st) Interpret.emptyState
+ mv <- newEmptyMVar
+ runStateT (do mapM Interpret.addFunction fun; mapM execute st) Interpret.emptyState{ret=mv}
print "Done running program!"
return ()
emptyState = State{variables=[empty], functions=(empty), function=(Ident "")}
-inList :: Eq a => a -> [a] -> Bool
-inList _ [] = False
-inList a (x:xs) = if a == x then True else inList a xs
-
assert :: Monad m => Bool -> String -> m ()
assert True _ = return ()
assert False s = fail s
t1 <- typeCheckExp e
t2 <- typeCheckExp e'
assert (t1 == t2) "The parameters for the binary operator aren't equal"
- if inList o [Eq,NEq] then return TBool
+ if elem o [Eq,NEq] then return TBool
else do
assert (t1 == TInt) "The parameters need to be of type int"
- if inList o [Plus,Minus,Times,Div]
+ if elem o [Plus,Minus,Times,Div]
then return TInt
else return TBool
typeCheckExp (EVar i) = typeCheckVar i