def test_pop_values(self, state: PushState): state["int"].push(100) state["int"].push(-77) state["str"].push("Hello") state["str"].push("World") popped_vals = state.pop_from_stacks(["int", "int", "str"]) assert popped_vals == [-77, 100, "World"] assert state["str"].top() == "Hello" assert state.size() == 1
def test_pop_values(self, state: PushState): state["int"].push(100) state["int"].push(-77) state["str"].push("Hello") state["str"].push("World") popped_vals = state.pop_from_stacks(["int", "int", "str"]) assert popped_vals == [-77, 100, "World"] assert state["str"].top() == "Hello" assert state.size() == 1
def evaluate(self, push_state: PushState, push_config: PushConfig = None) -> PushState: """Evaluate the instruction on the given PushState. Return mutated State. A ProducesManyOfTypeInstruction infers which values to pop from the stack based on `input_stacks` and pushes each output to the same stack based on `output_stack`. Parameters ---------- push_state : PushState Push state to modify with the Instruction. push_config : PushConfig Configuration of the interpreter. Used to get various limits. Returns ------- PushState Return the given state, possibly modified by the Instruction. """ # Pull args, if present. args = push_state.observe_stacks(self.input_stacks) if Token.no_stack_item in args: return push_state # Compute result, return if revert or response too big. result = self.f(*args) if result is Token.revert: return push_state if not isinstance(result, (list, tuple)): raise ValueError( "Instruction result must be a collection. {i} gave {t}.". format(i=self, t=type(result))) # Remove arguments, push results. push_state.pop_from_stacks(self.input_stacks) push_state.push_to_stacks(result, [self.output_stack] * len(result)) return push_state
def evaluate(self, push_state: PushState, interpreter_config): """Evaluate the instruction on the given PushState. Return mutated State. A ProducesManyOfTypeInstruction infers which values to pop from the stack based on `input_stacks` and pushes each output to the same stack based on `output_stack`. Parameters ---------- state : PushState Push state to modify with the Instruction. config : PushInterpreterConfig Configuration of the interpreter. Used to get various limits. Returns ------- PushState Return the given state, possibly modified by the Instruction. """ # Pull args, if present. args = push_state.observe_stacks(self.input_stacks) if Token.no_stack_item in args: return push_state # Compute result, return if revert or response too big. result = self.f(*args) if result is Token.revert: return push_state if not isinstance(result, (list, tuple)): raise ValueError("Instruction result must be a collection. {i} gave {t}.".format( i=self, t=type(result) )) # Remove arguments, push results. push_state.pop_from_stacks(self.input_stacks) push_state.push_to_stacks(result, [self.output_stack] * len(result)) return push_state
def evaluate(self, push_state: PushState, push_config: PushConfig = None) -> PushState: """Evaluate the instruction on the given PushState. Return mutated State. A SimpleInstruction infers which values to pop and push from the stack based on its `input_stacks` and `output_stacks`. Parameters ---------- push_state : PushState Push state to modify with the Instruction. push_config : pyshgp.push.interpreter.PushConfig Configuration of the interpreter. Used to get various limits. Returns ------- PushState Return the given state, possibly modified by the Instruction. """ # Pull args, if present. args = push_state.observe_stacks(self.input_stacks) if Token.no_stack_item in args: return push_state # Compute result, return if revert or response too big. result = self.f(*args) if result is Token.revert: return push_state _check_is_seq(result, self) # Remove arguments, push results. push_state.pop_from_stacks(self.input_stacks) push_state.push_to_stacks(result, self.output_stacks) return push_state
def test_pop_values_empty(self, state: PushState): popped_vals = state.pop_from_stacks(["int", "int", "str"]) assert popped_vals == Token.revert assert state.size() == 0
def test_pop_values_empty(self, state: PushState): popped_vals = state.pop_from_stacks(["int", "int", "str"]) assert popped_vals == Token.revert assert state.size() == 0