]> ruin.nu Git - proglang.git/blobdiff - Interpret.hs
replace E with in
[proglang.git] / Interpret.hs
index 9e4dcff34541b1f180b73b3c12dc15cbcfb3e642..fd04dcd722d9e33d8b65a70c292c96bd161e48e4 100644 (file)
@@ -23,7 +23,6 @@ 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 = error "EDefault called from an illegal place"
 eval (BiOpExp e o e') = do
        v <- eval e
        v'<- eval e'
@@ -78,11 +77,6 @@ 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 
-addVariable :: Ident -> Exp -> StateT Variables IO ()
-addVariable i e = do
-       e' <- eval e
-       (m:ms) <- get
-       put $ (insert i e' m):ms
 
 setVariableValue :: Ident -> Exp -> StateT Variables IO Value
 setVariableValue i e = do
@@ -95,6 +89,11 @@ 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 s = do
+       modify (empty:)
+       s
+       modify tail
 
 -- execute :: (MonadState Variables m) => Stm -> m ()
 execute :: Stm -> StateT Variables IO ()
@@ -102,20 +101,19 @@ 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 (SBlock ss) = do
-       modify (empty:)
-       mapM execute ss
-       modify tail
+execute (SBlock ss) = pushAndPop $ mapM execute ss
 execute (SWhile e s) = do
        (VBool b) <- eval e
-       if b then execute s >> execute (SWhile e s) else return ()
-execute (SDecl t i EDefault) = do
-       case t of
-               TInt -> addVariable i (EInt 0)
-               TBool -> addVariable i (EBool False)
-       return ()
-execute (SDecl t i e) = addVariable 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
+       e' <- eval e
+       (m:ms) <- get
+       put $ (insert i e' m):ms