create a list of operator

This commit is contained in:
lafrite 2014-11-11 20:35:30 +01:00
parent 4920bbd27d
commit 693c982cbe
3 changed files with 17 additions and 20 deletions

View File

@ -183,6 +183,9 @@ class Operator(str):
return flatten_list([op])
class op(object):
""" List of admited operations """
# TODO: On pourrait peut être le faire plus proprement avec des décorateurs? |mar. nov. 11 20:24:54 CET 2014
add = Operator("+")
sub = Operator("-")
mul = Operator("*")

View File

@ -65,17 +65,13 @@ def p2i_render(token):
try:
return getattr(token, '__p2i__')(*args)
except AttributeError:
return list(token)
return token
return render
p2i = Render(p2i_render)
if __name__ == '__main__':
from .operator import Operator
mul = Operator("*", 2)
add = Operator("+", 2)
sub1 = Operator("-", 1)
div = Operator("/", 1)
exp = [ 2, 3, add, 4, mul]
from .operator import op
exp = [ 2, 3, op.add, 4, op.mul]
print(exp)
print("txt(exp) :" + str(txt(exp)))
print("tex(exp) :" + str(tex(exp)))

View File

@ -161,12 +161,10 @@ if __name__ == '__main__':
#
#print(in2post_fix(in_tokens))
#a, s, m, d, p = Operator("+"), Operator("-"), Operator("*"), Operator("/"), Operator("^")
#s1 = Operator("-", 1)
#par = Operator("(")
#print(in2post_fix([par, 2, a, 5, s, 1, ')', d, par, 3, m, 4, ')']))
#print(in2post_fix([s1, par, s1, 2, ')']))
#print(in2post_fix([s1, par, s1, 2, a, 3, m, 4, ')']))
from .operator import op
print(in2post_fix([op.par, 2, op.add, 5, op.sub, 1, ')', op.div, op.par, 3, op.mul, 4, ')']))
print(in2post_fix([op.sub1, op.par, op.sub1, 2, ')']))
print(in2post_fix([op.sub1, op.par, op.sub1, 2, op.add, 3, op.mul, 4, ')']))
import doctest
doctest.testmod()