Skip to content

Commit

Permalink
Merge pull request relaypro-open#9 from josefalanga/variables-declara…
Browse files Browse the repository at this point in the history
…tion

Support Variable Declaration (set initial values)
  • Loading branch information
weaversam8 authored Jul 12, 2023
2 parents f95a1d7 + 75589df commit dd2586e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 22 deletions.
1 change: 1 addition & 0 deletions examples/yarn2/declarations-metadata.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id,node,lineNumber,tags
7 changes: 7 additions & 0 deletions examples/yarn2/declarations.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
id,text,file,node,lineNumber
line:C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn-Start-0,The value of test bool is {0},C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn,Start,10
line:C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn-Start-1,The value of test number is {0},C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn,Start,11
line:C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn-Start-2,"The value of test string is ""{0}""",C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn,Start,12
line:C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn-Start-3,The value of test bool is {0},C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn,Start,13
line:C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn-Start-4,The value of test number is {0},C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn,Start,14
line:C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn-Start-5,"The value of test string is ""{0}""",C:\jose\YarnRunner-Python\examples\yarn2\declarations.yarn,Start,15
16 changes: 16 additions & 0 deletions examples/yarn2/declarations.yarn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
title: Start
---
<<declare $testString = "initial value">>
<<declare $testNum = 1234556>>
<<declare $testBool = true>>
<<declare $testStringDefault = "">>
<<declare $testNumDefault = 0>>
<<declare $testBoolDefault = false>>

The value of test bool is {$testBool}
The value of test number is {$testNum}
The value of test string is "{$testString}"
The value of test bool is {$testBoolDefault}
The value of test number is {$testNumDefault}
The value of test string is "{$testStringDefault}"
===
Binary file added examples/yarn2/declarations.yarnc
Binary file not shown.
26 changes: 26 additions & 0 deletions tests/test_declarations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
from .context import YarnRunner

compiled_yarn = open(os.path.join(os.path.dirname(
__file__), '../examples/yarn2/declarations.yarnc'), 'rb')
names_csv = open(os.path.join(os.path.dirname(
__file__), '../examples/yarn2/declarations.csv'), 'r')

runner = YarnRunner(compiled_yarn, names_csv)


def test_variables1():
# variables are stored
assert runner.variables["$testBool"] is True
assert runner.variables["$testNum"] == 1234556
assert runner.variables["$testString"] == "initial value"

# the values get to the output without exceptions
assert runner.get_line() == 'The value of test bool is True'
assert runner.get_line() == 'The value of test number is 1234556.0'
assert runner.get_line() == 'The value of test string is "initial value"'
assert runner.get_line() == 'The value of test bool is False'
assert runner.get_line() == 'The value of test number is 0.0'
assert runner.get_line() == 'The value of test string is ""'

assert runner.has_line() is False
14 changes: 13 additions & 1 deletion yarnrunner_python/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,21 @@ def __init__(self, compiled_yarn_f, names_csv_f, autostart=True, enable_tracing=
self.paused = True
self.finished = False

self.load_initial_vars()

if autostart:
self.resume()

def load_initial_vars(self):
for key in self._compiled_yarn.initial_values:
initial_value = self._compiled_yarn.initial_values[key]
if initial_value.HasField("string_value"):
self.variables[key] = initial_value.string_value
elif initial_value.HasField("bool_value"):
self.variables[key] = initial_value.bool_value
elif initial_value.HasField("float_value"):
self.variables[key] = initial_value.float_value

def __construct_string_lookup_table(self):
self.string_lookup_table = dict()

Expand Down Expand Up @@ -382,7 +394,7 @@ def __push_variable(self, instruction):
return

if variable_name not in self.variables:
raise Exception(f"Variable {variable_name} has not been set.")
raise Exception(f"Variable {variable_name} has not been set or declared.")

self._vm_data_stack.insert(
0, self.variables[variable_name])
Expand Down
43 changes: 22 additions & 21 deletions yarnrunner_python/yarn_spinner_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd2586e

Please sign in to comment.