Added runner

master
Mark 2022-10-21 14:40:49 -07:00
parent 9ed159dee9
commit c671fc6f9a
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 46 additions and 0 deletions

46
runner.py Normal file
View File

@ -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)