diff options
Diffstat (limited to 'snug-clean/src/Snug/Compile/Simulate.icl')
-rw-r--r-- | snug-clean/src/Snug/Compile/Simulate.icl | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/snug-clean/src/Snug/Compile/Simulate.icl b/snug-clean/src/Snug/Compile/Simulate.icl index 672b47e..af72277 100644 --- a/snug-clean/src/Snug/Compile/Simulate.icl +++ b/snug-clean/src/Snug/Compile/Simulate.icl @@ -3,8 +3,10 @@ implementation module Snug.Compile.Simulate import StdEnv import Control.Monad +import Control.Monad.Fail import Control.Monad.Identity import Control.Monad.State +import Data.Error import Data.Functor import MIPS.MIPS32 @@ -17,11 +19,13 @@ import Snug.Syntax , stack :: ![StackValue] } -simulate :: ![StackValue] !(Simulator a) -> [Instruction] -simulate stack sim = flatten (reverse (execState sim initial).instrs) +simulate :: ![StackValue] !(Simulator a) -> MaybeError String [Instruction] +simulate stack sim = + execStateT sim initial >>= \state -> + if (length state.stack == length stack) + (pure (flatten (reverse state.instrs))) + (fail "stack size changed") where - // TODO: when finishing: - // - check that the stack is empty initial = { instrs = [] , hp_offset = 0 @@ -113,7 +117,7 @@ pushArg i j = SVRegOffset reg offset -> push (SVIndirect (offset + (j+1)*4) reg) _ -> - abort "unexpected reference in pushArg\n" + fail "unexpected reference in pushArg\n" indirectAndEval :: Simulator () indirectAndEval = @@ -136,4 +140,4 @@ indirectAndEval = [ AddImmediate Signed HeapPtr HeapPtr (Immediate hp_offset) ] _ -> - abort "unexpected top of stack in indirect\n" + fail "unexpected top of stack in indirect\n" |