diff --git a/math_parser/__init__.py b/math_parser/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/compute.py b/math_parser/compute.py similarity index 82% rename from compute.py rename to math_parser/compute.py index 57e95b5..1e3dca9 100644 --- a/compute.py +++ b/math_parser/compute.py @@ -1,13 +1,14 @@ import sys import operator -import parser +from math_parser 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/graphviz.py b/math_parser/graphviz.py similarity index 100% rename from graphviz.py rename to math_parser/graphviz.py diff --git a/parser.py b/math_parser/parser.py similarity index 94% rename from parser.py rename to math_parser/parser.py index c1d3cc4..4c40509 100644 --- a/parser.py +++ b/math_parser/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/test.py b/math_parser/test.py similarity index 82% rename from test.py rename to math_parser/test.py index f45f9df..25538e1 100644 --- a/test.py +++ b/math_parser/test.py @@ -1,5 +1,4 @@ -import compute -import parser +from math_parser import compute, parser 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) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..cde51de --- /dev/null +++ b/setup.py @@ -0,0 +1,14 @@ +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", + package_dir={"math_parser": ""}, +)