diff --git a/src/Computing/ComputeAST.hs b/src/Computing/ComputeAST.hs index 9f7459b..9b93c8a 100644 --- a/src/Computing/ComputeAST.hs +++ b/src/Computing/ComputeAST.hs @@ -94,7 +94,6 @@ computeFunctionBody env (Function _ fnParams (x:_)) args = case replaceFunctionParams env fnParams x args of (newEnv, Nothing) -> (newEnv, Right (undefined)) (newEnv, Just replaced) -> computeAST newEnv replaced - computeFunction :: Env -> Function -> [Tree] -> (Env, Result) computeFunction env (Function fnName fnParams (x:xs:rest)) args = case computeFunctionBody env (Function fnName fnParams [x]) args of diff --git a/src/Computing/Operators/Calculate.hs b/src/Computing/Operators/Calculate.hs index d344e94..c6db955 100644 --- a/src/Computing/Operators/Calculate.hs +++ b/src/Computing/Operators/Calculate.hs @@ -7,11 +7,11 @@ module Computing.Operators.Calculate ( - addition, - subtraction, - multiplication, - division, - modulo, + addition, + subtraction, + multiplication, + division, + modulo, ) where import Types @@ -30,20 +30,24 @@ calculate a b "mod" = Number (a `mod` b) calculate _ _ _ = Number 0 maybeCalculate :: Maybe Tree -> Maybe Tree -> Symbol -> Env -> (Env, Result) +maybeCalculate _ (Just (Number 0)) "div" env + = (registerError env "Division by 0", Right (undefined)) maybeCalculate (Just (Number a)) (Just (Number b)) operator env = (env, Left (Just (calculate a b operator))) maybeCalculate _ _ _ env = (registerError env "Symbol not found", Right (undefined)) calculateOperator :: Env -> [Tree] -> Symbol -> (Env, Result) +calculateOperator env [_, Number 0] "div" = + (registerError env "Division by 0", Right (undefined)) calculateOperator env [Number a, Number b] operator = - (env, Left (Just (calculate a b operator))) + (env, Left (Just (calculate a b operator))) calculateOperator env [Number a, Symbol b] operator = - maybeCalculate (Just (Number a)) (evaluateSymbol env b) operator env + maybeCalculate (Just (Number a)) (evaluateSymbol env b) operator env calculateOperator env [Symbol a, Number b] operator = - maybeCalculate (evaluateSymbol env a) (Just (Number b)) operator env + maybeCalculate (evaluateSymbol env a) (Just (Number b)) operator env calculateOperator env [Symbol a, Symbol b] operator = - maybeCalculate (evaluateSymbol env a) (evaluateSymbol env b) operator env + maybeCalculate (evaluateSymbol env a) (evaluateSymbol env b) operator env calculateOperator env list _ | length list /= 2 = (registerError env "Addition need 2 params", Right (undefined)) diff --git a/test/Spec.hs b/test/Spec.hs index dda8eb8..bfd66e7 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -208,6 +208,10 @@ unitTestsComputeBasics = testGroup "Tests compute basics" assertEqual "2 + 3 * (8 + (5* ( 2 + 3))) = 107" (defaultEnv, [Left (Just (Number 101))]) (computeAllAST (defaultEnv) [(List [Symbol "+", Number 2, (List [Symbol "*", Number 3, (List [Symbol "+", Number 8, (List [Symbol "*", Number 5, (List [Symbol "+", Number 2, Number 3])])])])])]) + , testCase "div 42 0" $ + assertEqual "div 42 0" + (Env {defines = [], errors = ["Division by 0"], functions = []}, Right (undefined)) + (computeAST (defaultEnv) (List [Symbol "div", Number 42, Number 0])) ] unitTestsComputeFunctions :: TestTree