Skip to content
Snippets Groups Projects
Commit 92af420e authored by Florentin Labelle's avatar Florentin Labelle
Browse files

integer calculator instead

parent 6f66eb2b
No related branches found
No related tags found
No related merge requests found
...@@ -28,8 +28,8 @@ class Calculator: ...@@ -28,8 +28,8 @@ class Calculator:
tokens.append(self.operators[token]) tokens.append(self.operators[token])
else: else:
try: try:
term = float(token) if int(token) == float(token):
tokens.append(term) tokens.append(int(token))
except ValueError as exc: except ValueError as exc:
raise ValueError(f"Invalid token {token}") from exc raise ValueError(f"Invalid token {token}") from exc
return tokens return tokens
......
...@@ -5,7 +5,7 @@ from typing import Union ...@@ -5,7 +5,7 @@ from typing import Union
from calculator.operators import Operator from calculator.operators import Operator
Term: type = float Term: type = int
Token: type = Union[Operator, Term] Token: type = Union[Operator, Term]
......
...@@ -25,12 +25,12 @@ def test_tokenizer(setup): ...@@ -25,12 +25,12 @@ def test_tokenizer(setup):
Test the tokenizer. Test the tokenizer.
""" """
plus, minus, times, divide, calc = setup plus, minus, times, divide, calc = setup
assert calc.tokenize("1 + 2") == [1.0, plus, 2.0] assert calc.tokenize("1 + 2") == [1, plus, 2]
assert calc.tokenize("1 + 2 * 3") == [1.0, plus, 2.0, times, 3.0] assert calc.tokenize("1 + 2 * 3") == [1, plus, 2, times, 3]
assert calc.tokenize( assert calc.tokenize(
"1 + 2 * 3 / 4") == [1.0, plus, 2.0, times, 3.0, divide, 4.0] "1 + 2 * 3 / 4") == [1, plus, 2, times, 3, divide, 4]
assert calc.tokenize( assert calc.tokenize(
"1 + 2 * 3 / 4 - 5") == [1.0, plus, 2.0, times, 3.0, divide, 4.0, minus, 5.0] "1 + 2 * 3 / 4 - 5") == [1, plus, 2, times, 3, divide, 4, minus, 5]
def test_parser(setup): def test_parser(setup):
...@@ -38,13 +38,13 @@ def test_parser(setup): ...@@ -38,13 +38,13 @@ def test_parser(setup):
Test the parser. Test the parser.
""" """
_, _, _, _, calc = setup _, _, _, _, calc = setup
assert repr(calc.parse(calc.tokenize("1 + 2"))) == '(1.0 + 2.0)' assert repr(calc.parse(calc.tokenize("1 + 2"))) == '(1 + 2)'
assert repr(calc.parse(calc.tokenize("1 + 2 * 3")) assert repr(calc.parse(calc.tokenize("1 + 2 * 3"))
) == '(1.0 + (2.0 * 3.0))' ) == '(1 + (2 * 3))'
assert repr(calc.parse(calc.tokenize( assert repr(calc.parse(calc.tokenize(
"1 + 2 * 3 / 4"))) == '(1.0 + ((2.0 * 3.0) / 4.0))' "1 + 2 * 3 / 4"))) == '(1 + ((2 * 3) / 4))'
assert repr(calc.parse(calc.tokenize( assert repr(calc.parse(calc.tokenize(
"1 + 2 * 3 / 4 - 5"))) == '((1.0 + ((2.0 * 3.0) / 4.0)) - 5.0)' "1 + 2 * 3 / 4 - 5"))) == '((1 + ((2 * 3) / 4)) - 5)'
def test_evaluation(setup): def test_evaluation(setup):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment