Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds register operation to Parser #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
52 changes: 31 additions & 21 deletions py_expression_eval/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,34 @@ class Expression(Expression):
CALL = 128
NULLARY_CALL = 256

ops = [('+', 2, '+'),
('-', 2, '-'),
('**', 6, '**'),
('*', 3, '*'),
(u'\u2219', 3, '*'), # bullet operator
(u'\u2022', 3, '*'), # black small circle
('/', 4, '/'),
('%', 4, '%'),
('^', 6, '^'),
('||', 1, '||'),
('==', 1, '=='),
('!=', 1, '!='),
('<=', 1, '<='),
('>=', 1, '>='),
('<', 1, '<'),
('>', 1, '>'),
('and ', 0, 'and'),
('or ', 0, 'or')]


def register_operation(self, token, priority, index, func):

if token not in self.ops1 or token not in self.ops1:
Copy link

@jeffreyscottgraham jeffreyscottgraham Jun 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

um.... this seems wrong... same check of ops1 twice? And not checking ops ?
And then adding it to yet another called ops2?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you guys are right. It worked for my simple cases, but indeed there is a lack of errorhandling and unittests. I'am sorry, about that.
Unfortunately I'am currently quite swamped at work and won´t be able to fix this right away. You guys have to give me some time, please.


self.ops.append((token, priority, index))
self.ops2[token] = func


def add(self, a, b):
return a + b

Expand Down Expand Up @@ -303,6 +331,7 @@ def append(self, a, b):
return a

def __init__(self):

self.success = False
self.errormsg = ''
self.expression = ''
Expand Down Expand Up @@ -648,27 +677,8 @@ def isConst(self):
return False

def isOperator(self):
ops = (
('+', 2, '+'),
('-', 2, '-'),
('**', 6, '**'),
('*', 3, '*'),
(u'\u2219', 3, '*'), # bullet operator
(u'\u2022', 3, '*'), # black small circle
('/', 4, '/'),
('%', 4, '%'),
('^', 6, '^'),
('||', 1, '||'),
('==', 1, '=='),
('!=', 1, '!='),
('<=', 1, '<='),
('>=', 1, '>='),
('<', 1, '<'),
('>', 1, '>'),
('and ', 0, 'and'),
('or ', 0, 'or'),
)
for token, priority, index in ops:

for token, priority, index in self.ops:
if self.expression.startswith(token, self.pos):
self.tokenprio = priority
self.tokenindex = index
Expand Down