def _run(self, interpreter: Interpreter): try: variable = interpreter.get_variable(self.value) except exceptions.UndefinedVariableException: variable = self.TOKEN_FACTORY.create_variable(self.value) if self.has_all_optionals: default_value = interpreter.stack_pop() variable.value = default_value.get_value() interpreter.set_variable(variable.name, variable) interpreter.stack_append(variable) interpreter.set_variable("it", variable)
def inner(interpreter: Interpreter): args = [] for x in range(function.__code__.co_argcount): try: args.append(interpreter.get_variable(x).get_value()) except exceptions.UndefinedVariableException: break try: return_value = function(*args) except TypeError as exc: raise exceptions.RunTimeException(token=self) from exc interpreter.stack_append( self.TOKEN_FACTORY.create_any_value(return_value))
def run(self, interpreter: Interpreter): try: it = interpreter.get_variable("it") except exceptions.UndefinedVariableException: it = None for token in self.tokens[2:]: token.run(interpreter) collection = interpreter.stack_pop() collection_list = collection.get_value() self.tokens[0].run(interpreter) variable = interpreter.stack_pop() try: collection_value = list(collection.get_value()) except TypeError: raise exceptions.TypeException( f"Value of type {collection.get_value().__class__.__name__} is not iterable!" ) from None indices = [] for i, value in enumerate(collection_value): interpreter.stack_append( self.TOKEN_FACTORY.create_any_value(value)) interpreter.run(self.tokens[1]) # type: ignore condition_value = interpreter.stack_pop().get_value() if condition_value: indices.append(i) values = [] for i, index in enumerate(indices): values.append(collection_list.pop(index - i)) variable.value = values interpreter.set_variable(variable.name, variable) if it is not None: interpreter.set_variable("it", it)
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)
def _run(self, interpreter: Interpreter): variable = interpreter.get_variable("result") interpreter.remove_variable("result") interpreter.set_variable("it", variable) interpreter.stack_append(variable)
def _run(self, interpreter: Interpreter): variable = interpreter.get_variable("it") interpreter.stack_append(variable)