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)
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
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
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 ()
[SReturn]
-<return e,c> <= <e,c> => <v,c'>, c'[ret->v], STOP
+<return e,c> => c'[ret->v] <= <e,c> => <v,c'>
Evaluate the expression, add the value as return value to the state and stop the execution of the state
<fs ss, c> => c'' <= c[fs] => c', <ss,c'> => c''
Add all the function to the context and execute the statements in this context
+
+[SEQ]
+
+<s1;s2,c> => c' <= <s1,c> => c', c'(ret)
+
+If the context returned by s1 contains a return-value, return this context without executing the next statement.
+
+[SWhile]
+
+<while e s,c> => c''' => <e,c> => <true,c'> push(c') => c'' <s,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.