Support multiple python versions for code generation
This commit is contained in:
parent
50e23d8a13
commit
bf99bef291
@ -1,3 +1,4 @@
|
||||
from typing import Dict
|
||||
from codegen.datatypes import CDataType
|
||||
from codegen.dbl_list.make_dbl_list import DblListData, make_dbl_list
|
||||
|
||||
@ -7,7 +8,7 @@ def main():
|
||||
|
||||
|
||||
def gen_dbl_list():
|
||||
datatypes: dict[CDataType, DblListData] = {}
|
||||
datatypes: Dict[CDataType, DblListData] = {}
|
||||
make_dbl_list(datatypes)
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from typing import Optional, Union, List
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
@ -70,7 +70,7 @@ class CEnumVal:
|
||||
@dataclass
|
||||
class CEnum:
|
||||
name: str
|
||||
values: list[CEnumVal]
|
||||
values: List[CEnumVal]
|
||||
typedef: bool = False
|
||||
|
||||
def __str__(self) -> str:
|
||||
@ -100,8 +100,8 @@ class CMacro:
|
||||
@dataclass
|
||||
class CStruct:
|
||||
name: str
|
||||
cargs: list["CArg"]
|
||||
typedef_name: str | None = None
|
||||
cargs: List["CArg"]
|
||||
typedef_name: Optional[str] = None
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.declare() + self.define()
|
||||
@ -145,10 +145,10 @@ class CArg:
|
||||
class CFunc:
|
||||
name: str
|
||||
ret_type: CDataType
|
||||
args: list[CArg]
|
||||
args: List[CArg]
|
||||
body: str
|
||||
pointer: CPointer = field(default_factory=CPointer)
|
||||
qualifiers: list[CQualifier] = field(default_factory=list)
|
||||
qualifiers: List[CQualifier] = field(default_factory=list)
|
||||
|
||||
def __str__(self) -> str:
|
||||
qualifiers = ""
|
||||
@ -203,8 +203,8 @@ class CInclude:
|
||||
class CFile:
|
||||
name: str
|
||||
extension: str
|
||||
decl_types: list[CStruct] = field(default_factory=list)
|
||||
macros: list[CMacro] = field(default_factory=list)
|
||||
decl_types: List[CStruct] = field(default_factory=list)
|
||||
macros: List[CMacro] = field(default_factory=list)
|
||||
|
||||
def save(self, output_dir: Path):
|
||||
output_file = output_dir / f"{self.name}.{self.extension}"
|
||||
@ -223,9 +223,9 @@ class CFile:
|
||||
@dataclass
|
||||
class CHeader(CFile):
|
||||
extension: str = "h"
|
||||
includes: list[CInclude] = field(default_factory=list)
|
||||
types: list[CUserType] = field(default_factory=list)
|
||||
funcs: list[CFunc] = field(default_factory=list)
|
||||
includes: List[CInclude] = field(default_factory=list)
|
||||
types: List[CUserType] = field(default_factory=list)
|
||||
funcs: List[CFunc] = field(default_factory=list)
|
||||
|
||||
def __str__(self) -> str:
|
||||
name_upper = self.name.upper()
|
||||
@ -275,10 +275,10 @@ class CHeader(CFile):
|
||||
@dataclass
|
||||
class CSource(CFile):
|
||||
extension: str = "c"
|
||||
includes: list[CInclude] = field(default_factory=list)
|
||||
types: list[CUserType] = field(default_factory=list)
|
||||
internal_funcs: list[CFunc] = field(default_factory=list)
|
||||
funcs: list[CFunc] = field(default_factory=list)
|
||||
includes: List[CInclude] = field(default_factory=list)
|
||||
types: List[CUserType] = field(default_factory=list)
|
||||
internal_funcs: List[CFunc] = field(default_factory=list)
|
||||
funcs: List[CFunc] = field(default_factory=list)
|
||||
|
||||
def __str__(self) -> str:
|
||||
includes = _get_includes_string(self.includes)
|
||||
@ -333,7 +333,7 @@ def get_datatype_string(_type: CDataType) -> str:
|
||||
return _type
|
||||
|
||||
|
||||
def _get_includes_string(includes: list[CInclude]) -> str:
|
||||
def _get_includes_string(includes: List[CInclude]) -> str:
|
||||
output = ""
|
||||
for include in sorted(includes, key=lambda inc: inc.local, reverse=True):
|
||||
output += str(include)
|
||||
|
@ -1,7 +1,8 @@
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Dict
|
||||
from codegen.constants import WAPP_SRC_ROOT
|
||||
from codegen.utils import load_func_body_from_file
|
||||
from codegen.utils import load_func_body_from_file, convert_to_relative
|
||||
from codegen.datatypes import (
|
||||
CDataType,
|
||||
CMacro,
|
||||
@ -23,11 +24,11 @@ from codegen.datatypes import (
|
||||
class DblListData:
|
||||
node_typename: str
|
||||
list_typename: str
|
||||
hdr_decl_types: list[CStruct] = field(default_factory=list)
|
||||
src_decl_types: list[CStruct] = field(default_factory=list)
|
||||
hdr_decl_types: List[CStruct] = field(default_factory=list)
|
||||
src_decl_types: List[CStruct] = field(default_factory=list)
|
||||
|
||||
|
||||
def make_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
|
||||
def make_dbl_list(user_datatypes: Dict[CDataType, DblListData] = {}):
|
||||
def __format_func_body(
|
||||
filename: Path,
|
||||
type_string: str,
|
||||
@ -50,18 +51,18 @@ def make_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
|
||||
common_local_include_files = [
|
||||
(WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h")
|
||||
]
|
||||
common_includes: list[CInclude] = [
|
||||
common_includes: List[CInclude] = [
|
||||
CInclude(header="stdbool.h")
|
||||
]
|
||||
for local_file in common_local_include_files:
|
||||
common_includes.append(
|
||||
CInclude(
|
||||
header=str(local_file.relative_to(out_dir, walk_up=True)),
|
||||
header=str(convert_to_relative(local_file, out_dir)),
|
||||
local=True,
|
||||
)
|
||||
)
|
||||
|
||||
common_decl_types: list[CStruct] = []
|
||||
common_decl_types: List[CStruct] = []
|
||||
|
||||
datatypes: dict[CDataType, DblListData] = {
|
||||
"Str8": DblListData(
|
||||
|
@ -1,6 +1,18 @@
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def load_func_body_from_file(filename: Path) -> str:
|
||||
with open(filename, "r") as infile:
|
||||
return infile.read().rstrip()
|
||||
|
||||
|
||||
def convert_to_relative(path: Path, target: Path) -> Path:
|
||||
major = sys.version_info.major
|
||||
minor = sys.version_info.minor
|
||||
|
||||
if major >= 3 and minor >= 12:
|
||||
return path.relative_to(target, walk_up=True)
|
||||
else:
|
||||
return Path(os.path.relpath(str(path), start=str(target)))
|
Loading…
x
Reference in New Issue
Block a user