Skip to content

Commit 8daaf1e

Browse files
committed
Implement JUMP opcode.
1 parent 6f34d83 commit 8daaf1e

6 files changed

Lines changed: 69 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Only a subset of Yarn Spinner opcodes are currently implemented. This will certa
5555
| OpCode | Status |
5656
| ---------------- | ------------------------------------------------------ |
5757
| `JUMP_TO` |  Implemented in `runner.__jump_to` |
58-
| `JUMP` | 🚫  Not Implemented |
58+
| `JUMP` |   Implemented in `runner.__jump` |
5959
| `RUN_LINE` |  Implemented in `runner.__run_line` |
6060
| `RUN_COMMAND` |  Implemented in `runner.__run_command` |
6161
| `ADD_OPTION` |  Implemented in `runner.__add_option` |

examples/shortcuts.csv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
id,text,file,node,lineNumber
2+
shortcuts-Start-0,This is a test of shortcut functionality.,shortcuts,Start,3
3+
shortcuts-Start-1,Option 1,shortcuts,Start,5
4+
shortcuts-Start-2,Option 2,shortcuts,Start,7
5+
shortcuts-Start-3,Option 3,shortcuts,Start,9
6+
shortcuts-Start-4,Option 4,shortcuts,Start,10
7+
shortcuts-Start-5,Option 1 selected.,shortcuts,Start,6
8+
shortcuts-Start-6,Option 2 selected.,shortcuts,Start,8
9+
shortcuts-Start-7,This is the last line.,shortcuts,Start,12

examples/shortcuts.yarn

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
title: Start
2+
---
3+
This is a test of shortcut functionality.
4+
5+
-> Option 1
6+
Option 1 selected.
7+
-> Option 2
8+
Option 2 selected.
9+
-> Option 3
10+
-> Option 4
11+
12+
This is the last line.
13+
===

examples/shortcuts.yarnc

503 Bytes
Binary file not shown.

tests/test_shortcuts.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
from .context import YarnRunner
3+
4+
compiled_yarn_f = open(os.path.join(os.path.dirname(
5+
__file__), '../examples/shortcuts.yarnc'), 'rb')
6+
names_csv_f = open(os.path.join(os.path.dirname(
7+
__file__), '../examples/shortcuts.csv'), 'r')
8+
9+
runner = YarnRunner(compiled_yarn_f, names_csv_f)
10+
11+
12+
def test_start_node_text():
13+
assert "This is a test of shortcut functionality." == runner.get_line()
14+
assert not runner.has_line()
15+
assert not runner.finished
16+
17+
18+
def test_start_node_choices():
19+
choices = runner.get_choices()
20+
21+
assert len(choices) == 4
22+
assert choices[0]["text"] == "Option 1"
23+
assert choices[1]["text"] == "Option 2"
24+
assert choices[2]["text"] == "Option 3"
25+
assert choices[3]["text"] == "Option 4"
26+
27+
28+
def test_shortcuts():
29+
runner.choose(0)
30+
31+
assert "Option 1 selected." == runner.get_line()
32+
assert runner.has_line()
33+
assert "This is the last line." == runner.get_line()
34+
assert not runner.has_line()
35+
assert runner.finished
36+
assert runner.current_node == 'Start'

yarnrunner_python/runner.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ def __jump_to(self, instruction):
126126
instruction.operands[0].string_value)
127127
print(f"to {self._program_counter}")
128128

129+
def __jump(self, _instruction):
130+
if len(self._vm_data_stack) < 1 or type(self._vm_data_stack[0]) != str:
131+
raise Exception(
132+
"The JUMP opcode requires a string to be on the top of the stack. A string is not currently present.")
133+
134+
self._program_counter = self.__find_label(
135+
self._vm_data_stack[0]
136+
)
137+
129138
def __go_to_node(self, node_key):
130139
# print(f"Go from {self.current_node} to node {node_key}")
131140
if node_key not in self._compiled_yarn.nodes.keys():
@@ -244,7 +253,7 @@ def noop(instruction):
244253

245254
opcode_functions = {
246255
Instruction.OpCode.JUMP_TO: self.__jump_to,
247-
Instruction.OpCode.JUMP: noop,
256+
Instruction.OpCode.JUMP: self.__jump,
248257
Instruction.OpCode.RUN_LINE: self.__run_line,
249258
Instruction.OpCode.RUN_COMMAND: self.__run_command,
250259
Instruction.OpCode.ADD_OPTION: self.__add_option,

0 commit comments

Comments
 (0)