def _run(self, interpreter: Interpreter):
     second_value = interpreter.stack_pop().get_value()
     first_value = interpreter.stack_pop().get_value()
     condition_result = self.OPERATOR(  # pylint: disable=too-many-function-args
         first_value, second_value)
     value = self.TOKEN_FACTORY.create_value(condition_result)
     interpreter.stack_append(value)
    def run(self, interpreter: Interpreter):
        if self.collection is None:
            interpreter.run(self.tokens[-1])
            self.collection = interpreter.stack_pop().get_value()

        try:
            collection_value = self.collection[self._index]
        except IndexError:
            self.reset()
            raise exceptions.BreakIterationException(self) from None
        except TypeError:
            raise exceptions.TypeException(
                f"Cannot iterate through value of type {self.collection.__class__.__name__}!",
                token=self,
            ) from None

        value = self.TOKEN_FACTORY.create_any_value(value=collection_value)
        interpreter.stack_append(value)
        self._index += 1

        # ignore collection (last token), as it was previously run
        for token in self.tokens[:-1]:
            interpreter.run(token)

        # append it again, this value can be consumed by optional for-each
        # condition, or needs to be thrown away by for-each loop if no condition
        interpreter.stack_append(value)
    def _get_condition_result(self, value: Any,
                              interpreter: Interpreter) -> bool:
        if not self.has_all_optionals:
            return bool(value)

        interpreter.stack_append(self.TOKEN_FACTORY.create_any_value(value))
        interpreter.run(self.tokens[1])  # type: ignore
        return interpreter.stack_pop().get_value()
 def _run(self, interpreter: Interpreter):
     # ignore last token, which closes the collection (COLLECTION_END)
     self.value = []
     for _ in self.tokens[:-1]:
         value = interpreter.stack_pop()
         self.value.append(value)
     self.value.reverse()
     value = self.TOKEN_FACTORY.create_iterable_value(self.value)
     interpreter.stack_append(value)
    def _run(self, interpreter: Interpreter):
        collection = interpreter.stack_pop().get_value()

        try:
            len_ = len(collection)
        except TypeError:
            raise exceptions.TypeException(
                f"Value of type {collection.__class__.__name__} has no length!",
                token=self,
            ) from None

        length = self.TOKEN_FACTORY.create_value(len_)
        interpreter.stack_append(length)
    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):
        interpreter.run(self.tokens[0])
        collection = interpreter.stack_pop().get_value()

        for value in collection:
            result = self._get_condition_result(value, interpreter)
            if self._check_if_should_break(result):  # pylint: disable=no-member
                condition_value = not self.initial_condition_value
                break
        else:
            condition_value = self.initial_condition_value

        result = self.TOKEN_FACTORY.create_value(condition_value)
        interpreter.stack_append(result)
    def _run(self, interpreter: Interpreter):
        value = interpreter.stack_pop().get_value()
        if not isinstance(value, str):
            raise exceptions.TypeException(
                f"Value of type {value.__class__.__name__} is not a valid file identifier!"
            )

        try:
            with open(value, "r", encoding="utf-8") as file:
                content = file.read()
        except:
            raise exceptions.FileNotFoundException(
                f"File {value} cannot be read!") from None

        interpreter.stack_append(self.TOKEN_FACTORY.create_value(content))
Beispiel #10
0
    def _run(self, interpreter: Interpreter):
        try:
            collection = interpreter.stack_pop()
        except exceptions.EmptyStackError:
            self.raise_syntax_exception()

        interpreter.stack_append(collection)

        try:
            len_ = len(collection.get_value())
        except TypeError:
            raise exceptions.TypeException(
                f"Value of type {collection.__class__.__name__} has no length!"
            ) from None

        interpreter.stack_append(self.TOKEN_FACTORY.create_value(len_))
Beispiel #11
0
    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))
Beispiel #12
0
    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)
Beispiel #13
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)
Beispiel #14
0
 def _run(self, interpreter: Interpreter):
     value = interpreter.stack_pop()
     value.negate_value()
     interpreter.stack_append(value)
Beispiel #15
0
 def _run(self, interpreter: Interpreter):
     interpreter.stack_append(self.token_value)
Beispiel #16
0
 def _run(self, interpreter: Interpreter):
     variable = interpreter.get_variable("result")
     interpreter.remove_variable("result")
     interpreter.set_variable("it", variable)
     interpreter.stack_append(variable)
Beispiel #17
0
 def run(self, interpreter: Interpreter):
     interpreter.stack_append(
         self.TOKEN_FACTORY.create_iterable_value(value=[]))
     super().run(interpreter)
Beispiel #18
0
 def _run_tokens(self, interpreter: Interpreter):
     for token in self._runnable_tokens:
         interpreter.run(token)
     interpreter.stack_append(self._none)
Beispiel #19
0
 def _run(self, interpreter: Interpreter):
     end = interpreter.stack_pop().get_value()
     start = interpreter.stack_pop().get_value()
     interpreter.stack_append(
         self.TOKEN_FACTORY.create_iterable_value(
             value=list(range(start, end))))
Beispiel #20
0
 def _run(self, interpreter: Interpreter):
     variable = interpreter.get_variable("it")
     interpreter.stack_append(variable)
Beispiel #21
0
 def _run(self, interpreter: Interpreter):
     variable = interpreter.stack_pop()
     constant = self.TOKEN_FACTORY.create_constant(variable)
     interpreter.stack_append(constant)
Beispiel #22
0
 def _run(self, interpreter: Interpreter):
     variable = interpreter.stack_pop()
     variable.add_qualifier(self.qualifier)  # pylint: disable=no-member
     interpreter.stack_append(variable)
Beispiel #23
0
 def _run(self, interpreter: Interpreter):
     collection = interpreter.stack_pop()
     interpreter.stack_pop()  # default empty args
     interpreter.stack_append(collection)
Beispiel #24
0
 def run(self, interpreter: Interpreter):
     value = self.TOKEN_FACTORY.create_value(self._run_tokens)
     interpreter.stack_append(value)
Beispiel #25
0
 def _run(self, interpreter: Interpreter):
     interpreter.stack_append(
         self.TOKEN_FACTORY.create_value(self.EXCEPTION))