2018-03-09 16:33:02 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
from ..operator import OPERATORS
|
2018-03-10 05:44:01 +00:00
|
|
|
|
|
|
|
__all__ = ['tree2txt']
|
2018-03-10 05:03:55 +00:00
|
|
|
|
2018-03-09 16:33:02 +00:00
|
|
|
def plus2txt(left, right):
|
|
|
|
""" + rendering
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> from ..MO import MO
|
|
|
|
>>> plus2txt(MO.factory(2), MO.factory(3))
|
2018-03-09 16:33:02 +00:00
|
|
|
'2 + 3'
|
2018-03-10 05:44:01 +00:00
|
|
|
>>> from ..tree import Tree
|
2018-03-09 16:33:02 +00:00
|
|
|
>>> t = Tree.from_str("1+2")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> plus2txt(t, MO.factory(3))
|
2018-03-09 16:33:02 +00:00
|
|
|
'1 + 2 + 3'
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> plus2txt(t, MO.factory(-3))
|
2018-03-10 05:03:55 +00:00
|
|
|
'1 + 2 - 3'
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> plus2txt(MO.factory(-3), t)
|
2018-03-10 05:03:55 +00:00
|
|
|
'- 3 + 1 + 2'
|
|
|
|
>>> t = Tree.from_str("-2*3")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> plus2txt(MO.factory(3), t)
|
2018-03-10 05:03:55 +00:00
|
|
|
'3 - 2 * 3'
|
2018-03-09 16:33:02 +00:00
|
|
|
"""
|
2018-03-10 05:03:55 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
left_ = render_with_parenthesis(left, "+")
|
2018-12-21 10:31:10 +00:00
|
|
|
right_ = render_with_parenthesis(right, "+")
|
2018-12-21 10:26:37 +00:00
|
|
|
|
|
|
|
display_plus = True
|
|
|
|
if right_.startswith("-"):
|
|
|
|
display_plus = False
|
2018-03-09 16:33:02 +00:00
|
|
|
|
2018-03-10 05:03:55 +00:00
|
|
|
if display_plus:
|
|
|
|
return f"{left_} + {right_}"
|
2018-03-09 16:33:02 +00:00
|
|
|
|
2018-03-10 05:03:55 +00:00
|
|
|
return f"{left_} {right_}"
|
2018-03-09 16:33:02 +00:00
|
|
|
|
|
|
|
def minus2txt(left, right):
|
2018-03-10 05:03:55 +00:00
|
|
|
""" - rendering
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> from ..MO import MO
|
|
|
|
>>> minus2txt(None, MO.factory(3))
|
2018-03-10 05:03:55 +00:00
|
|
|
'- 3'
|
2018-03-10 05:44:01 +00:00
|
|
|
>>> from ..tree import Tree
|
2018-03-10 05:03:55 +00:00
|
|
|
>>> t = Tree.from_str("1+2")
|
|
|
|
>>> minus2txt(None, t)
|
|
|
|
'- (1 + 2)'
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
right_need_parenthesis = False
|
|
|
|
if OPERATORS[right.node]["precedence"] < OPERATORS['-']["precedence"]:
|
|
|
|
right_need_parenthesis = True
|
|
|
|
except AttributeError:
|
|
|
|
right_ = right.__txt__
|
|
|
|
else:
|
|
|
|
if right_need_parenthesis:
|
|
|
|
right_ = f"({tree2txt(right)})"
|
|
|
|
else:
|
|
|
|
right_ = tree2txt(right)
|
|
|
|
|
|
|
|
return f"- {right_}"
|
2018-03-09 16:33:02 +00:00
|
|
|
|
|
|
|
def mul2txt(left, right):
|
2018-03-10 05:03:55 +00:00
|
|
|
""" * rendering
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> from ..MO import MO
|
|
|
|
>>> mul2txt(MO.factory(2), MO.factory(3))
|
2018-03-10 05:03:55 +00:00
|
|
|
'2 * 3'
|
2018-03-10 05:44:01 +00:00
|
|
|
>>> from ..tree import Tree
|
2018-03-10 05:03:55 +00:00
|
|
|
>>> t = Tree.from_str("1*2")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> mul2txt(t, MO.factory(3))
|
2018-03-10 05:03:55 +00:00
|
|
|
'1 * 2 * 3'
|
|
|
|
>>> t = Tree.from_str("1+2")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> mul2txt(t, MO.factory(3))
|
2018-03-10 05:03:55 +00:00
|
|
|
'(1 + 2) * 3'
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> mul2txt(MO.factory(3), t)
|
2018-03-10 05:03:55 +00:00
|
|
|
'3(1 + 2)'
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> a = MO.factory('x')
|
|
|
|
>>> mul2txt(MO.factory(3), a)
|
2018-03-10 05:03:55 +00:00
|
|
|
'3x'
|
2018-12-21 11:20:13 +00:00
|
|
|
>>> mul2txt(MO.factory(-3), a)
|
|
|
|
'- 3x'
|
2018-10-10 08:40:40 +00:00
|
|
|
>>> mul2txt(a, a)
|
|
|
|
'x * x'
|
2018-03-10 05:03:55 +00:00
|
|
|
"""
|
|
|
|
display_time = True
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
left_ = render_with_parenthesis(left, "*")
|
|
|
|
right_ = render_with_parenthesis(right, "*")
|
|
|
|
|
2018-12-21 11:20:13 +00:00
|
|
|
if right_[0].isalpha():
|
|
|
|
# TODO: C'est bien beurk en dessous... |ven. déc. 21 12:03:07 CET 2018
|
|
|
|
if type(left).__name__ == 'MOnumber':
|
|
|
|
display_time = False
|
|
|
|
elif right_[0] == '(':
|
2018-12-21 10:26:37 +00:00
|
|
|
display_time = False
|
2018-03-10 05:03:55 +00:00
|
|
|
|
|
|
|
if display_time:
|
|
|
|
return f"{left_} * {right_}"
|
|
|
|
else:
|
|
|
|
return f"{left_}{right_}"
|
2018-03-09 16:33:02 +00:00
|
|
|
|
|
|
|
def div2txt(left, right):
|
2018-03-10 05:03:55 +00:00
|
|
|
""" / rendering
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> from ..MO import MO
|
|
|
|
>>> div2txt(MO.factory(2), MO.factory(3))
|
2018-03-10 05:03:55 +00:00
|
|
|
'2 / 3'
|
2018-03-10 05:44:01 +00:00
|
|
|
>>> from ..tree import Tree
|
2018-03-10 05:03:55 +00:00
|
|
|
>>> t = Tree.from_str("1/2")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> div2txt(t, MO.factory(3))
|
2018-03-10 05:03:55 +00:00
|
|
|
'1 / 2 / 3'
|
|
|
|
>>> t = Tree.from_str("1+2")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> div2txt(t, MO.factory(3))
|
2018-03-10 05:03:55 +00:00
|
|
|
'(1 + 2) / 3'
|
|
|
|
>>> t = Tree.from_str("1*2")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> div2txt(MO.factory(3), t)
|
2018-03-10 05:03:55 +00:00
|
|
|
'3 / (1 * 2)'
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
left_need_parenthesis = False
|
|
|
|
if OPERATORS[left.node]["precedence"] < OPERATORS['/']["precedence"]:
|
|
|
|
left_need_parenthesis = True
|
|
|
|
except AttributeError:
|
|
|
|
left_ = left.__txt__
|
|
|
|
else:
|
|
|
|
if left_need_parenthesis:
|
|
|
|
left_ = f"({tree2txt(left)})"
|
|
|
|
else:
|
|
|
|
left_ = tree2txt(left)
|
|
|
|
try:
|
|
|
|
right_need_parenthesis = False
|
|
|
|
if OPERATORS[right.node]["precedence"] < OPERATORS['/']["precedence"]:
|
|
|
|
right_need_parenthesis = True
|
|
|
|
except AttributeError:
|
|
|
|
right_ = right.__txt__
|
|
|
|
else:
|
|
|
|
if right_need_parenthesis:
|
|
|
|
right_ = f"({tree2txt(right)})"
|
|
|
|
else:
|
|
|
|
right_ = tree2txt(right)
|
|
|
|
|
|
|
|
return f"{left_} / {right_}"
|
2018-03-09 16:33:02 +00:00
|
|
|
|
|
|
|
def pow2txt(left, right):
|
2018-03-10 05:03:55 +00:00
|
|
|
""" ^ rendering
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> from ..MO import MO
|
|
|
|
>>> pow2txt(MO.factory(2), MO.factory(3))
|
2018-03-14 08:40:57 +00:00
|
|
|
'2^3'
|
2018-03-10 05:44:01 +00:00
|
|
|
>>> from ..tree import Tree
|
2018-03-10 05:03:55 +00:00
|
|
|
>>> t = Tree.from_str("1^2")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> pow2txt(t, MO.factory(3))
|
2018-03-14 08:40:57 +00:00
|
|
|
'1^2^3'
|
2018-03-10 05:03:55 +00:00
|
|
|
>>> t = Tree.from_str("1+2")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> pow2txt(t, MO.factory(3))
|
2018-03-14 08:40:57 +00:00
|
|
|
'(1 + 2)^3'
|
2018-03-10 05:03:55 +00:00
|
|
|
>>> t = Tree.from_str("1*2")
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> pow2txt(MO.factory(3), t)
|
2018-03-14 08:40:57 +00:00
|
|
|
'3^(1 * 2)'
|
2018-03-10 05:03:55 +00:00
|
|
|
"""
|
2018-12-21 10:26:37 +00:00
|
|
|
left_ = render_with_parenthesis(left, "^")
|
|
|
|
|
2018-03-10 05:03:55 +00:00
|
|
|
try:
|
|
|
|
right_need_parenthesis = False
|
|
|
|
if OPERATORS[right.node]["precedence"] < OPERATORS['^']["precedence"]:
|
|
|
|
right_need_parenthesis = True
|
|
|
|
except AttributeError:
|
|
|
|
right_ = right.__txt__
|
|
|
|
else:
|
|
|
|
if right_need_parenthesis:
|
|
|
|
right_ = f"({tree2txt(right)})"
|
|
|
|
else:
|
|
|
|
right_ = tree2txt(right)
|
|
|
|
|
2018-03-14 08:40:57 +00:00
|
|
|
return f"{left_}^{right_}"
|
2018-03-09 16:33:02 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
def render_with_parenthesis(subtree, operator):
|
|
|
|
subtree_need_parenthesis = False
|
|
|
|
try:
|
|
|
|
subtree.node
|
|
|
|
except AttributeError:
|
|
|
|
try:
|
|
|
|
if OPERATORS[subtree.MAINOP]["precedence"] < OPERATORS[operator]["precedence"]:
|
|
|
|
subtree_need_parenthesis = True
|
|
|
|
except (AttributeError, KeyError):
|
|
|
|
pass
|
|
|
|
subtree_ = subtree.__txt__
|
|
|
|
else:
|
|
|
|
if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]:
|
|
|
|
subtree_need_parenthesis = True
|
|
|
|
subtree_ = tree2txt(subtree)
|
|
|
|
|
|
|
|
if subtree_need_parenthesis:
|
|
|
|
return f"({subtree_})"
|
|
|
|
return subtree_
|
|
|
|
|
2018-03-09 16:33:02 +00:00
|
|
|
OPERATOR2TXT = {
|
2018-03-13 11:43:48 +00:00
|
|
|
"+": plus2txt,
|
|
|
|
"-": minus2txt,
|
|
|
|
"*": mul2txt,
|
|
|
|
"/": div2txt,
|
|
|
|
"^": pow2txt,
|
2018-03-09 16:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def tree2txt(tree):
|
|
|
|
""" Convert a tree into its txt version
|
|
|
|
|
|
|
|
It calls __txt__ to render MOs.
|
|
|
|
|
2018-09-17 14:27:20 +00:00
|
|
|
:param tree: tree to render
|
|
|
|
|
|
|
|
:example:
|
|
|
|
|
2018-03-10 05:44:01 +00:00
|
|
|
>>> from ..tree import Tree
|
2018-03-09 16:33:02 +00:00
|
|
|
>>> t = Tree.from_str("2+3*4")
|
|
|
|
>>> tree2txt(t)
|
2018-03-10 05:03:55 +00:00
|
|
|
'2 + 3 * 4'
|
2018-03-09 16:33:02 +00:00
|
|
|
"""
|
2018-12-21 10:26:37 +00:00
|
|
|
from ..tree import Tree
|
|
|
|
if not isinstance(tree, Tree):
|
|
|
|
raise ValueError(f"Can only render a Tree (got {type(tree).__name__}: {tree})")
|
2018-03-13 11:43:48 +00:00
|
|
|
return OPERATOR2TXT[tree.node](tree.left_value, tree.right_value)
|
2018-03-09 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|