[tools] Allow struct types in definitions

Allow struct type names in definitions, which result in struct buffer
pointers in generated code.
This commit is contained in:
Justin C. Miller
2022-10-06 23:15:20 -07:00
parent 6b20f1fb19
commit d04b2ae315
7 changed files with 42 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@@ -126,6 +126,11 @@ class DefTransformer(Transformer):
from .types import ObjectRef
return ObjectRef(n, self.filename)
@v_args(inline=True)
def struct_name(self, n):
from .types import Struct
return Struct(n)
def PRIMITIVE(self, s):
from .types import get_primitive
return get_primitive(s)

View File

@@ -24,3 +24,4 @@ from .function import Function, Method, Param
from .type import Type
from .primitive import get_primitive
from .objref import ObjectRef
from .struct import Struct

View File

@@ -0,0 +1,17 @@
from .type import Type
class Struct(Type):
def __repr__(self):
return f'Struct({self.name})'
def c_names(self, options):
one = f"struct j6_{self.name} *"
two = "size_t"
out = bool({"out", "inout"}.intersection(options))
if out:
two += " *"
return ((one, ""), (two, "_size"))
def cxx_names(self, options):
return self.c_names(options)

4
scripts/generate_parser.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env bash
python3 -m lark.tools.standalone assets/grammars/definitions.g > scripts/definitions/parser.py
python3 scripts/test_parse_definitions.py

View File

@@ -0,0 +1,10 @@
import sys
from pathlib import Path
from definitions.context import Context
root = Path(sys.argv[0]).parent.parent
defs_path = (root / "definitions").resolve()
print(f"Definitions: {defs_path}")
ctx = Context(str(defs_path))
ctx.parse("syscalls.def")