Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
fix division by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Dec 17, 2023
1 parent 99c2a7a commit 442eb03
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/Computing/ComputeAST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions src/Computing/Operators/Calculate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

module Computing.Operators.Calculate
(
addition,
subtraction,
multiplication,
division,
modulo,
addition,
subtraction,
multiplication,
division,
modulo,
) where

import Types
Expand All @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 442eb03

Please sign in to comment.