agtest/gen.py

35 lines
672 B
Python

import textwrap
import os
from lark import Lark
from agast import *
from agtypeck import *
p = Lark(open("grammar.lark").read(), start="program", parser="lalr")
if __name__ == "__main__":
with open("arith.ag") as f:
data = f.read()
cst = p.parse(data)
# print("cst", cst)
trans = Parser()
ast = trans.transform(cst)
print("ast", ast)
typecheck(ast)
parser_data = ""
if not os.path.exists("gen"):
os.makedirs("gen")
with open("gen/arith.py", "w") as f:
f.write(textwrap.dedent(f"""
from typing import Generic, TypeVar
from lark import Lark
T = TypeVar('T')
class Thunk(Generic[T]):
pass
parser = Lark('''{parser_data}''')
"""))