Skip to content

Commit 4796881

Browse files
committed
Implement new opcodes:
- `PUSH_FLOAT` - `POP` - `PUSH_VARIABLE` - `STORE_VARIABLE`
1 parent efa9936 commit 4796881

2 files changed

Lines changed: 42 additions & 23 deletions

File tree

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,25 @@ A few gotchas to look out for:
5252

5353
Only a subset of Yarn Spinner opcodes are currently implemented. This will certainly change over time. The current status is:
5454

55-
| OpCode | Status |
56-
| ---------------- | ---------------------------------------------------- |
57-
| `JUMP_TO` | 🚫  Not Implemented |
58-
| `JUMP` | 🚫  Not Implemented |
59-
| `RUN_LINE` |  Implemented in `runner.__run_line` |
60-
| `RUN_COMMAND` |  Implemented in `runner.__run_command` |
61-
| `ADD_OPTION` |  Implemented in `runner.__add_option` |
62-
| `SHOW_OPTIONS` |  Implemented in `runner.__show_options` |
63-
| `PUSH_STRING` | 🚫  Not Implemented |
64-
| `PUSH_FLOAT` | 🚫  Not Implemented |
65-
| `PUSH_BOOL` | 🚫  Not Implemented |
66-
| `PUSH_NULL` | 🚫  Not Implemented |
67-
| `JUMP_IF_FALSE` | 🚫  Not Implemented |
68-
| `POP` | 🚫  Not Implemented |
69-
| `CALL_FUNC` | 🚫  Not Implemented |
70-
| `PUSH_VARIABLE` | 🚫  Not Implemented |
71-
| `STORE_VARIABLE` | 🚫  Not Implemented |
72-
| `STOP` |  Implemented in `runner.stop` |
73-
| `RUN_NODE` |  Implemented in `runner.__run_node` |
55+
| OpCode | Status |
56+
| ---------------- | ------------------------------------------------------ |
57+
| `JUMP_TO` | 🚫  Not Implemented |
58+
| `JUMP` | 🚫  Not Implemented |
59+
| `RUN_LINE` |  Implemented in `runner.__run_line` |
60+
| `RUN_COMMAND` |  Implemented in `runner.__run_command` |
61+
| `ADD_OPTION` |  Implemented in `runner.__add_option` |
62+
| `SHOW_OPTIONS` |  Implemented in `runner.__show_options` |
63+
| `PUSH_STRING` | 🚫  Not Implemented |
64+
| `PUSH_FLOAT` |   Implemented in `runner.__push_float` |
65+
| `PUSH_BOOL` | 🚫  Not Implemented |
66+
| `PUSH_NULL` | 🚫  Not Implemented |
67+
| `JUMP_IF_FALSE` | 🚫  Not Implemented |
68+
| `POP` |   Implemented in `runner.__pop` |
69+
| `CALL_FUNC` | 🚫  Not Implemented |
70+
| `PUSH_VARIABLE` |   Implemented in `runner.__push_variable` |
71+
| `STORE_VARIABLE` |   Implemented in `runner.__store_variable` |
72+
| `STOP` |  Implemented in `runner.__stop` |
73+
| `RUN_NODE` |  Implemented in `runner.__run_node` |
7474

7575
## Development
7676

yarnrunner_python/runner.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(self, compiled_yarn_f, names_csv_f, autostart=True) -> None:
1111
self.__construct_string_lookup_table()
1212

1313
self.visits = {key: 0 for key in self._compiled_yarn.nodes.keys()}
14+
self.variables = {}
1415
self.current_node = None
1516
self._command_handlers = {}
1617
self._line_buffer = []
@@ -53,6 +54,11 @@ def debug_vm(self):
5354
self.debug_vm_instruction_stack()
5455
print("The current VM data stack is:")
5556
print(self._vm_data_stack)
57+
self.debug_variables()
58+
59+
def debug_variables(self):
60+
print("The current variables stored are:")
61+
print(self.variables)
5662

5763
def debug_vm_instruction_stack(self):
5864
print(f"The current program counter is: {self._program_counter}")
@@ -138,6 +144,19 @@ def __add_option(self, instruction):
138144
def __show_options(self, _instruction):
139145
self.paused = True
140146

147+
def __push_float(self, instruction):
148+
self._vm_data_stack.insert(0, instruction.operands[0].float_value)
149+
150+
def __pop(self, _instruction):
151+
self._vm_data_stack.pop(0)
152+
153+
def __push_variable(self, instruction):
154+
self._vm_data_stack.insert(
155+
0, self.variables[instruction.operands[0].string_value])
156+
157+
def __store_variable(self, instruction):
158+
self.variables[instruction.operands[0].string_value] = self._vm_data_stack[0]
159+
141160
def __stop(self, _instruction):
142161
self.finished = True
143162

@@ -181,14 +200,14 @@ def noop(instruction):
181200
Instruction.OpCode.ADD_OPTION: self.__add_option,
182201
Instruction.OpCode.SHOW_OPTIONS: self.__show_options,
183202
Instruction.OpCode.PUSH_STRING: noop,
184-
Instruction.OpCode.PUSH_FLOAT: noop,
203+
Instruction.OpCode.PUSH_FLOAT: self.__push_float,
185204
Instruction.OpCode.PUSH_BOOL: noop,
186205
Instruction.OpCode.PUSH_NULL: noop,
187206
Instruction.OpCode.JUMP_IF_FALSE: noop,
188-
Instruction.OpCode.POP: noop,
207+
Instruction.OpCode.POP: self.__pop,
189208
Instruction.OpCode.CALL_FUNC: noop,
190-
Instruction.OpCode.PUSH_VARIABLE: noop,
191-
Instruction.OpCode.STORE_VARIABLE: noop,
209+
Instruction.OpCode.PUSH_VARIABLE: self.__push_variable,
210+
Instruction.OpCode.STORE_VARIABLE: self.__store_variable,
192211
Instruction.OpCode.STOP: self.__stop,
193212
Instruction.OpCode.RUN_NODE: self.__run_node,
194213
}

0 commit comments

Comments
 (0)