agtest/agmain.py

41 lines
966 B
Python
Raw Normal View History

2021-06-09 00:56:30 +00:00
import textwrap
import os
2021-09-30 22:49:26 +00:00
import json
2021-06-09 07:44:52 +00:00
import importlib
2021-06-09 00:56:30 +00:00
from lark import Lark
from agast import *
2021-06-13 23:28:28 +00:00
from aggen import *
2021-06-09 05:10:45 +00:00
p = Lark(open("grammar.lark").read(), start="program", parser="lalr")
2021-06-09 00:56:30 +00:00
if __name__ == "__main__":
2021-09-30 21:37:43 +00:00
with open("test/arith.ag") as f:
2021-06-14 20:52:53 +00:00
data = f.read()
cst = p.parse(data)
trans = Parser()
ast = trans.transform(cst)
res = gen(ast)
2021-09-30 22:39:08 +00:00
print("Grammar:")
print(res.parser_data)
2021-06-14 20:52:53 +00:00
if not os.path.exists("gen"):
os.makedirs("gen")
with open("gen/arith.py", "w") as f:
2021-09-30 22:53:47 +00:00
fmt_str = open("agruntime.tmpl.py", "r").read()
2021-09-30 20:59:03 +00:00
f.write(
2021-09-30 21:07:36 +00:00
fmt_str.format(
pd=res.parser_data,
ex=res.extra,
starts=list(res.starts),
transdef=res.transdef,
2021-09-30 22:49:26 +00:00
ntmap=json.dumps(res.nonterminal_map),
2021-09-30 21:07:36 +00:00
)
2021-09-30 20:59:03 +00:00
)
2021-06-14 20:52:53 +00:00
mod = importlib.import_module("gen.arith")
2021-09-30 22:49:26 +00:00
print(mod.parse("1 + 2 * 3", start="Expr")) # type: ignore