Fix(Core): bug with nested tree and parsing parenthesis
This commit is contained in:
parent
5cbefa3165
commit
5d2611260a
@ -612,6 +612,14 @@ def str2(sink, convert_to_mo=True):
|
||||
>>> t = str2nestedlist(exp)
|
||||
>>> print(t)
|
||||
[1, '+', 3, '*', 'x', '^', 2]
|
||||
>>> exp = "1+(3+4)*2"
|
||||
>>> t = str2nestedlist(exp)
|
||||
>>> print(t)
|
||||
[1, '+', [3, '+', 4], '*', 2]
|
||||
>>> exp = "6x + (8 + 3) * x"
|
||||
>>> t = str2nestedlist(exp)
|
||||
>>> print(t)
|
||||
[6, '*', 'x', '+', [8, '+', 3], '*', 'x']
|
||||
|
||||
>>> from .tree import MutableTree
|
||||
>>> str2tree = str2(MutableTree.sink)
|
||||
@ -683,6 +691,17 @@ def str2(sink, convert_to_mo=True):
|
||||
| > +
|
||||
| | > 3
|
||||
| | > 4
|
||||
>>> exp = "1+(3+4)*2"
|
||||
>>> t = str2tree(exp)
|
||||
>>> print(t)
|
||||
+
|
||||
> 1
|
||||
> *
|
||||
| > +
|
||||
| | > 3
|
||||
| | > 4
|
||||
| > 2
|
||||
|
||||
>>> exp = "2*4-1"
|
||||
>>> t = str2tree(exp)
|
||||
>>> print(t)
|
||||
|
@ -934,6 +934,18 @@ class MutableTree(Tree):
|
||||
| | > 2
|
||||
| | > 3
|
||||
| > 4
|
||||
>>> t = MutableTree.from_str("6x + (8 + 3) * x")
|
||||
>>> print(t)
|
||||
+
|
||||
> *
|
||||
| > 6
|
||||
| > x
|
||||
> *
|
||||
| > +
|
||||
| | > 8
|
||||
| | > 3
|
||||
| > x
|
||||
|
||||
"""
|
||||
str_2_mut_tree = str2(cls.sink, convert_to_mo)
|
||||
return str_2_mut_tree(expression)
|
||||
@ -977,21 +989,25 @@ class MutableTree(Tree):
|
||||
| > 1
|
||||
> 2
|
||||
>>> sink = MutableTree.sink()
|
||||
>>> for i in ["1", "+", "2", "*", "x", "^", "3"]:
|
||||
>>> for i in ["1", "+", "2"]:
|
||||
... sink.send(i)
|
||||
>>> t = sink.throw(STOOOP)
|
||||
>>> sink = MutableTree.sink()
|
||||
>>> for i in ["1", "+", t, "*", 5]:
|
||||
... sink.send(i)
|
||||
>>> a = sink.throw(STOOOP)
|
||||
>>> print(a)
|
||||
+
|
||||
> 1
|
||||
> *
|
||||
| > 2
|
||||
| > ^
|
||||
| | > x
|
||||
| | > 3
|
||||
|
||||
| > +
|
||||
| | > 1
|
||||
| | > 2
|
||||
| > 5
|
||||
|
||||
"""
|
||||
ans = cls()
|
||||
nested_tree = None
|
||||
try:
|
||||
while True:
|
||||
c = yield
|
||||
@ -1001,12 +1017,33 @@ class MutableTree(Tree):
|
||||
ans.set_node(c)
|
||||
except ValueError:
|
||||
if OPERATORS[c]["precedence"] > OPERATORS[ans.node]["precedence"]:
|
||||
# the operation has to be done first
|
||||
if nested_tree is not None:
|
||||
b_tree = cls(c, nested_tree, None)
|
||||
nested_tree = None
|
||||
ans.append(b_tree)
|
||||
else:
|
||||
ans.append_bot(c)
|
||||
else:
|
||||
if nested_tree is not None:
|
||||
ans.append(nested_tree)
|
||||
nested_tree = None
|
||||
ans.append_top(c)
|
||||
else:
|
||||
if nested_tree is not None:
|
||||
ans.set_left_value(nested_tree)
|
||||
nested_tree = None
|
||||
|
||||
else:
|
||||
try:
|
||||
c.node
|
||||
except AttributeError:
|
||||
ans.append(c)
|
||||
else:
|
||||
nested_tree = c
|
||||
except STOOOP:
|
||||
if nested_tree is not None:
|
||||
ans.append(nested_tree)
|
||||
yield ans
|
||||
|
||||
def set_node(self, value):
|
||||
@ -1136,7 +1173,7 @@ class MutableTree(Tree):
|
||||
else:
|
||||
self.set_right_value(value)
|
||||
|
||||
def append_top(self, value):
|
||||
def append_top(self, node):
|
||||
""" Append node into the tree at the top
|
||||
|
||||
:example:
|
||||
@ -1151,14 +1188,15 @@ class MutableTree(Tree):
|
||||
> None
|
||||
"""
|
||||
|
||||
#self_cp = MutableTree.from_any_tree(self)
|
||||
self_cp = MutableTree()
|
||||
self_cp.set_node(self.node)
|
||||
self_cp.set_left_value(self.left_value)
|
||||
self_cp.set_right_value(self.right_value)
|
||||
|
||||
self.left_value, self.node, self.right_value = self_cp, value, None
|
||||
self.left_value, self.node, self.right_value = self_cp, node, None
|
||||
|
||||
def append_bot(self, value):
|
||||
def append_bot(self, node):
|
||||
""" Append node into the tree at the bottom right_value
|
||||
|
||||
:example:
|
||||
@ -1192,13 +1230,13 @@ class MutableTree(Tree):
|
||||
|
||||
rv = self.right_value
|
||||
try:
|
||||
if value == rv.node:
|
||||
rv.append_top(value)
|
||||
if node == rv.node:
|
||||
rv.append_top(node)
|
||||
else:
|
||||
rv.append_bot(value)
|
||||
rv.append_bot(node)
|
||||
except AttributeError:
|
||||
nright_value = MutableTree()
|
||||
nright_value.set_node(value)
|
||||
nright_value.set_node(node)
|
||||
nright_value.set_left_value(rv)
|
||||
|
||||
self.right_value = nright_value
|
||||
@ -1607,6 +1645,10 @@ class AssocialTree(Tree):
|
||||
|
||||
return tree
|
||||
|
||||
if __name__ == "__main__":
|
||||
a = MutableTree.from_str("(1+2)*3")
|
||||
print(a)
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
Loading…
Reference in New Issue
Block a user