예제 #1
0
 def __getitem__(self, name: Token):
     if name.lexeme in self.memory:
         return self.memory[name.lexeme]
     if self.parent is not None:
         return self.parent[name]
     raise PyLOXRuntimeError(
         name, "{name} is not defined in the current "
         "environment".format(name=name.lexeme))
예제 #2
0
 def assign(self, name: Token, value: object) -> None:
     if name.lexeme not in self.memory:
         if self.parent is not None:
             self.parent.assign(name, value)
             return
         raise PyLOXRuntimeError(
             name, "{name} is not defined in the current"
             " environment".format(name=name.lexeme))
     self.memory[name.lexeme] = value
예제 #3
0
    def addition(self, operator: Token, lhs: object, rhs: object) -> object:
        if isinstance(lhs, float) and isinstance(rhs, float):
            return lhs + rhs
        if isinstance(lhs, str) and isinstance(rhs, str):
            return lhs + rhs

        raise PyLOXRuntimeError(
            operator, "PLUS was expecting (float, float) or "
            "(str, str) instead found ({lhs}, "
            "{rhs})".format(lhs=type(lhs), rhs=type(rhs)))
예제 #4
0
 def wrapped(self, operator, *args):
     for expected_type, arg in zip(types, args):
         if type(arg) != expected_type:
             received_type_message = format_type(type(arg) for arg in args)
             raise PyLOXRuntimeError(
                 operator, "{operator} was expecting "
                 "{expected} instead received "
                 "{given}".format(operator=operator.type,
                                  expected=expected_type_message,
                                  given=received_type_message))
     return fn(self, operator, *args)
예제 #5
0
 def division(self, operator: Token, lhs: float, rhs: float) -> float:
     if rhs == 0:
         raise PyLOXRuntimeError(operator, "Zero division error")
     return lhs / rhs
예제 #6
0
 def not_implemented(self, operator, *args) -> None:
     raise PyLOXRuntimeError(
         operator, "unary operator {operator} is not "
         "implemented".format(operator=operator))
예제 #7
0
 def visit_break(self, stmt: Break):
     raise PyLOXRuntimeError(stmt.token, "break statement seen outside of "
                             "a loop")