Arm the REPL with a customized tree printer
This commit is contained in:
parent
72471195c9
commit
f79ff28b9a
1 changed files with 26 additions and 5 deletions
|
@ -3,7 +3,6 @@
|
|||
import argparse
|
||||
import logging
|
||||
import sys
|
||||
import pprint
|
||||
|
||||
from flowmetal.syntax_analyzer import analyzes
|
||||
|
||||
|
@ -22,23 +21,45 @@ STYLE = Style.from_dict({
|
|||
|
||||
|
||||
class InterpreterInterrupt(Exception):
|
||||
"""An exception used to break the prompt or evaluation."""
|
||||
"""An exception used to break the prompt or evaluation."""
|
||||
|
||||
|
||||
def pp(t, indent=""):
|
||||
if isinstance(t, list): # lists
|
||||
buff = ["["]
|
||||
for e in t:
|
||||
buff.append(f"{indent} " + pp(e, indent+" ")+",")
|
||||
return "\n".join(buff + [f"{indent}]"])
|
||||
|
||||
elif hasattr(t, '_fields'): # namedtuples
|
||||
buff = [f"{type(t).__name__}("]
|
||||
for field, value in zip(t._fields, t):
|
||||
buff.append(f"{indent} {field}=" + pp(value, indent+" ")+",")
|
||||
return "\n".join(buff + [f"{indent})"])
|
||||
|
||||
elif isinstance(t, tuple): # tuples
|
||||
buff = ["("]
|
||||
for e in t:
|
||||
buff.append(f"{indent} " + pp(e, indent+" ")+",")
|
||||
return "\n".join(buff + [f"{indent})"])
|
||||
|
||||
else:
|
||||
return repr(t)
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
def main():
|
||||
"""REPL entry point."""
|
||||
|
||||
args = parser.parse_args(sys.argv[1:])
|
||||
logger = logging.getLogger("arrdem.datalog")
|
||||
logger = logging.getLogger("flowmetal")
|
||||
ch = logging.StreamHandler()
|
||||
ch.setLevel(logging.INFO)
|
||||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||
ch.setFormatter(formatter)
|
||||
logger.addHandler(ch)
|
||||
|
||||
session = PromptSession(history=FileHistory(".datalog.history"))
|
||||
session = PromptSession(history=FileHistory(".iflow.history"))
|
||||
line_no = 0
|
||||
|
||||
while True:
|
||||
|
@ -50,7 +71,7 @@ def main():
|
|||
break
|
||||
|
||||
try:
|
||||
pprint.pprint(analyzes(line, source_name=f"repl@{line_no}"))
|
||||
print(pp(analyzes(line, source_name=f"repl@{line_no}")))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
finally:
|
||||
|
|
Loading…
Reference in a new issue