From 5d2611260a330b5b76a1e48a5d04e22a768604eb Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Tue, 27 Nov 2018 17:11:06 +0100 Subject: [PATCH] Fix(Core): bug with nested tree and parsing parenthesis --- mapytex/calculus/core/str2.py | 19 +++++++++ mapytex/calculus/core/tree.py | 72 +++++++++++++++++++++++++++-------- 2 files changed, 76 insertions(+), 15 deletions(-) diff --git a/mapytex/calculus/core/str2.py b/mapytex/calculus/core/str2.py index 909ee1d..0024d06 100644 --- a/mapytex/calculus/core/str2.py +++ b/mapytex/calculus/core/str2.py @@ -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) diff --git a/mapytex/calculus/core/tree.py b/mapytex/calculus/core/tree.py index 62f1b62..0d2abc6 100644 --- a/mapytex/calculus/core/tree.py +++ b/mapytex/calculus/core/tree.py @@ -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"]: - ans.append_bot(c) + # 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: - ans.append(c) + 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: