コード例 #1
0
    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)
コード例 #2
0
 def _run(self, interpreter: Interpreter):
     function = interpreter.stack_pop()
     code = interpreter.stack_pop()
     input_parameters = interpreter.stack_pop()
     function.value = code.get_value()
     function.inputs = input_parameters
     interpreter.set_variable(function.name, function)
コード例 #3
0
    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()
コード例 #4
0
    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)
コード例 #5
0
    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)
コード例 #6
0
    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)
コード例 #7
0
    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)
コード例 #8
0
    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)
コード例 #9
0
    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)
コード例 #10
0
 def _run(self, interpreter: Interpreter):
     variable: Variable = interpreter.stack_pop()  # type: ignore
     value = interpreter.stack_pop()
     variable.value = value.get_value()
     interpreter.set_variable(variable.name, variable)
コード例 #11
0
 def _run(self, interpreter: Interpreter):
     variable = interpreter.get_variable("result")
     interpreter.remove_variable("result")
     interpreter.set_variable("it", variable)
     interpreter.stack_append(variable)