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 .vim
gen/* gen/*
!gen/__init__.py !gen/__init__.py
build
dist
*.egg-info

View file

@ -6,5 +6,5 @@ doc:
run: run:
mypy *.py mypy *.py
python agmain.py python agtest
mypy gen/*.py 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 typing import *
from lark import Transformer, Tree, Token import lark
import re import re
from re import Pattern from re import Pattern
@ -190,7 +190,7 @@ class ExprName(Expr):
return f"{self.name}" return f"{self.name}"
class Parser(Transformer[List[Decl]]): class Parser(lark.Transformer[List[Decl]]):
def program(self, items: List[Decl]) -> List[Decl]: def program(self, items: List[Decl]) -> List[Decl]:
return items return items
@ -269,8 +269,8 @@ class Parser(Transformer[List[Decl]]):
def expr_name(self, items: List[str]) -> Expr: def expr_name(self, items: List[str]) -> Expr:
return ExprName(items[0]) 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)) 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) return cast(str, items[0].value)

View file

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

View file

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

View file

@ -12,6 +12,14 @@ agtest.gen module
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
agtest.driver module
--------------------
.. automodule:: agtest.driver
:members:
:undoc-members:
:show-inheritance:
Module contents 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',
],
},
)