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 _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): 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 _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
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
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
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
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
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
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): 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
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))
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)
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)
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_))
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
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))
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)
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)