get rid of terminal aliases
This commit is contained in:
parent
5c09d658b9
commit
d91eb65d4d
6 changed files with 41 additions and 35 deletions
2
Justfile
2
Justfile
|
@ -1,2 +1,2 @@
|
||||||
watch:
|
watch:
|
||||||
watchexec -ce py -i gen 'mypy *.py && python gen.py && mypy gen/*.py'
|
watchexec -ce py,lark -i gen 'mypy *.py && python gen.py && mypy gen/*.py'
|
||||||
|
|
8
agast.py
8
agast.py
|
@ -85,16 +85,16 @@ class Parser(Transformer[List[Decl]]):
|
||||||
def node_ref_name(self, items: List[str]) -> NodeRefByName: return NodeRefByName(items[0])
|
def node_ref_name(self, items: List[str]) -> NodeRefByName: return NodeRefByName(items[0])
|
||||||
|
|
||||||
def expr_dot(self, items: List[Expr]) -> Expr:
|
def expr_dot(self, items: List[Expr]) -> Expr:
|
||||||
[left, _, right] = items
|
[left, right] = items
|
||||||
return ExprDot(left, right)
|
return ExprDot(left, right)
|
||||||
def expr_add(self, items: List[Expr]) -> Expr:
|
def expr_add(self, items: List[Expr]) -> Expr:
|
||||||
[left, _, right] = items
|
[left, right] = items
|
||||||
return ExprAdd(left, right)
|
return ExprAdd(left, right)
|
||||||
def expr_mul(self, items: List[Expr]) -> Expr:
|
def expr_mul(self, items: List[Expr]) -> Expr:
|
||||||
[left, _, right] = items
|
[left, right] = items
|
||||||
return ExprMul(left, right)
|
return ExprMul(left, right)
|
||||||
def expr_call(self, items: List[Expr]) -> Expr:
|
def expr_call(self, items: List[Expr]) -> Expr:
|
||||||
[func, _, args, _] = items
|
[func, args] = items
|
||||||
return ExprMul(func, args)
|
return ExprMul(func, args)
|
||||||
|
|
||||||
def sep_trail(self, items: List[Tree]) -> List[T]:
|
def sep_trail(self, items: List[Tree]) -> List[T]:
|
||||||
|
|
14
agtypeck.py
14
agtypeck.py
|
@ -1,7 +1,12 @@
|
||||||
from typing import *
|
from typing import *
|
||||||
from agast import *
|
from agast import *
|
||||||
|
|
||||||
def typecheck(program: List[Decl]) -> None:
|
class TypecheckResult:
|
||||||
|
def __init__(self, pd: str):
|
||||||
|
self.parser_data = pd
|
||||||
|
|
||||||
|
|
||||||
|
def typecheck(program: List[Decl]) -> TypecheckResult:
|
||||||
i = 0
|
i = 0
|
||||||
def gen(name: str = "") -> str:
|
def gen(name: str = "") -> str:
|
||||||
return f"__ag{i:03}{name}"
|
return f"__ag{i:03}{name}"
|
||||||
|
@ -13,5 +18,12 @@ def typecheck(program: List[Decl]) -> None:
|
||||||
program)))
|
program)))
|
||||||
print(ifaces)
|
print(ifaces)
|
||||||
|
|
||||||
|
# a high-level dictionary of productions; this has sub-productions
|
||||||
|
# that should be further expanded at a later step before converting
|
||||||
|
# into lark code
|
||||||
|
productions_hi: Dict[str, Union[str, List[str]]] = dict()
|
||||||
|
|
||||||
for node in filter(lambda c: isinstance(c, Node), program):
|
for node in filter(lambda c: isinstance(c, Node), program):
|
||||||
print(node)
|
print(node)
|
||||||
|
|
||||||
|
return TypecheckResult("")
|
20
gen.py
20
gen.py
|
@ -18,18 +18,26 @@ if __name__ == "__main__":
|
||||||
ast = trans.transform(cst)
|
ast = trans.transform(cst)
|
||||||
print("ast", ast)
|
print("ast", ast)
|
||||||
|
|
||||||
typecheck(ast)
|
res = typecheck(ast)
|
||||||
|
|
||||||
parser_data = ""
|
|
||||||
|
|
||||||
if not os.path.exists("gen"):
|
if not os.path.exists("gen"):
|
||||||
os.makedirs("gen")
|
os.makedirs("gen")
|
||||||
with open("gen/arith.py", "w") as f:
|
with open("gen/arith.py", "w") as f:
|
||||||
f.write(textwrap.dedent(f"""
|
f.write(textwrap.dedent(f"""
|
||||||
from typing import Generic, TypeVar
|
from typing import Generic, TypeVar, Optional, Callable
|
||||||
from lark import Lark
|
from lark import Lark, Transformer
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
class Thunk(Generic[T]):
|
class Thunk(Generic[T]):
|
||||||
|
''' A thunk represents a value that may be computed lazily. '''
|
||||||
|
value: Optional[T]
|
||||||
|
def __init__(self, func: Callable[[], T]):
|
||||||
|
self.func = func
|
||||||
|
self.value = None
|
||||||
|
def get(self) -> T:
|
||||||
|
if self.value is None:
|
||||||
|
self.value = self.func()
|
||||||
|
return self.value
|
||||||
|
parser = Lark('''{res.parser_data}''')
|
||||||
|
class Trans(Transformer[None]):
|
||||||
pass
|
pass
|
||||||
parser = Lark('''{parser_data}''')
|
|
||||||
"""))
|
"""))
|
||||||
|
|
30
grammar.lark
30
grammar.lark
|
@ -8,7 +8,7 @@ sep_trail{item, punc}: item (punc item)? punc?
|
||||||
iface: "iface" ident "{" sep_trail{iface_field, ","} "}"
|
iface: "iface" ident "{" sep_trail{iface_field, ","} "}"
|
||||||
iface_field: ident ":" ident
|
iface_field: ident ":" ident
|
||||||
|
|
||||||
node: NODE ident ":" ident "{" variant* "}"
|
node: "node" ident ":" ident "{" variant* "}"
|
||||||
variant: prod "=>" "{" equation_* "}"
|
variant: prod "=>" "{" equation_* "}"
|
||||||
prod: sym*
|
prod: sym*
|
||||||
sym: sym_rename
|
sym: sym_rename
|
||||||
|
@ -17,37 +17,23 @@ sym_rename: "<" ident ":" node_ref ">"
|
||||||
node_ref: node_ref_name
|
node_ref: node_ref_name
|
||||||
| STRING
|
| STRING
|
||||||
node_ref_name: ident
|
node_ref_name: ident
|
||||||
equation_: equation SEMI
|
equation_: equation ";"
|
||||||
equation: expr EQ expr
|
equation: expr "=" expr
|
||||||
|
|
||||||
expr: expr_dot
|
expr: expr_dot
|
||||||
| expr_add
|
| expr_add
|
||||||
| expr_mul
|
| expr_mul
|
||||||
| expr_call
|
| expr_call
|
||||||
| expr_name
|
| expr_name
|
||||||
expr_dot: expr DOT expr
|
expr_dot: expr "." expr
|
||||||
expr_add: expr ADD expr
|
expr_add: expr "+" expr
|
||||||
expr_mul: expr MUL expr
|
expr_mul: expr "*" expr
|
||||||
expr_call: expr LPAR args RPAR
|
expr_call: expr "(" args ")"
|
||||||
expr_name: ident
|
expr_name: ident
|
||||||
args: expr (COMMA expr)? COMMA?
|
args: sep_trail{expr, ","}
|
||||||
|
|
||||||
ident: IDENT
|
ident: IDENT
|
||||||
IDENT: /([a-zA-Z][a-zA-Z0-9_]*)|(_[a-zA-Z0-9_]+)/
|
IDENT: /([a-zA-Z][a-zA-Z0-9_]*)|(_[a-zA-Z0-9_]+)/
|
||||||
IFACE: "iface"
|
|
||||||
NODE: "node"
|
|
||||||
LANG: "<"
|
|
||||||
RANG: ">"
|
|
||||||
ADD: "+"
|
|
||||||
MUL: "*"
|
|
||||||
COMMA: ","
|
|
||||||
COLON: ":"
|
|
||||||
LPAR: "("
|
|
||||||
RPAR: ")"
|
|
||||||
EQ: "="
|
|
||||||
DOT: "."
|
|
||||||
SEMI: ";"
|
|
||||||
THICCARROW: "=>"
|
|
||||||
|
|
||||||
%import python.STRING
|
%import python.STRING
|
||||||
%import common.WS
|
%import common.WS
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
watchexec --shell=powershell -ce py -i gen './run.ps1'
|
watchexec --shell=powershell -ce py,lark -i gen './run.ps1'
|
||||||
|
|
Loading…
Reference in a new issue