39 lines
748 B
Python
39 lines
748 B
Python
# fx-92 Scientifique Collège+ language interpreter: AST printer
|
|
|
|
from ast import N, Node
|
|
__all__ = ["print_ast"]
|
|
|
|
#---
|
|
# Message definitions
|
|
#---
|
|
|
|
class MessageFrench:
|
|
multiply = "mul({})"
|
|
goto = "goto {}, {}"
|
|
|
|
class MessageEnglish:
|
|
pass
|
|
|
|
#---
|
|
# Printer
|
|
#---
|
|
|
|
def print_ast(n, lang="en", indent=0):
|
|
if lang == "fr": lang = MessageFrench
|
|
if lang == "en": lang = MessageEnglish
|
|
|
|
print(" " * indent, end="")
|
|
|
|
if not isinstance(n, Node):
|
|
print(f"{type(n)}({n})")
|
|
return
|
|
|
|
if n.type == N.CONST:
|
|
print(n.args[0])
|
|
elif n.type == N.VAR:
|
|
print(f"VAR({n.args[0]})")
|
|
else:
|
|
print(f"{n.type.name}")
|
|
for arg in n.args:
|
|
print_ast(arg, lang=lang, indent=indent+2)
|