Skip to content

Commit

Permalink
Added possibility to have digits in function names
Browse files Browse the repository at this point in the history
  • Loading branch information
charon25 committed Mar 13, 2023
1 parent b4f6045 commit 43488e3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="shunting-yard",
version="1.0.5",
version="1.0.6",
author="Paul 'charon25' Kern",
description="Compute any math expression",
long_description=long_description,
Expand All @@ -14,5 +14,5 @@
url="https://www.github.com/charon25/ShuntingYard",
license="MIT",
packages=['shunting_yard'],
download_url="https://github.com/charon25/ShuntingYard/archive/refs/tags/v1.0.5.tar.gz"
download_url="https://github.com/charon25/ShuntingYard/archive/refs/tags/v1.0.6.tar.gz"
)
2 changes: 1 addition & 1 deletion shunting_yard/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

if len(sys.argv) <= 1:
print('Expression to evaluate ?')
expression = input('>>>')
expression = input('>>> ')
else:
expression = sys.argv[1]

Expand Down
8 changes: 5 additions & 3 deletions shunting_yard/constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from string import ascii_lowercase
from string import ascii_lowercase, digits


UNARY_OPERATORS = '+-'
UNARY_OPERATORS_SYMBOLS = ('-u', '+u')
BASE_OPERATORS = '+-*/^'
NUMBER_CHARS = '0123456789.'
FUNCTION_CHARS = ascii_lowercase + '_'
NUMBER_CHARS = digits + '.'
# functions cannot start with a number
FUNCTION_FIRST_CHARS = ascii_lowercase + '_'
FUNCTION_CHARS = FUNCTION_FIRST_CHARS + NUMBER_CHARS
4 changes: 4 additions & 0 deletions shunting_yard/rpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class WrongExpressionError(Exception):
'cos': (1, math.cos),
'tan': (1, math.tan),
'min': (2, min),
'min3': (3, min),
'min4': (4, min),
'max': (2, max),
'max3': (3, max),
'max4': (4, max),
'abs': (1, abs)
}

Expand Down
6 changes: 3 additions & 3 deletions shunting_yard/tokenize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Iterator

from shunting_yard.constants import BASE_OPERATORS, FUNCTION_CHARS, NUMBER_CHARS, UNARY_OPERATORS
from shunting_yard.constants import BASE_OPERATORS, FUNCTION_CHARS, FUNCTION_FIRST_CHARS, NUMBER_CHARS, UNARY_OPERATORS


def tokenize(string: str) -> Iterator[str]:
Expand Down Expand Up @@ -35,8 +35,8 @@ def tokenize(string: str) -> Iterator[str]:
cursor += (cursor_end - cursor)
is_infix = True

elif char in FUNCTION_CHARS:
# Go through until not a number anymore
elif char in FUNCTION_FIRST_CHARS:
# Go through until not a function anymore
cursor_end = cursor + 1
while cursor_end < len(string) and string[cursor_end] in FUNCTION_CHARS:
cursor_end += 1
Expand Down
3 changes: 3 additions & 0 deletions tests/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def test_function_unary_in_function(self):
self.assertListEqual(list(tokenize('min(1, -2)')), ['min', '(', '1', '-u', '2', ')'])
self.assertListEqual(list(tokenize('min(1, -(2+sin(3)))')), ['min', '(', '1', '-u', '(', '2', '+', 'sin', '(', '3', ')', ')', ')'])

def test_digits_in_function_name(self):
self.assertListEqual(list(tokenize('min3(1, 2)')), ['min3', '(', '1', '2', ')'])



if __name__ == '__main__':
Expand Down

0 comments on commit 43488e3

Please sign in to comment.