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 argparse
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
|
||||||
|
|
||||||
from flowmetal.syntax_analyzer import analyzes
|
from flowmetal.syntax_analyzer import analyzes
|
||||||
|
|
||||||
|
@ -25,20 +24,42 @@ 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()
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""REPL entry point."""
|
"""REPL entry point."""
|
||||||
|
|
||||||
args = parser.parse_args(sys.argv[1:])
|
args = parser.parse_args(sys.argv[1:])
|
||||||
logger = logging.getLogger("arrdem.datalog")
|
logger = logging.getLogger("flowmetal")
|
||||||
ch = logging.StreamHandler()
|
ch = logging.StreamHandler()
|
||||||
ch.setLevel(logging.INFO)
|
ch.setLevel(logging.INFO)
|
||||||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||||
ch.setFormatter(formatter)
|
ch.setFormatter(formatter)
|
||||||
logger.addHandler(ch)
|
logger.addHandler(ch)
|
||||||
|
|
||||||
session = PromptSession(history=FileHistory(".datalog.history"))
|
session = PromptSession(history=FileHistory(".iflow.history"))
|
||||||
line_no = 0
|
line_no = 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
@ -50,7 +71,7 @@ def main():
|
||||||
break
|
break
|
||||||
|
|
||||||
try:
|
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:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
finally:
|
finally:
|
||||||
|
|
Loading…
Reference in a new issue