def evaluate(self, push_state: PushState, interpreter_config): """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_types` and `output_types`. 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. """ # Compute result. State should be modified in place during function. result = self.f(push_state) # Return if revert. 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))) # Push results. push_state.push_to_stacks(result, self.output_types) return push_state
def evaluate(self, push_state: PushState, interpreter_config): """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 ---------- 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. """ # Compute result. State should be modified in place during function. result = self.f(push_state) # Return if revert. 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) )) # Push results. push_state.push_to_stacks(result, self.output_stacks) 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 : PushConfig Configuration of the interpreter. Used to get various limits. Returns ------- PushState Return the given state, possibly modified by the Instruction. """ # Compute result. State should be modified in place during function. result = self.f(push_state) # Return if revert. if result is Token.revert: return push_state _check_is_seq(result, self) # Push results. push_state.push_to_stacks(result, self.output_stacks) return push_state
def test_push_values(self, state: PushState): state.push_to_stacks( [5, 100, "Foo"], ["int", "int", "str"] ) assert state["int"].top() == 100 assert state["str"].top() == "Foo" assert state.size() == 3
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_push_values(self, state: PushState): state.push_to_stacks([5, 100, "Foo"], ["int", "int", "str"]) assert state["int"].top() == 100 assert state["str"].top() == "Foo" assert state.size() == 3