This commit is contained in:
Michael Zhang 2021-10-01 12:33:51 -05:00
parent 0fea420749
commit ceaa9de8c5
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
8 changed files with 50 additions and 20 deletions

4
.gitignore vendored
View file

@ -3,3 +3,7 @@ __pycache__
.vim
gen/*
!gen/__init__.py
build
dist
*.egg-info

View file

@ -6,5 +6,5 @@ doc:
run:
mypy *.py
python agmain.py
python agtest
mypy gen/*.py

5
agtest/__main__.py Normal file
View file

@ -0,0 +1,5 @@
import sys
import agtest.driver
if __name__ == "__main__":
agtest.driver.run(sys.argv[1])

View file

@ -1,5 +1,5 @@
from typing import *
from lark import Transformer, Tree, Token
import lark
import re
from re import Pattern
@ -190,7 +190,7 @@ class ExprName(Expr):
return f"{self.name}"
class Parser(Transformer[List[Decl]]):
class Parser(lark.Transformer[List[Decl]]):
def program(self, items: List[Decl]) -> List[Decl]:
return items
@ -269,8 +269,8 @@ class Parser(Transformer[List[Decl]]):
def expr_name(self, items: List[str]) -> Expr:
return ExprName(items[0])
def sep_trail(self, items: List[Tree]) -> List[T]:
def sep_trail(self, items: List[lark.Tree]) -> List[T]:
return list(map(lambda it: cast(T, it), items))
def ident(self, items: List[Token]) -> str:
def ident(self, items: List[lark.Token]) -> str:
return cast(str, items[0].value)

View file

@ -1,19 +1,18 @@
import textwrap
import lark
import importlib
import json
import os
from os import path
import json
import importlib
from lark import Lark
import sys
from pathlib import Path
from agtest.ast import *
from agtest.gen import *
from agtest.ast import Parser
from agtest.gen import GenResult
grammar = path.join(path.dirname(path.realpath(__file__)), "grammar.lark")
p = Lark(open(grammar).read(), start="program", parser="lalr")
p = lark.Lark(open(grammar).read(), start="program", parser="lalr")
if __name__ == "__main__":
with open("test/arith.ag") as f:
def run(path: Path):
with open(path) as f:
data = f.read()
cst = p.parse(data)
@ -21,11 +20,13 @@ if __name__ == "__main__":
trans = Parser()
ast = trans.transform(cst)
res = gen(ast)
res = GenResult(ast)
res.build()
print("Grammar:")
print(res.parser_data)
if not os.path.exists("gen"):
if not path.exists("gen"):
os.makedirs("gen")
with open("agruntime.tmpl.py", "r") as f:
@ -37,7 +38,7 @@ if __name__ == "__main__":
pd=res.parser_data,
ex=res.extra,
starts=list(res.starts),
transdef=res.transdef,
trans_def=res.trans_def,
ntmap=json.dumps(res.nonterminal_map),
)
)

View file

@ -43,7 +43,7 @@ class GenResult:
self.nonterminal_map: Dict[str, str] = dict()
@property
def transdef(self) -> str:
def trans_def(self) -> str:
s = []
for name, rules in self.parse_rules.items():
n = name.lstrip("?")
@ -78,7 +78,7 @@ class GenResult:
return "\n".join(s)
def gen(self) -> None:
def build(self) -> None:
def v(name: str) -> str:
return f"__ag_{name}"

View file

@ -12,6 +12,14 @@ agtest.gen module
:undoc-members:
:show-inheritance:
agtest.driver module
--------------------
.. automodule:: agtest.driver
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------

12
setup.py Normal file
View file

@ -0,0 +1,12 @@
from setuptools import setup
setup(
name="agtest",
version="0.1.0",
packages=["agtest"],
entry_points={
'console_scripts': [
'agt = agtest.driver.run',
],
},
)