Feat: dirty sub (repeatition in explain)

This commit is contained in:
Bertrand Benjamin 2019-10-13 22:32:18 +02:00
parent 207dead5d0
commit 2fad004d6d

View File

@ -97,6 +97,10 @@ class Token(object):
_other = factory(Expression(moify(other))) _other = factory(Expression(moify(other)))
else: else:
_other = other _other = other
if operation == '-':
tree = Tree("+", self._mo, Tree("-", None, _other._mo))
else:
tree = Tree(operation, self._mo, _other._mo) tree = Tree(operation, self._mo, _other._mo)
return Expression(tree).simplify() return Expression(tree).simplify()
@ -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,6 +252,9 @@ class Token(object):
_other = factory(Expression(moify(other))) _other = factory(Expression(moify(other)))
else: else:
_other = other _other = other
if operation == '-':
tree = Tree("+", _other._mo, Tree("-", None, self._mo))
else:
tree = Tree(operation, _other._mo, self._mo) tree = Tree(operation, _other._mo, self._mo)
return Expression(tree).simplify() return Expression(tree).simplify()
@ -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