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