@@ -58,8 +58,8 @@ def parse_manifest_from_header(header_path: Path) -> Dict:
58
58
manifest_data [key ] = [{k : v } for k , v in val .items ()]
59
59
elif isinstance (val , list ):
60
60
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
63
63
for k , v in (item .items () if isinstance (item , dict ) else [(item , "" )])
64
64
]
65
65
elif val is None :
@@ -72,26 +72,29 @@ def parse_manifest_from_header(header_path: Path) -> Dict:
72
72
print (f"[INFO] Successfully parsed manifest for { header_path .stem } " )
73
73
return manifest_data
74
74
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 :
76
76
"""
77
77
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} .
79
79
"""
80
80
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 ()) + '}'
84
83
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 ) + '}'
86
85
elif isinstance (value , bool ):
87
86
return "true" if value else "false"
88
87
elif isinstance (value , str ):
88
+ # @instance 变量
89
89
if value .startswith ('@' ) and re .match (r"^@[A-Za-z_][\w\d_]*$" , value ):
90
90
return value [1 :]
91
+ # C++ 枚举、作用域名等
91
92
if re .match (r"^[A-Za-z_][\w:<>\s,]*::[A-Za-z_][\w:]*$" , value ):
92
93
return value
94
+ # 纯数字
93
95
elif re .match (r"^-?\d+(\.\d+)?$" , value ):
94
96
return value
97
+ # 普通字符串
95
98
else :
96
99
return f'"{ value } "'
97
100
else :
@@ -225,7 +228,7 @@ def generate_xrobot_main_code(hw_var: str, modules: List[str], config: Dict) ->
225
228
226
229
args_dict = entry .get ("constructor_args" , {})
227
230
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 ()]
229
232
else :
230
233
args_list = []
231
234
@@ -259,7 +262,7 @@ def auto_discover_modules(modules_dir: Path = Path("Modules")) -> List[str]:
259
262
Returns a list of module names that have their .hpp files.
260
263
"""
261
264
discovered_modules = [
262
- sub .name for sub in modules_dir .iterdir ()
265
+ sub .name for sub in modules_dir .iterdir ()
263
266
if sub .is_dir () and (sub / f"{ sub .name } .hpp" ).exists ()
264
267
]
265
268
if not discovered_modules :
0 commit comments