From 92fd10b102519d03741d61c2679c4cfbe1b486a4 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Thu, 2 Mar 2006 19:48:59 +0000 Subject: [PATCH] added pushAndPop function to push and pop the scope --- Interpret.hs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Interpret.hs b/Interpret.hs index 5a792b9..8ac65bf 100644 --- a/Interpret.hs +++ b/Interpret.hs @@ -89,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 () @@ -96,17 +101,14 @@ 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 () + if b then pushAndPop (execute s) >> execute (SWhile e s) else return () execute (SDecl t i e) =do e' <- eval e (m:ms) <- get -- 2.39.2