Skip to content

Commit 5847f46

Browse files
committed
ïmplement basic line expression substitutions
1 parent 4d4edae commit 5847f46

2 files changed

Lines changed: 27 additions & 26 deletions

File tree

tests/test_expressions.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,18 @@
1717

1818

1919
def test_expressions1():
20-
try:
21-
runner1.resume()
20+
expected_line = "Hello there Sam."
2221

23-
# the runner should throw an error
24-
raise Exception(
25-
"The runner ran without any issues. This test should fail. An Exception was expected.")
26-
except Exception as e:
27-
assert str(
28-
e) == "Yarn stories with interpolated inline expressions are not yet supported."
22+
runner1.resume()
23+
24+
actual_line = runner1.get_line()
25+
assert actual_line == expected_line
2926

3027

3128
def test_expressions2():
32-
try:
33-
runner2.resume()
34-
35-
# the runner should throw an error
36-
raise Exception(
37-
"The runner ran without any issues. This test should fail. An Exception was expected.")
38-
except Exception as e:
39-
assert str(
40-
e) == "Yarn stories with interpolated inline expressions are not yet supported."
29+
expected_line = "Hello there Sam."
30+
31+
runner2.resume()
32+
33+
actual_line = runner2.get_line()
34+
assert actual_line == expected_line

yarnrunner_python/runner.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ def __find_label(self, label_key):
7272
f"The current node `{self.current_node}` does not have a label named `{label_key}")
7373

7474
def __find_expressions(self, operand):
75-
# TODO: implement this functionality
76-
if int(operand.float_value) != 0:
77-
raise Exception(
78-
f"Yarn stories with interpolated inline expressions are not yet supported.")
75+
params_amount = operand.float_value
76+
77+
params = []
78+
while params_amount > 0:
79+
params.insert(0, self._vm_data_stack.pop(0))
80+
params_amount -= 1
81+
return params
7982

8083
def __debug_log(self, msg, **kwargs):
8184
if self._enable_tracing:
@@ -202,10 +205,9 @@ def __run_line(self, instruction):
202205

203206
# if this instruction has a second operand, it's the number of expressions
204207
# on the line that need to be evaluated.
208+
line_substitutions = []
205209
if len(instruction.operands) > 1:
206-
line_substitutions = self.__find_expressions(
207-
instruction.operands[1])
208-
# TODO: implement substitutions
210+
line_substitutions = self.__find_expressions(instruction.operands[1])
209211

210212
if self._experimental_newlines:
211213
# attempt to add a newlines if the last thing we did was run a line
@@ -219,7 +221,13 @@ def __run_line(self, instruction):
219221
for _i in range(diff - 1):
220222
self._line_buffer.append('')
221223

222-
self._line_buffer.append(self.__lookup_string(string_key))
224+
self._line_buffer.append(
225+
self._make_line_substitutions(
226+
self.__lookup_string(string_key), line_substitutions))
227+
228+
@staticmethod
229+
def _make_line_substitutions(line, substitutions):
230+
return line.format(*substitutions)
223231

224232
def __run_command(self, instruction):
225233
# split the command specifier by spaces, ignoring spaces
@@ -265,7 +273,6 @@ def __add_option(self, instruction):
265273
if len(instruction.operands) > 2:
266274
line_substitutions = self.__find_expressions(
267275
instruction.operands[2])
268-
# TODO: implement substitutions
269276

270277
self._option_buffer.append({
271278
'index': len(self._option_buffer),

0 commit comments

Comments
 (0)