def _run(self, interpreter: Interpreter): end_index = interpreter.stack_pop().get_value() collection = interpreter.stack_pop().get_value() start_index = interpreter.stack_pop().get_value() if not isinstance(start_index, int): raise exceptions.ValueException( f"Wrong index of type {start_index.__class__.__name__}!" ) from None try: subcollection = collection[start_index:end_index] except TypeError: raise exceptions.TypeException( f"Value of type {collection.__class__.__name__} cannot be indexed!" ) from None except IndexError: raise exceptions.ValueException("Index out of range!") from None interpreter.stack_append( self.TOKEN_FACTORY.create_iterable_value(subcollection))
def _run(self, interpreter: Interpreter): index = interpreter.stack_pop().get_value() parent_value = interpreter.stack_pop().get_value() value = interpreter.stack_pop() try: parent_value[index] = value.get_value() except TypeError: raise exceptions.TypeException( f"Value of type {parent_value.__class__.__name__} cannot be indexed!" ) from None except IndexError: raise exceptions.ValueException("Index out of range!") from None
def _run(self, interpreter: Interpreter): collection: List[Any] = interpreter.stack_pop().get_value() value = interpreter.stack_pop().get_value() try: collection.remove(value) except AttributeError: raise exceptions.TypeException( f"Cannot remove value from type {collection.__class__.__name__}!", token=self, ) from None except ValueError: raise exceptions.ValueException("Value not in collection!", token=self) from None
def _run(self, interpreter: Interpreter): variable = interpreter.stack_pop() collection = interpreter.stack_pop().get_value() index = interpreter.stack_pop().get_value() try: variable.value = collection[index] except IndexError: raise exceptions.ValueException( f"Collection index {index} out of range!") from None except TypeError: raise exceptions.TypeException( f"Cannot index value of type {collection.__class__.__name__}!" ) from None interpreter.set_variable(variable.name, variable)
def _run(self, interpreter: Interpreter): variable = interpreter.stack_pop() collection_variable: List[Any] = interpreter.stack_pop() collection = collection_variable.get_value() try: variable.value = collection.pop() except AttributeError: raise exceptions.TypeException( f"Cannot pop from type {collection.__class__.__name__}!", token=self) from None except IndexError: raise exceptions.ValueException( "Cannot pop from empty collection!", token=self) from None interpreter.set_variable(variable.name, variable)
def run(self, interpreter: Interpreter): interpreter.run(self.tokens[-1]) collection = interpreter.stack_pop().get_value() try: collection_value = collection[self.RETURN_TOKEN_INDEX] except IndexError: raise exceptions.ValueException( "Cannot extract value from empty collection!") from None except TypeError: raise exceptions.TypeException( f"Cannot extract from value of type {collection.__class__.__name__}!", ) from None value = self.TOKEN_FACTORY.create_any_value(collection_value) interpreter.stack_append(value) # ignore collection (last token), as it was previously run for token in self.tokens[:-1]: interpreter.run(token) # append it again, previous stack append gets consumed by IN token interpreter.stack_append(value)
def run(self, interpreter: Interpreter): interpreter.run(self.tokens[0]) import_variables = interpreter.stack_pop().value filename = self.tokens[-1].value import_ = (self._import_python_module if filename.endswith(".py") else self._import_tokens) try: import_(interpreter, filename) except (FileNotFoundError, ModuleNotFoundError): raise exceptions.ImportException( f"Failed to import because file could not be found: {filename}", token=self, ) from None variables: List[Variable] = [] for import_variable in import_variables: try: variable = interpreter.get_variable(import_variable.name) except AttributeError: type_ = import_variable.value.__class__.__name__ raise exceptions.ValueException( f"Cannot import: value of type {type_} is not a variable!" ) from None if variable.get_qualifier("private"): raise exceptions.ImportException( f"Could not import private variable {variable.name} from module {filename}!", token=self, ) from None variables.append(variable) interpreter.remove_stack() for variable in variables: interpreter.set_variable(variable.name, variable)