From 89a4c9c8d8cb07f4fb749316fa20534708073b62 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Wed, 15 Mar 2006 16:20:05 +0000 Subject: [PATCH] better semantics and show div by zero errors earlier --- Interpret.hs | 25 ++++++++++++------------- documentation | 14 +++++++++++++- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Interpret.hs b/Interpret.hs index 652e979..a674d1e 100644 --- a/Interpret.hs +++ b/Interpret.hs @@ -28,7 +28,6 @@ interpret fun st = do runStateT (runErrorT (do mapM addFunction fun; mapM_ execute st)) emptyState return () ---eval :: (MonadState State m, MonadError String m, MonadIO m) => Exp -> m Value eval :: (MonadState State m, MonadIO m) => Exp -> EvalM m Value eval (EBool b) = return (VBool b) eval (EInt n) = return (VInt n) @@ -40,7 +39,7 @@ eval (BiOpExp e o e') = do v <- eval e v'<- eval e' 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 + 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 setVariableValue i $ op o n 1 @@ -89,16 +88,17 @@ getNumber2 = do else return "" -- op :: Op -> (a -> a -> Value) -opE Eq = \e e' -> VBool $ e == e' -opE NEq = \e e' -> VBool $ not (e == e') -op Plus = \e e' -> VInt $ e + e' -op Minus = \e e' -> VInt $ e - e' -op Times = \e e' -> VInt $ e * e' -op Div = \e e' -> VInt $ e `div` e' -op Lt = \e e' -> VBool $ e < e' -op ELt = \e e' -> VBool $ e <= e' -op Gt = \e e' -> VBool $ e > e' -op EGt = \e e' -> VBool $ e >= e' +opE Eq e e' = VBool $ e == e' +opE NEq e e' = VBool $ not (e == e') +op Plus e e' =VInt $ e + e' +op Minus e e' = VInt $ e - e' +op Times e e' = VInt $ e * e' +op Div _ 0 = error "Division by zero" +op Div e e' = VInt $ e `div` e' +op Lt e e' = VBool $ e < e' +op ELt e e' = VBool $ e <= e' +op Gt e e' = VBool $ e > e' +op EGt e e' = VBool $ e >= e' getVariableValue :: (MonadState State m) => Ident -> m Value getVariableValue i = do @@ -124,7 +124,6 @@ pushAndPop s = do s modify (\s -> s { variables = tail $ variables s}) --- execute :: (MonadState Variables m) => Stm -> m () execute :: (MonadState State m, MonadIO m) => Stm -> EvalM m () execute (SNoop) = return () execute (SExp e) = eval e >> return () diff --git a/documentation b/documentation index 9127c08..196220a 100644 --- a/documentation +++ b/documentation @@ -72,7 +72,7 @@ Evaluate the arguments in order, find function definition, remove the old variab [SReturn] - <= => , c'[ret->v], STOP + => c'[ret->v] <= => Evaluate the expression, add the value as return value to the state and stop the execution of the state @@ -87,3 +87,15 @@ Adds the function i with parameters ds and body ss to the context => c'' <= c[fs] => c', => c'' Add all the function to the context and execute the statements in this context + +[SEQ] + + => c' <= => c', c'(ret) + +If the context returned by s1 contains a return-value, return this context without executing the next statement. + +[SWhile] + + => c''' => => push(c') => c'' => c''', c'''(ret) + +If the context returned by the body contains a return-value, return this context and don't try to run the loop again. -- 2.39.2