Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added __init__.py
Empty file.
Empty file.
6 changes: 3 additions & 3 deletions compute.py → arithmetic_expressions/compute.py
Original file line number Diff line number Diff line change
@@ -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
}


Expand Down
7 changes: 5 additions & 2 deletions parser.py → arithmetic_expressions/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TokenType(enum.Enum):
T_LPAR = 5
T_RPAR = 6
T_END = 7
T_MODULO = 8


class Node:
Expand All @@ -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:
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion graphviz.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import parser
from arithmetic_expressions import parser
import sys

node_counter = 1
Expand Down
13 changes: 13 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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",
)
Empty file added tests/__init__.py
Empty file.
8 changes: 6 additions & 2 deletions test.py → tests/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import compute
import parser
from arithmetic_expressions import parser, compute


def test_computation(inputstring, expected_output):
Expand All @@ -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)
Expand Down