Example #1
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)
Example #2
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)
Example #3
0
 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)
Example #4
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()
Example #5
0
    def _run(self, interpreter: Interpreter):
        try_clause = interpreter.stack_pop().get_value()
        else_clause = interpreter.stack_pop().get_value()
        exception = interpreter.stack_pop().get_value()

        try:
            try_clause(interpreter)
        except exception:
            else_clause(interpreter)
Example #6
0
 def _run(self, interpreter: Interpreter):
     if_clause = interpreter.stack_pop()
     condition_value = interpreter.stack_pop()
     else_clause = interpreter.stack_pop(
     ) if self.has_all_optionals else None
     if condition_value.get_value() == 1:
         if_clause.get_value()(interpreter)
     elif else_clause is not None:
         else_clause.get_value()(interpreter)
Example #7
0
 def _run(self, interpreter: Interpreter):
     variable = interpreter.stack_pop()
     value = interpreter.stack_pop()
     try:
         self.do_operation(variable, value)  # pylint: disable=no-member
     except TypeError:
         type_ = lambda x: x.get_value().__class__.__name__
         raise exceptions.TypeException(
             f"Unsupported operation for types {type_(variable)} and {type_(value)}!",
             token=self,
         ) from None
Example #8
0
 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
Example #9
0
    def _run(self, interpreter: Interpreter):
        collection_variable = interpreter.stack_pop()
        collection: List[Any] = collection_variable.get_value()
        value = interpreter.stack_pop().get_value()

        try:
            collection.append(value)
        except AttributeError:
            raise exceptions.TypeException(
                f"Cannot append value to type {collection.__class__.__name__}!",
                token=self,
            ) from None
Example #10
0
    def run(self, interpreter: Interpreter):
        interpreter.run(self.tokens[-1])
        clause_function = interpreter.stack_pop().get_value()
        condition = self.tokens[0]
        while True:
            condition.run(interpreter)
            if not interpreter.stack_pop().get_value():
                break

            try:
                clause_function(interpreter)
            except exceptions.BreakIterationException:
                break
Example #11
0
    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
Example #12
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)
Example #13
0
    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)
Example #14
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)
Example #15
0
 def _run(self, interpreter: Interpreter):
     collection: List[Any] = interpreter.stack_pop().get_value()
     try:
         collection.reverse()
     except AttributeError:
         raise exceptions.TypeException(
             f"Cannot reverse value of type {collection.__class__.__name__}!"
         ) from None
Example #16
0
    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()
Example #17
0
 def _run(self, interpreter: Interpreter):
     value = interpreter.stack_pop()
     try:
         value.value = round(value.value)
     except TypeError:
         raise exceptions.TypeException(
             f"Cannot round value of type {value.value.__class__.__name__}!"
         ) from None
Example #18
0
 def _run(self, interpreter: Interpreter):
     value: List[Any] = interpreter.stack_pop().get_value()
     try:
         value.sort()
     except AttributeError:
         raise exceptions.TypeException(
             f"Cannot sort value of type {value.__class__.__name__}"
         ) from None
Example #19
0
 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)
Example #20
0
    def _run(self, interpreter: Interpreter):
        mode = "w"
        if self.has_all_optionals:
            interpreter.stack_pop()
            mode = "a"

        filename = interpreter.stack_pop().get_value()
        if not isinstance(filename, str):
            raise exceptions.TypeException(
                f"Value of type {filename.__class__.__name__} is not a valid file identifier!"
            )

        value = interpreter.stack_pop().get_value()
        try:
            with open(filename, mode=mode, encoding="utf-8") as file:
                file.write(value)
        except Exception as exc:
            raise exceptions.RunTimeException(
                f"Failed to read file {filename}!") from exc
Example #21
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))
Example #22
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)
Example #23
0
    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)
Example #24
0
    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)
Example #25
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)
Example #26
0
    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))
Example #27
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_))
Example #28
0
    def run(self, interpreter: Interpreter):
        while True:
            try:
                for token in self.tokens:
                    interpreter.run(token)
            except exceptions.BreakIterationException:
                break

            clause = interpreter.stack_pop()
            try:
                if self._check_condition(interpreter):
                    clause.get_value()(interpreter)
            except exceptions.SkipElementException:
                pass
            except exceptions.BreakIterationException:
                break
Example #29
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)
Example #30
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)