Skip to content

Commit 4d4edae

Browse files
committed
Update to version 0.2.4.
- Experimental: the runner now maintains a stack of nodes visited and can climb the stack upon request to return to a previously visited node - Tests for this new functionality have been added - The package now exports `YarnProgram` and `YarnInstruction` protobufs
1 parent 77b6151 commit 4d4edae

8 files changed

Lines changed: 65 additions & 1 deletion

File tree

examples/yarn2/return-metadata.csv

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

examples/yarn2/return.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id,text,file,node,lineNumber
2+
line:/Users/sweaver/Git/YarnRunner-Python/examples/yarn2/return.yarn-Start-0,Here's an example of returning inline from a node.,/Users/sweaver/Git/YarnRunner-Python/examples/yarn2/return.yarn,Start,3
3+
line:/Users/sweaver/Git/YarnRunner-Python/examples/yarn2/return.yarn-Start-1,Here's the last line.,/Users/sweaver/Git/YarnRunner-Python/examples/yarn2/return.yarn,Start,7
4+
line:/Users/sweaver/Git/YarnRunner-Python/examples/yarn2/return.yarn-node2-2,Here's the inline node.,/Users/sweaver/Git/YarnRunner-Python/examples/yarn2/return.yarn,node2,12

examples/yarn2/return.yarn

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
title: Start
2+
---
3+
Here's an example of returning inline from a node.
4+
5+
<<jump node2>>
6+
7+
Here's the last line.
8+
===
9+
title: node2
10+
---
11+
12+
Here's the inline node.
13+
14+
// This command isn't implemented in the runner, but only in this specific test.
15+
<<return>>
16+
17+
===

examples/yarn2/return.yarnc

371 Bytes
Binary file not shown.

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[metadata]
44
name = yarnrunner-python
5-
version = 0.2.3
5+
version = 0.2.4
66
author = Sam Weaver
77
author_email = sweaver@relaypro.com
88
description = An unofficial Python interpreter for compiled Yarn Spinner programs.

tests/test_return.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
from .context import YarnRunner
3+
4+
compiled_yarn_f = open(os.path.join(os.path.dirname(
5+
__file__), '../examples/yarn2/return.yarnc'), 'rb')
6+
names_csv_f = open(os.path.join(os.path.dirname(
7+
__file__), '../examples/yarn2/return.csv'), 'r')
8+
9+
runner = YarnRunner(compiled_yarn_f, names_csv_f, autostart=False)
10+
11+
12+
def return_command():
13+
runner.climb_node_stack()
14+
15+
16+
runner.add_command_handler("return", return_command)
17+
18+
19+
def test_return():
20+
runner.resume()
21+
assert "Here's an example of returning inline from a node." == runner.get_line()
22+
assert runner.has_line()
23+
assert "Here's the inline node." == runner.get_line()
24+
assert runner.has_line()
25+
assert "Here's the last line." == runner.get_line()
26+
assert not runner.has_line()
27+
assert runner.finished

yarnrunner_python/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .runner import YarnRunner
2+
from .yarn_spinner_pb2 import Program as YarnProgram, Instruction as YarnInstruction

yarnrunner_python/runner.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def __init__(self, compiled_yarn_f, names_csv_f, autostart=True, enable_tracing=
1818
self.visits = {key: 0 for key in self._compiled_yarn.nodes.keys()}
1919
self.variables = {}
2020
self.current_node = None
21+
self._node_stack = []
2122
self._command_handlers = {}
2223
self._line_buffer = []
2324
self._option_buffer = []
@@ -146,6 +147,18 @@ def choose(self, index):
146147
def add_command_handler(self, command, fn):
147148
self._command_handlers[command] = fn
148149

150+
def climb_node_stack(self):
151+
if len(self._node_stack) < 1:
152+
raise Exception(
153+
"climb_node_stack() was called with an empty node stack.")
154+
155+
previous_node, previous_pc = self._node_stack.pop()
156+
self.current_node = previous_node
157+
self._vm_instruction_stack = self._compiled_yarn.nodes[previous_node].instructions
158+
self._program_counter = previous_pc
159+
if not self.paused:
160+
self.__process_instruction()
161+
149162
##### OpCode Implementations below here #####
150163

151164
def __jump_to(self, instruction):
@@ -170,6 +183,7 @@ def __go_to_node(self, node_key):
170183
raise Exception(
171184
f"{node_key} is not a valid node in this Yarn story.")
172185

186+
self._node_stack.append((self.current_node, self._program_counter))
173187
self.current_node = node_key
174188
self.visits[node_key] += 1
175189
self.__debug_log(

0 commit comments

Comments
 (0)