Skip to content

Commit

Permalink
Fixed bug that will cause formulas such as 1*-2^2 failed
Browse files Browse the repository at this point in the history
  • Loading branch information
NielXu committed Dec 5, 2018
1 parent 173c5dd commit 14e4f52
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions ast.py
Expand Up @@ -55,10 +55,18 @@ def add(self, sym):
self.cur.left = node(sym, self.cur)
self.cur = self.cur.left
else:
while self.cur.left is not None or expr.is_unary(self.cur.sym):
temp = self.cur
while self.cur is not None and (self.cur.left is not None or expr.is_unary(self.cur.sym)):
self.cur = self.cur.parent
self.cur.left = node(sym, self.cur)
self.cur = self.cur.left
if self.cur is None:
n = node(sym)
n.right = self.root
self.root.parent = n
self.root = n
self.cur = temp
else:
self.cur.left = node(sym, self.cur)
self.cur = self.cur.left
else:
if self.cur.right is None:
self.cur.right = node(sym, self.cur)
Expand Down Expand Up @@ -303,9 +311,9 @@ def main():


def test():
a = build("-1^2")
a = build("-1*--2^-2")
view(a)
print(evaluate(a.root))
print(evaluate(a))


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions expr.py
Expand Up @@ -39,7 +39,7 @@
"-": lambda x,y : x-y,
"*": lambda x,y : x*y,
"/": lambda x,y : x/y,
"^": lambda x,y : x**y,
"^": lambda x,y : x**y
}
symbols.update(opeartors_mapper)

Expand Down Expand Up @@ -414,4 +414,4 @@ def _match_regex(r, e):
for m in reg.finditer(e):
starts.append(m.start())
ends.append(m.end())
return starts, ends
return starts, ends

0 comments on commit 14e4f52

Please sign in to comment.