Skip to content

Commit 4b05094

Browse files
committed
fix(codegen): 支持嵌套结构体参数自动生成 C++ 初始化列表.
1 parent 3a2249a commit 4b05094

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/xrobot/GenerateMain.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def parse_manifest_from_header(header_path: Path) -> Dict:
5858
manifest_data[key] = [{k: v} for k, v in val.items()]
5959
elif isinstance(val, list):
6060
manifest_data[key] = [
61-
{k: v} if isinstance(item, dict) else {str(item): ""}
62-
for item in val
61+
{k: v} if isinstance(item, dict) else {str(item): ""}
62+
for item in val
6363
for k, v in (item.items() if isinstance(item, dict) else [(item, "")])
6464
]
6565
elif val is None:
@@ -72,26 +72,29 @@ def parse_manifest_from_header(header_path: Path) -> Dict:
7272
print(f"[INFO] Successfully parsed manifest for {header_path.stem}")
7373
return manifest_data
7474

75-
def _format_cpp_value(value: Union[dict, list, str, int, float, bool]) -> str:
75+
def _format_cpp_value(value: Union[dict, list, str, int, float, bool], key: str = "") -> str:
7676
"""
7777
Format a value as C++-compliant parameter.
78-
Supports @instance references, numbers, identifiers, and strings.
78+
Supports numbers, bool, identifiers, @instance, string, nested dict as {a,b,c}, and list as {a,b,c}.
7979
"""
8080
if isinstance(value, dict):
81-
# Single key:value
82-
k, v = list(value.items())[0]
83-
return _format_cpp_value(v)
81+
# 输出为聚合初始化列表,顺序由 yaml/OrderedDict 决定
82+
return '{' + ', '.join(_format_cpp_value(v) for v in value.values()) + '}'
8483
elif isinstance(value, list):
85-
return "{" + ", ".join(_format_cpp_value(v) for v in value) + "}"
84+
return '{' + ', '.join(_format_cpp_value(v) for v in value) + '}'
8685
elif isinstance(value, bool):
8786
return "true" if value else "false"
8887
elif isinstance(value, str):
88+
# @instance 变量
8989
if value.startswith('@') and re.match(r"^@[A-Za-z_][\w\d_]*$", value):
9090
return value[1:]
91+
# C++ 枚举、作用域名等
9192
if re.match(r"^[A-Za-z_][\w:<>\s,]*::[A-Za-z_][\w:]*$", value):
9293
return value
94+
# 纯数字
9395
elif re.match(r"^-?\d+(\.\d+)?$", value):
9496
return value
97+
# 普通字符串
9598
else:
9699
return f'"{value}"'
97100
else:
@@ -225,7 +228,7 @@ def generate_xrobot_main_code(hw_var: str, modules: List[str], config: Dict) ->
225228

226229
args_dict = entry.get("constructor_args", {})
227230
if isinstance(args_dict, dict):
228-
args_list = [_format_cpp_value(v) for k, v in args_dict.items()]
231+
args_list = [_format_cpp_value(v, k) for k, v in args_dict.items()]
229232
else:
230233
args_list = []
231234

@@ -259,7 +262,7 @@ def auto_discover_modules(modules_dir: Path = Path("Modules")) -> List[str]:
259262
Returns a list of module names that have their .hpp files.
260263
"""
261264
discovered_modules = [
262-
sub.name for sub in modules_dir.iterdir()
265+
sub.name for sub in modules_dir.iterdir()
263266
if sub.is_dir() and (sub / f"{sub.name}.hpp").exists()
264267
]
265268
if not discovered_modules:

0 commit comments

Comments
 (0)