예제 #1
0
    def test_complex_operation(self):

        two = Operation(OperationType.POSITIVE(), [Variable(2)])
        negative_x = Operation(OperationType.NEGATIVE(), [Variable('x')])
        op = Operation(OperationType.TIMES(), [two, negative_x])

        verify(str(op), self.reporter)
예제 #2
0
    def test_equation(self):

        two = Operation(OperationType.POSITIVE(), [Variable(2)])
        negative_x = Operation(OperationType.NEGATIVE(), [Variable('x')])
        lhs = Operation(OperationType.TIMES(), [two, negative_x])

        rhs = Variable(3.1415)

        eq = Equation(lhs, rhs)

        verify(str(eq), self.reporter)
예제 #3
0
class PrefixParselet:

    token_map = {
        TokenType.PLUS : OperationType.POSITIVE(),
        TokenType.MINUS : OperationType.NEGATIVE()
    }

    def __init__(self, token):
        assert isinstance(token, Token)

        if token.token_type in self.token_map.keys():
            self._operation_type = self.token_map[token.token_type]
        else:
            self._operation_type = OperationType.VARIABLE(token.value)

    def parse(self, parser):
        assert isinstance(parser, Parser)

        if self._operation_type in self.token_map.values():
            right = parser.parse(precedence=10) # All prefixes should be evaluated before infixes
            return Operation(self._operation_type, [right])
        else:
            return Operation(self._operation_type)