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