Skip to content

Commit 41aa04a

Browse files
committed
Add debug logging, fix a bug with the $visits_ variable.
1 parent 492eeea commit 41aa04a

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

yarnrunner_python/runner.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77

88
class YarnRunner(object):
9-
def __init__(self, compiled_yarn_f, names_csv_f, autostart=True) -> None:
9+
def __init__(self, compiled_yarn_f, names_csv_f, autostart=True, enable_tracing=False) -> None:
1010
self._compiled_yarn = YarnProgram()
1111
self._compiled_yarn.ParseFromString(compiled_yarn_f.read())
1212
self._names_csv = csv.DictReader(names_csv_f)
1313
self.__construct_string_lookup_table()
14+
self._enable_tracing = enable_tracing
1415

1516
self.visits = {key: 0 for key in self._compiled_yarn.nodes.keys()}
1617
self.variables = {}
@@ -58,6 +59,10 @@ def __find_label(self, label_key):
5859
raise Exception(
5960
f"The current node `{self.current_node}` does not have a label named `{label_key}")
6061

62+
def __debug_log(self, msg, **kwargs):
63+
if self._enable_tracing:
64+
print(msg, **kwargs)
65+
6166
def debug_vm(self):
6267
print(f"VM paused: {self.paused}")
6368
print(f"VM finished: {self.finished}")
@@ -122,10 +127,11 @@ def add_command_handler(self, command, fn):
122127
##### OpCode Implementations below here #####
123128

124129
def __jump_to(self, instruction):
125-
# print(f"Jump from {self._program_counter} ", end='')
130+
self.__debug_log(
131+
f"__jump_to: Jump from {self._program_counter} ", end='')
126132
self._program_counter = self.__find_label(
127133
instruction.operands[0].string_value)
128-
# print(f"to {self._program_counter}")
134+
self.__debug_log(f"to {self._program_counter}")
129135

130136
def __jump(self, _instruction):
131137
if len(self._vm_data_stack) < 1 or type(self._vm_data_stack[0]) != str:
@@ -144,6 +150,8 @@ def __go_to_node(self, node_key):
144150

145151
self.current_node = node_key
146152
self.visits[node_key] += 1
153+
self.__debug_log(
154+
f"__go_to_node: visits[{node_key}] = {self.visits[node_key]}")
147155
self._vm_instruction_stack = (
148156
self._compiled_yarn.nodes[node_key].instructions)
149157
self._program_counter = 0
@@ -216,10 +224,13 @@ def __push_variable(self, instruction):
216224
match = re.search(r"\$visits_([a-zA-Z\_0-9]+)", variable_name)
217225
if match:
218226
node_name = match.group(1)
219-
if node_name not in self.visits:
227+
visits_lookup = {node_key.replace(".", "_"): v for (
228+
node_key, v) in self.visits.items()}
229+
230+
if node_name not in visits_lookup:
220231
visits = 0
221232
else:
222-
visits = self.visits[node_name]
233+
visits = visits_lookup[node_name]
223234
self._vm_data_stack.insert(0, visits)
224235
return
225236

0 commit comments

Comments
 (0)