From c671fc6f9ad1cc414c40d89ff3e88c63172c05f8 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 21 Oct 2022 14:40:49 -0700 Subject: [PATCH] Added runner --- runner.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 runner.py diff --git a/runner.py b/runner.py new file mode 100644 index 0000000..dff6efe --- /dev/null +++ b/runner.py @@ -0,0 +1,46 @@ +import tokens +from parser import Parser + +class Runner: + def __init__(self): + self.macro_table = {} + self.expr = None + + # Apply a list of definitions + def run(self, line: str): + e = Parser.parse_line(line) + + if isinstance(e, tokens.macro_expression): + if e.label in self.macro_table: + raise NameError(f"Label {e.label} exists!") + e.exp.bind_variables() + self.macro_table[e.label] = e.exp + + elif isinstance(e, tokens.command): + pass + else: + e.bind_variables() + self.expr = e + + outs = [str(e)] + for i in range(300): + r = self.expr.reduce(self.macro_table) + self.expr = r.output + s = str(r.output) + p = s if len(s) < 100 else s[:97] + "..." + + #if s in outs: + #print(p) + #print("\nLoop detected, exiting.") + #break + + if not r.was_reduced: + print("\nCannot evaluate any further.") + break + + print(f"Performed {i} {'operations' if i != 1 else 'operation'}.") + return self.expr + + def run_lines(self, lines): + for l in lines: + self.run(l)