diff --git a/README.md b/README.md index 4b30a8d..c050ddc 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ It exists solely for educational reasons. ## How to Use it ``` -python3 compute.py '2*(3+4)' +python3 arithmetic_expressions/compute.py '2*(3+4)' ``` and you should receive `14` as the result. This is perhaps not so surprising, but you can also run diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arithmetic_expressions/__init__.py b/arithmetic_expressions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/compute.py b/arithmetic_expressions/compute.py similarity index 80% rename from compute.py rename to arithmetic_expressions/compute.py index 57e95b5..fb16717 100644 --- a/compute.py +++ b/arithmetic_expressions/compute.py @@ -1,13 +1,13 @@ import sys import operator -import parser - +from arithmetic_expressions import parser operations = { parser.TokenType.T_PLUS: operator.add, parser.TokenType.T_MINUS: operator.sub, parser.TokenType.T_MULT: operator.mul, - parser.TokenType.T_DIV: operator.truediv + parser.TokenType.T_DIV: operator.truediv, + parser.TokenType.T_MODULO: operator.mod } diff --git a/parser.py b/arithmetic_expressions/parser.py similarity index 94% rename from parser.py rename to arithmetic_expressions/parser.py index c1d3cc4..4c40509 100644 --- a/parser.py +++ b/arithmetic_expressions/parser.py @@ -11,6 +11,7 @@ class TokenType(enum.Enum): T_LPAR = 5 T_RPAR = 6 T_END = 7 + T_MODULO = 8 class Node: @@ -27,7 +28,9 @@ def lexical_analysis(s): '*': TokenType.T_MULT, '/': TokenType.T_DIV, '(': TokenType.T_LPAR, - ')': TokenType.T_RPAR} + ')': TokenType.T_RPAR, + '%': TokenType.T_MODULO + } tokens = [] for c in s: @@ -65,7 +68,7 @@ def parse_e(tokens): def parse_e2(tokens): left_node = parse_e3(tokens) - while tokens[0].token_type in [TokenType.T_MULT, TokenType.T_DIV]: + while tokens[0].token_type in [TokenType.T_MULT, TokenType.T_DIV, TokenType.T_MODULO]: node = tokens.pop(0) node.children.append(left_node) node.children.append(parse_e3(tokens)) diff --git a/graphviz.py b/graphviz.py index a8bb018..3cf59a1 100644 --- a/graphviz.py +++ b/graphviz.py @@ -1,4 +1,4 @@ -import parser +from arithmetic_expressions import parser import sys node_counter = 1 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..c5ced58 --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +import setuptools + +with open("README.md", "r") as fh: + long_description = fh.read() + +setuptools.setup( + name="parser", + version="0.0.1", + long_description=long_description, + long_description_content_type="text/markdown", + packages=setuptools.find_packages(where="."), + python_requires=">=3.7", +) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test.py b/tests/test.py similarity index 81% rename from test.py rename to tests/test.py index f45f9df..1c20a8f 100644 --- a/test.py +++ b/tests/test.py @@ -1,5 +1,4 @@ -import compute -import parser +from arithmetic_expressions import parser, compute def test_computation(inputstring, expected_output): @@ -24,6 +23,11 @@ def test_computation(inputstring, expected_output): test_computation('2-(3*4+1)', -11) test_computation('2*(3*4+1)', 26) test_computation('8/((1+3)*2)', 1) +test_computation('2%3', 2) +test_computation('2%3+1', 3) +test_computation('1+2%3', 3) +test_computation('(1+2)%3', 0) +test_computation('(1+2)%3', 0) try: test_computation('1+1)', 1)