X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=Interpret.hs;h=fd04dcd722d9e33d8b65a70c292c96bd161e48e4;hp=5a792b9b2bbbe27be026d99096d17df1ef5cffa2;hb=d553df8dfdffca78342d6fae142ceded9cd64415;hpb=565fbd61dca527c23888e08783d0d91cee458524 diff --git a/Interpret.hs b/Interpret.hs index 5a792b9..fd04dcd 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,18 @@ 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 (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