This commit is contained in:
Michael Zhang 2021-09-30 16:07:36 -05:00
parent 18df271749
commit 61bfd16b81
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 236 additions and 216 deletions

View file

@ -1,6 +1,9 @@
watch:
watchexec -ce py,lark,ag -i gen 'just run'
fmt:
pipenv run black .
run:
mypy *.py
python agmain.py

View file

@ -18,6 +18,14 @@ class GenResult:
self.parse_rules: defaultdict[str, List[str]] = defaultdict(list)
self.starts: Set[str] = set()
self.extra = ex
self.trans: List[str] = list()
@property
def transdef(self) -> str:
s = self.trans
if not s:
s = ["pass"]
return "\n" + "\n".join(map(lambda c: f" {c}", s))
@property
def parser_data(self) -> str:

View file

@ -25,6 +25,8 @@ if __name__ == "__main__":
with open("gen/arith.py", "w") as f:
fmt_str = textwrap.dedent(
"""
# This documented generated by agtest.
__all__ = ["parse"]
from typing import Generic, TypeVar, Optional, Callable, Dict, Any
from lark import Lark, Transformer
@ -43,15 +45,22 @@ if __name__ == "__main__":
self.value = self.func()
return self.value
parser = Lark('''{pd}''', parser='lalr', start={starts}, debug=True)
class Trans(Transformer[None]):
pass
class Trans(Transformer[None]): {transdef}
{ex}
def parse(input: str, start: Optional[str] = None) -> Any:
return parser.parse(input, start)
tree = parser.parse(input, start)
trans = Trans()
res = trans.transform(tree)
return res
"""
)
f.write(
fmt_str.format(pd=res.parser_data, ex=res.extra, starts=list(res.starts))
fmt_str.format(
pd=res.parser_data,
ex=res.extra,
starts=list(res.starts),
transdef=res.transdef,
)
)
mod = importlib.import_module("gen.arith")