Throw bases for tree2txt
This commit is contained in:
parent
a8a62864a6
commit
a51386fded
73
mapytex/calculus/core/tree2txt.py
Normal file
73
mapytex/calculus/core/tree2txt.py
Normal file
@ -0,0 +1,73 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2017 lafrite <lafrite@Poivre>
|
||||
#
|
||||
# Distributed under terms of the MIT license.
|
||||
|
||||
def plus2txt(left, right):
|
||||
""" + rendering
|
||||
|
||||
>>> from .MO import mo
|
||||
>>> plus2txt(mo.MO(2), mo.MO(3))
|
||||
'2 + 3'
|
||||
>>> from .tree import Tree
|
||||
>>> t = Tree.from_str("1+2")
|
||||
>>> plus2txt(t, mo.MO(3))
|
||||
'1 + 2 + 3'
|
||||
"""
|
||||
try:
|
||||
left.node
|
||||
except AttributeError:
|
||||
left_ = left.__txt__
|
||||
else:
|
||||
left_ = tree2txt(left)
|
||||
try:
|
||||
right.node
|
||||
except AttributeError:
|
||||
right_ = right.__txt__
|
||||
else:
|
||||
right_ = tree2txt(right)
|
||||
|
||||
return f"{left_} + {right_}"
|
||||
|
||||
|
||||
|
||||
def minus2txt(left, right):
|
||||
pass
|
||||
|
||||
def mul2txt(left, right):
|
||||
pass
|
||||
|
||||
def div2txt(left, right):
|
||||
pass
|
||||
|
||||
def pow2txt(left, right):
|
||||
pass
|
||||
|
||||
OPERATOR2TXT = {
|
||||
"+": plus2txt,
|
||||
"-": minus2txt,
|
||||
"*": mul2txt,
|
||||
"/": div2txt,
|
||||
"^": pow2txt,
|
||||
}
|
||||
|
||||
def tree2txt(tree):
|
||||
""" Convert a tree into its txt version
|
||||
|
||||
It calls __txt__ to render MOs.
|
||||
|
||||
>>> from .tree import Tree
|
||||
>>> t = Tree.from_str("2+3*4")
|
||||
>>> tree2txt(t)
|
||||
"2 + 3 * 4"
|
||||
"""
|
||||
return OPERATOR2TXT[tree.node](tree.left_value, tree.right_value)
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
Loading…
Reference in New Issue
Block a user