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
Empty file added math_parser/__init__.py
Empty file.
5 changes: 3 additions & 2 deletions compute.py → math_parser/compute.py
Original file line number Diff line number Diff line change
@@ -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
}


Expand Down
File renamed without changes.
7 changes: 5 additions & 2 deletions parser.py → math_parser/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
8 changes: 6 additions & 2 deletions test.py → math_parser/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import compute
import parser
from math_parser import compute, parser


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
14 changes: 14 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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": ""},
)