Added runner
parent
9ed159dee9
commit
c671fc6f9a
|
@ -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)
|
Reference in New Issue