2013-12-08 20:02:19 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
from generic import Stack,flatten_list
|
|
|
|
from fraction import Fraction
|
2014-01-17 19:17:49 +00:00
|
|
|
from formal import FormalExp
|
2013-12-08 20:02:19 +00:00
|
|
|
|
|
|
|
class Render(object):
|
|
|
|
"""A class which aims to create render functions from three dictionnaries:
|
|
|
|
- op_infix: dict of caracters
|
|
|
|
- op_postfix: dict of 2 arguments functions
|
|
|
|
- other: dict of caracters
|
|
|
|
Those three dictionnaries while define how a postfix expression will be transform into a string.
|
|
|
|
"""
|
|
|
|
|
2014-01-28 19:52:38 +00:00
|
|
|
PRIORITY = {"^": 4,"*" : 3, "/": 3, ":": 3, "+": 2, "-":2, "(": 1}
|
2013-12-08 20:02:19 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str, FormalExp: str}):
|
2013-12-08 20:02:19 +00:00
|
|
|
"""Initiate the render
|
|
|
|
|
|
|
|
@param op_infix: the dictionnary of infix operator with how they have to be render
|
|
|
|
@param op_postfix: the dictionnary of postfix operator with how they have to be render
|
|
|
|
@param other: other caracters like parenthesis.
|
2013-12-08 20:38:11 +00:00
|
|
|
@param raw: the caracter for joining the list of tokens (if False then it returns the list of tokens)
|
2013-12-08 21:24:27 +00:00
|
|
|
@param type_render: how to render number (str or tex for fractions for example)
|
2013-12-08 20:02:19 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
self.op_infix = op_infix
|
|
|
|
self.op_postfix = op_postfix
|
|
|
|
self.other = other
|
2013-12-08 20:38:11 +00:00
|
|
|
# TODO: there may be issues with PRIORITY if a sign does not appear in PRIORITY
|
|
|
|
|
|
|
|
self.join = join
|
2013-12-08 21:24:27 +00:00
|
|
|
self.type_render = type_render
|
2013-12-08 20:02:19 +00:00
|
|
|
|
|
|
|
self.operators = list(self.op_infix.keys()) + list(self.op_postfix.keys()) + list(self.other.keys())
|
|
|
|
|
|
|
|
def __call__(self, postfix_tokens):
|
|
|
|
"""Make the object acting like a function
|
|
|
|
|
|
|
|
:param postfix_tokens: the list of postfix tokens to be render
|
|
|
|
:returns: the render string
|
|
|
|
|
|
|
|
"""
|
|
|
|
operandeStack = Stack()
|
|
|
|
|
|
|
|
|
|
|
|
for token in postfix_tokens:
|
|
|
|
if self.isOperator(token):
|
2013-12-08 21:24:27 +00:00
|
|
|
|
2013-12-08 20:02:19 +00:00
|
|
|
op2 = operandeStack.pop()
|
|
|
|
if self.needPar(op2, token, "after"):
|
2013-12-08 20:38:11 +00:00
|
|
|
op2 = [self.other["("] , op2 , self.other[")"]]
|
2013-12-08 21:24:27 +00:00
|
|
|
|
2013-12-08 20:02:19 +00:00
|
|
|
op1 = operandeStack.pop()
|
|
|
|
if self.needPar(op1, token, "before"):
|
2013-12-08 20:38:11 +00:00
|
|
|
op1 = [self.other["("] , op1 , self.other[")"]]
|
2013-12-08 20:02:19 +00:00
|
|
|
|
|
|
|
if token in self.op_infix:
|
2013-12-08 20:38:11 +00:00
|
|
|
res = flist([op1 , self.op_infix[token] , op2])
|
2013-12-08 20:02:19 +00:00
|
|
|
|
|
|
|
elif token in self.op_postfix:
|
2013-12-08 20:38:11 +00:00
|
|
|
res = flist([self.op_postfix[token](op1, op2)])
|
2013-12-08 20:02:19 +00:00
|
|
|
|
|
|
|
# Trick to remember the main op when the render will be done!
|
|
|
|
res.mainOp = token
|
|
|
|
|
|
|
|
operandeStack.push(res)
|
|
|
|
|
|
|
|
else:
|
|
|
|
operandeStack.push(token)
|
2014-01-17 19:17:49 +00:00
|
|
|
|
2013-12-08 20:02:19 +00:00
|
|
|
|
2013-12-08 21:24:27 +00:00
|
|
|
# Manip pour gerer les cas de listes imbriquées dans d'autres listes
|
2013-12-08 20:02:19 +00:00
|
|
|
infix_tokens = operandeStack.pop()
|
2013-12-08 20:38:11 +00:00
|
|
|
if type(infix_tokens) == list or type(infix_tokens) == flist:
|
2013-12-08 20:02:19 +00:00
|
|
|
infix_tokens = flatten_list(infix_tokens)
|
2014-01-17 19:17:49 +00:00
|
|
|
elif self.isNumerande(infix_tokens):
|
2013-12-08 20:02:19 +00:00
|
|
|
infix_tokens = [infix_tokens]
|
|
|
|
|
2013-12-08 20:38:11 +00:00
|
|
|
if self.join:
|
2013-12-08 21:24:27 +00:00
|
|
|
return self.join.join(flatten_list([self.render_from_type(t) for t in infix_tokens]))
|
2013-12-08 20:38:11 +00:00
|
|
|
else:
|
|
|
|
return infix_tokens
|
2013-12-08 20:02:19 +00:00
|
|
|
|
2013-12-08 21:24:27 +00:00
|
|
|
def render_from_type(self, op):
|
2014-01-17 19:17:49 +00:00
|
|
|
""" If the op is a numerande, it transforms it with type_render conditions
|
2013-12-08 21:24:27 +00:00
|
|
|
|
|
|
|
:param op: the operator
|
|
|
|
:returns: the op transformed if it's necessary
|
|
|
|
|
|
|
|
"""
|
2014-01-17 19:17:49 +00:00
|
|
|
if self.isNumerande(op):
|
2013-12-08 21:24:27 +00:00
|
|
|
return self.type_render[type(op)](op)
|
|
|
|
else:
|
|
|
|
return op
|
|
|
|
|
|
|
|
|
2013-12-08 20:02:19 +00:00
|
|
|
# ---------------------
|
|
|
|
# Tools for placing parenthesis in infix notation
|
|
|
|
|
|
|
|
def needPar(self, operande, operator, posi = "after"):
|
|
|
|
"""Says whether or not the operande needs parenthesis
|
|
|
|
|
|
|
|
:param operande: the operande
|
|
|
|
:param operator: the operator
|
|
|
|
:param posi: "after"(default) if the operande will be after the operator, "before" othewise
|
|
|
|
:returns: bollean
|
|
|
|
"""
|
2014-01-28 19:52:38 +00:00
|
|
|
# Si l'operande est negatif
|
2014-01-17 13:57:45 +00:00
|
|
|
if self.isNumber(operande) \
|
|
|
|
and operande < 0:
|
2013-12-08 20:02:19 +00:00
|
|
|
return 1
|
2014-01-17 19:17:49 +00:00
|
|
|
|
2014-01-28 19:52:38 +00:00
|
|
|
# Si c'est un expression formelle
|
2014-01-17 19:17:49 +00:00
|
|
|
elif type(operande) == FormalExp:
|
2014-01-28 19:52:38 +00:00
|
|
|
if operator in ["*", "/", "^"]:
|
2014-01-17 19:17:49 +00:00
|
|
|
if len(operande) > 1 \
|
|
|
|
or operande.master_coef() < 0:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2013-12-08 20:02:19 +00:00
|
|
|
elif not self.isNumber(operande):
|
|
|
|
# Si c'est une grande expression ou un chiffre négatif
|
|
|
|
stand_alone = self.get_main_op(operande)
|
|
|
|
# Si la priorité de l'operande est plus faible que celle de l'opérateur
|
|
|
|
minor_priority = self.PRIORITY[self.get_main_op(operande)] < self.PRIORITY[operator]
|
2014-01-28 19:52:38 +00:00
|
|
|
# Si l'opérateur est - ou / pour after ou / ou ^ pour before
|
|
|
|
special = (operator in "-/" and posi == "after") or (operator in "/^" and posi == "before")
|
2013-12-08 20:02:19 +00:00
|
|
|
|
|
|
|
return stand_alone and (minor_priority or special)
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def get_main_op(self, tokens):
|
|
|
|
"""Getting the main operation of the list of tokens
|
|
|
|
|
|
|
|
:param exp: the list of tokens
|
|
|
|
:returns: the main operation (+, -, * or /) or 0 if the expression is only one element
|
|
|
|
|
|
|
|
"""
|
|
|
|
if hasattr(tokens, "mainOp"):
|
|
|
|
return tokens.mainOp
|
|
|
|
|
|
|
|
if len(tokens) == 1:
|
|
|
|
# Si l'expression n'est qu'un élément
|
|
|
|
return 0
|
|
|
|
|
|
|
|
parStack = Stack()
|
|
|
|
main_op = []
|
|
|
|
|
|
|
|
for token in tokens:
|
|
|
|
if token == "(":
|
|
|
|
parStack.push(token)
|
|
|
|
elif token == ")":
|
|
|
|
parStack.pop()
|
|
|
|
elif self.isOperator(token) and parStack.isEmpty():
|
|
|
|
main_op.append(token)
|
|
|
|
|
|
|
|
return min(main_op, key = lambda s: self.PRIORITY[s])
|
|
|
|
|
|
|
|
## ---------------------
|
|
|
|
## Recognize numbers and operators
|
|
|
|
|
2013-12-08 21:24:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def isNumber( exp):
|
2014-01-17 19:17:49 +00:00
|
|
|
"""Check if the expression can be a number which means int or Fraction
|
2013-12-08 20:02:19 +00:00
|
|
|
|
|
|
|
:param exp: an expression
|
|
|
|
:returns: True if the expression can be a number and false otherwise
|
|
|
|
|
|
|
|
"""
|
2014-01-17 19:17:49 +00:00
|
|
|
return type(exp) == int \
|
|
|
|
or type(exp) == Fraction
|
2014-01-17 13:57:45 +00:00
|
|
|
#return type(exp) == int or type(exp) == Fraction
|
2013-12-08 20:02:19 +00:00
|
|
|
|
2014-01-17 19:17:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def isNumerande(exp):
|
|
|
|
"""Check if the expression can be a numerande (not an operator)
|
|
|
|
|
|
|
|
:param exp: an expression
|
|
|
|
:returns: True if the expression can be a number and false otherwise
|
|
|
|
|
|
|
|
"""
|
|
|
|
return type(exp) == int \
|
|
|
|
or type(exp) == Fraction \
|
|
|
|
or type(exp) == FormalExp
|
|
|
|
|
2013-12-08 20:02:19 +00:00
|
|
|
def isOperator(self, exp):
|
|
|
|
"""Check if the expression is in self.operators
|
|
|
|
|
|
|
|
:param exp: an expression
|
|
|
|
:returns: boolean
|
|
|
|
|
|
|
|
"""
|
|
|
|
return (type(exp) == str and exp in self.operators)
|
|
|
|
|
2013-12-08 20:38:11 +00:00
|
|
|
class flist(list):
|
|
|
|
"""Fake list- they are used to stock the main operation of an rendered expression"""
|
2013-12-08 20:02:19 +00:00
|
|
|
pass
|
|
|
|
|
2013-12-08 20:38:11 +00:00
|
|
|
|
2013-12-08 20:02:19 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|