diff --git a/mapytex/calculus/API/tokens/token.py b/mapytex/calculus/API/tokens/token.py index 650f9a0..7b62a90 100644 --- a/mapytex/calculus/API/tokens/token.py +++ b/mapytex/calculus/API/tokens/token.py @@ -97,7 +97,11 @@ class Token(object): _other = factory(Expression(moify(other))) else: _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() def __add__(self, other): @@ -143,6 +147,32 @@ class Token(object): """ 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 + + >>> for i in c.explain(): + ... print(i) + 3 - 7 + 3 - 7 + - 4 + >>> a = Integer(3) + >>> c = a - 7 + >>> c + + >>> a = Integer(3) + >>> c = a - "x" + >>> c + + """ + return self._operate(other, "-") + def __mul__(self, other): """ Multiply 2 Tokens or a Token and a Expression @@ -222,7 +252,10 @@ class Token(object): _other = factory(Expression(moify(other))) else: _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() def __radd__(self, other): @@ -240,6 +273,21 @@ class Token(object): """ 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 + + >>> a = Integer(3) + >>> c = "x" - a + >>> c + + """ + return self._roperate(other, "-") def __rmul__(self, other): """ Multiply 2 Tokens or a Token and a Expression