Skip to content

Commit e0296b0

Browse files
committed
Implement the 3 missing "PUSH" opcodes.
All opcodes are now implemented.
1 parent 41aa04a commit e0296b0

6 files changed

Lines changed: 42 additions & 7 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ A few gotchas to look out for:
5050
- Make sure to open the compiled story file as a binary file (see the above example, use `open(filename, 'rb')`) in order for it to be properly parsed by the compiled protobuf library.
5151
- Unless you pass `autostart=False` to the runner when creating it, it will automatically start and run to the next choice point.
5252

53-
Only a subset of Yarn Spinner opcodes are currently implemented. This will certainly change over time. The current status is:
53+
All Yarn Spinner opcodes are currently implemented. This may certainly change over time, if new opcodes are added to the language. The current status is:
5454

5555
| OpCode | Status |
5656
| ---------------- | ------------------------------------------------------ |
@@ -60,10 +60,10 @@ Only a subset of Yarn Spinner opcodes are currently implemented. This will certa
6060
| `RUN_COMMAND` |  Implemented in `runner.__run_command` |
6161
| `ADD_OPTION` |  Implemented in `runner.__add_option` |
6262
| `SHOW_OPTIONS` |  Implemented in `runner.__show_options` |
63-
| `PUSH_STRING` | 🚫  Not Implemented |
63+
| `PUSH_STRING` |   Implemented in `runner.__push_string` |
6464
| `PUSH_FLOAT` |  Implemented in `runner.__push_float` |
65-
| `PUSH_BOOL` | 🚫  Not Implemented |
66-
| `PUSH_NULL` | 🚫  Not Implemented |
65+
| `PUSH_BOOL` |   Implemented in `runner.__push_bool` |
66+
| `PUSH_NULL` |   Implemented in `runner.__push_null` |
6767
| `JUMP_IF_FALSE` |  Implemented in `runner.__jump_if_false` |
6868
| `POP` |  Implemented in `runner.__pop` |
6969
| `CALL_FUNC` |  Implemented in `runner.__call_func` |

examples/variables.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
id,text,file,node,lineNumber

examples/variables.yarn

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title: Start
2+
---
3+
<<set $value_string to "string">>
4+
<<set $value_float to 1.25>>
5+
<<set $value_bool to true>>
6+
<<set $value_null to null>>
7+
===

examples/variables.yarnc

162 Bytes
Binary file not shown.

tests/test_variables.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
from .context import YarnRunner
3+
4+
compiled_yarn_f = open(os.path.join(os.path.dirname(
5+
__file__), '../examples/variables.yarnc'), 'rb')
6+
names_csv_f = open(os.path.join(os.path.dirname(
7+
__file__), '../examples/variables.csv'), 'r')
8+
9+
runner = YarnRunner(compiled_yarn_f, names_csv_f)
10+
11+
12+
def test_variables():
13+
assert not runner.has_line()
14+
assert runner.finished
15+
assert runner.variables["$value_string"] == "string"
16+
assert runner.variables["$value_float"] == 1.25
17+
assert runner.variables["$value_bool"] == True
18+
assert runner.variables["$value_null"] is None

yarnrunner_python/runner.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,18 @@ def __add_option(self, instruction):
184184
def __show_options(self, _instruction):
185185
self.paused = True
186186

187+
def __push_string(self, instruction):
188+
self._vm_data_stack.insert(0, instruction.operands[0].string_value)
189+
187190
def __push_float(self, instruction):
188191
self._vm_data_stack.insert(0, instruction.operands[0].float_value)
189192

193+
def __push_bool(self, instruction):
194+
self._vm_data_stack.insert(0, instruction.operands[0].bool_value)
195+
196+
def __push_null(self, _instruction):
197+
self._vm_data_stack.insert(0, None)
198+
190199
def __jump_if_false(self, instruction):
191200
if self._vm_data_stack[0] == False:
192201
self.__jump_to(instruction)
@@ -285,10 +294,10 @@ def noop(instruction):
285294
Instruction.OpCode.RUN_COMMAND: self.__run_command,
286295
Instruction.OpCode.ADD_OPTION: self.__add_option,
287296
Instruction.OpCode.SHOW_OPTIONS: self.__show_options,
288-
Instruction.OpCode.PUSH_STRING: noop,
297+
Instruction.OpCode.PUSH_STRING: self.__push_string,
289298
Instruction.OpCode.PUSH_FLOAT: self.__push_float,
290-
Instruction.OpCode.PUSH_BOOL: noop,
291-
Instruction.OpCode.PUSH_NULL: noop,
299+
Instruction.OpCode.PUSH_BOOL: self.__push_bool,
300+
Instruction.OpCode.PUSH_NULL: self.__push_null,
292301
Instruction.OpCode.JUMP_IF_FALSE: self.__jump_if_false,
293302
Instruction.OpCode.POP: self.__pop,
294303
Instruction.OpCode.CALL_FUNC: self.__call_func,

0 commit comments

Comments
 (0)