From af54d09b9e5eee36ebd1127f40b3b2b6deb0ab6a Mon Sep 17 00:00:00 2001 From: Hudson Worden Date: Tue, 11 Jul 2023 20:33:37 -0400 Subject: [PATCH 1/3] Added support for modulo operator. --- compute.py | 3 ++- parser.py | 7 +++++-- test.py | 5 +++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/compute.py b/compute.py index 57e95b5..c502817 100644 --- a/compute.py +++ b/compute.py @@ -7,7 +7,8 @@ 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/parser.py index c1d3cc4..4c40509 100644 --- a/parser.py +++ b/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/test.py index f45f9df..c91c62d 100644 --- a/test.py +++ b/test.py @@ -24,6 +24,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) From 221d6464fae62f843e9fe2b6a19004ba11d23931 Mon Sep 17 00:00:00 2001 From: Hudson Worden Date: Tue, 11 Jul 2023 20:44:05 -0400 Subject: [PATCH 2/3] Added setup.py file. --- __init__.py | 0 setup.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 __init__.py create mode 100644 setup.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 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": ""}, +) From e5801d1d696b8edcf63bfc763528852cf60ff1e5 Mon Sep 17 00:00:00 2001 From: Hudson Worden Date: Wed, 12 Jul 2023 11:13:18 -0400 Subject: [PATCH 3/3] Moved stuff around. --- README.md | 2 +- arithmetic_expressions/__init__.py | 0 compute.py => arithmetic_expressions/compute.py | 3 +-- parser.py => arithmetic_expressions/parser.py | 0 graphviz.py | 2 +- setup.py | 1 - tests/__init__.py | 0 test.py => tests/test.py | 3 +-- 8 files changed, 4 insertions(+), 7 deletions(-) create mode 100644 arithmetic_expressions/__init__.py rename compute.py => arithmetic_expressions/compute.py (93%) rename parser.py => arithmetic_expressions/parser.py (100%) create mode 100644 tests/__init__.py rename test.py => tests/test.py (95%) 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/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 93% rename from compute.py rename to arithmetic_expressions/compute.py index c502817..fb16717 100644 --- a/compute.py +++ b/arithmetic_expressions/compute.py @@ -1,7 +1,6 @@ import sys import operator -import parser - +from arithmetic_expressions import parser operations = { parser.TokenType.T_PLUS: operator.add, diff --git a/parser.py b/arithmetic_expressions/parser.py similarity index 100% rename from parser.py rename to arithmetic_expressions/parser.py 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 index cde51de..c5ced58 100644 --- a/setup.py +++ b/setup.py @@ -10,5 +10,4 @@ long_description_content_type="text/markdown", packages=setuptools.find_packages(where="."), python_requires=">=3.7", - package_dir={"math_parser": ""}, ) 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 95% rename from test.py rename to tests/test.py index c91c62d..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):