def _run(self, interpreter: Interpreter): function = interpreter.stack_pop() input_values: List[Any] if self.has_all_optionals: inputs = interpreter.stack_pop() inputs.get_value() # check defined input_values = inputs.value else: input_values = [] interpreter.add_stack() # some functions dont have inputs if function.inputs and function.inputs.value: # take input parameters from stack for input_, variable in zip(input_values, function.inputs.value): variable.value = input_.get_value() variable.inputs = input_.inputs interpreter.set_variable(variable.name, variable) try: function.get_value()(interpreter) except TypeError: raise exceptions.TypeException( f"Cannot call variable of type {function.get_value().__class__.__name__}!", token=self, ) from None except exceptions.ReturnException: pass return_value: Variable = interpreter.stack_pop() # type: ignore interpreter.remove_stack() interpreter.set_variable("result", return_value)
def _run(self, interpreter: Interpreter): collection = interpreter.stack_pop() try: list_ = list(collection.get_value()) except TypeError: raise exceptions.TypeException( f"Value of type {collection.get_value().__class__.__name__} is not iterable!" ) from None function = interpreter.stack_pop() # some functions dont have inputs if (not function.inputs or not function.inputs.value or not len(function.inputs.value) == 1): raise exceptions.TypeException( "Apply function should expect one input!") interpreter.add_stack() variable = function.inputs.value[0] interpreter.set_variable(variable.name, variable) for i, value in enumerate(list_): variable.value = value try: function.get_value()(interpreter) except TypeError: raise exceptions.TypeException( f"Cannot call variable of type {function.get_value().__class__.__name__}!" ) from None except exceptions.ReturnException: pass collection.value[i] = interpreter.stack_pop().get_value( ) # return value interpreter.remove_stack()
def _import_tokens(self, interpreter: Interpreter, filename: str) -> None: if self.TOKEN_COMPILER is not None: try: tokens = self.TOKEN_COMPILER.read_compiled_file(filename) except self.TOKEN_COMPILER.exception: tokens = self._construct_tokens(filename) else: tokens = self._construct_tokens(filename) interpreter.add_stack() for token in tokens: interpreter.init(token) with path.ChangeDir(os.path.dirname(filename)): for token in tokens: interpreter.run(token)
def _import_python_module(self, interpreter: Interpreter, filename: str) -> None: module = importlib.import_module(os.path.splitext(filename)[0]) interpreter.add_stack() for name, value in module.__dict__.items(): if name.startswith("_"): continue variable = self.TOKEN_FACTORY.create_variable(name) if callable(value): variable.inputs = self.TOKEN_FACTORY.create_iterable_value( value=[ self.TOKEN_FACTORY.create_variable(x) for x in range(value.__code__.co_argcount) ]) value = self._wrap_python_callable(value) variable.value = value interpreter.set_variable(name, variable) interpreter.set_variable("it", variable)