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))
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
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)))
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)
def division(self, operator: Token, lhs: float, rhs: float) -> float: if rhs == 0: raise PyLOXRuntimeError(operator, "Zero division error") return lhs / rhs
def not_implemented(self, operator, *args) -> None: raise PyLOXRuntimeError( operator, "unary operator {operator} is not " "implemented".format(operator=operator))
def visit_break(self, stmt: Break): raise PyLOXRuntimeError(stmt.token, "break statement seen outside of " "a loop")