Feat: dirty sub (repeatition in explain)

This commit is contained in:
Bertrand Benjamin 2019-10-13 22:32:18 +02:00
parent 510f6a1fa2
commit 1dccaabd86

View File

@ -97,7 +97,11 @@ class Token(object):
_other = factory(Expression(moify(other))) _other = factory(Expression(moify(other)))
else: else:
_other = other _other = other
tree = Tree(operation, self._mo, _other._mo)
if operation == '-':
tree = Tree("+", self._mo, Tree("-", None, _other._mo))
else:
tree = Tree(operation, self._mo, _other._mo)
return Expression(tree).simplify() return Expression(tree).simplify()
def __add__(self, other): def __add__(self, other):
@ -143,6 +147,32 @@ class Token(object):
""" """
return self._operate(other, "+") return self._operate(other, "+")
def __sub__(self, other):
""" Subing 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> b = Integer(7)
>>> c = a - b
>>> c
<Integer - 4>
>>> for i in c.explain():
... print(i)
3 - 7
3 - 7
- 4
>>> a = Integer(3)
>>> c = a - 7
>>> c
<Integer - 4>
>>> a = Integer(3)
>>> c = a - "x"
>>> c
<Linear 3 - x>
"""
return self._operate(other, "-")
def __mul__(self, other): def __mul__(self, other):
""" Multiply 2 Tokens or a Token and a Expression """ Multiply 2 Tokens or a Token and a Expression
@ -222,7 +252,10 @@ class Token(object):
_other = factory(Expression(moify(other))) _other = factory(Expression(moify(other)))
else: else:
_other = other _other = other
tree = Tree(operation, _other._mo, self._mo) if operation == '-':
tree = Tree("+", _other._mo, Tree("-", None, self._mo))
else:
tree = Tree(operation, _other._mo, self._mo)
return Expression(tree).simplify() return Expression(tree).simplify()
def __radd__(self, other): def __radd__(self, other):
@ -240,6 +273,21 @@ class Token(object):
""" """
return self._roperate(other, "+") return self._roperate(other, "+")
def __rsub__(self, other):
""" Subing 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> c = 7 - a
>>> c
<Integer 4>
>>> a = Integer(3)
>>> c = "x" - a
>>> c
<Linear x - 3>
"""
return self._roperate(other, "-")
def __rmul__(self, other): def __rmul__(self, other):
""" Multiply 2 Tokens or a Token and a Expression """ Multiply 2 Tokens or a Token and a Expression