l0n0lc 0.8.4__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. l0n0lc/Py/350/275/254Cpp/350/275/254/350/257/221/345/231/250.py +579 -0
  2. l0n0lc/__init__.py +75 -6
  3. l0n0lc/aot/347/274/226/350/257/221.py +679 -0
  4. l0n0lc/ast/350/256/277/351/227/256/350/200/205.py +599 -0
  5. l0n0lc/cpp/347/261/273/345/236/213.py +330 -0
  6. l0n0lc/cpp/347/274/226/350/257/221/345/231/250.py +317 -0
  7. l0n0lc/simd/344/274/230/345/214/226.py +260 -0
  8. l0n0lc/std_map.py +153 -0
  9. l0n0lc/std_set.py +185 -0
  10. l0n0lc/std_vector.py +96 -0
  11. l0n0lc//344/273/243/347/240/201/344/274/230/345/214/226.py +302 -0
  12. l0n0lc//344/273/243/347/240/201/347/224/237/346/210/220.py +546 -0
  13. l0n0lc//344/276/235/350/265/226/346/263/250/345/205/245.py +155 -0
  14. l0n0lc//345/215/263/346/227/266/347/274/226/350/257/221.py +192 -0
  15. l0n0lc//345/217/230/351/207/217/347/256/241/347/220/206/345/231/250.py +123 -0
  16. l0n0lc//345/237/272/347/241/200/346/230/240/345/260/204.py +103 -0
  17. l0n0lc//345/237/272/347/241/200/346/267/267/345/205/245.py +147 -0
  18. l0n0lc//345/256/271/345/231/250/346/236/204/345/273/272/345/231/250.py +214 -0
  19. l0n0lc//345/267/245/345/205/267.py +285 -0
  20. l0n0lc//345/271/266/350/241/214/347/274/226/350/257/221/345/231/250.py +412 -0
  21. l0n0lc//345/274/202/345/270/270.py +474 -0
  22. l0n0lc//346/225/260/347/273/204/345/257/271/350/261/241/346/261/240.py +248 -0
  23. l0n0lc//346/226/207/344/273/266/347/256/241/347/220/206/345/231/250.py +286 -0
  24. l0n0lc//346/227/245/345/277/227/345/267/245/345/205/267.py +152 -0
  25. l0n0lc//347/261/273/345/236/213/346/216/250/346/226/255/345/267/245/345/205/267.py +352 -0
  26. l0n0lc//347/261/273/345/236/213/350/275/254/346/215/242.py +210 -0
  27. l0n0lc//347/261/273/346/224/257/346/214/201.py +372 -0
  28. l0n0lc//347/274/226/350/257/221/344/270/212/344/270/213/346/226/207.py +132 -0
  29. l0n0lc//347/274/226/350/257/221/347/256/241/347/220/206/345/231/250.py +171 -0
  30. l0n0lc//350/241/250/350/276/276/345/274/217/345/244/204/347/220/206.py +462 -0
  31. l0n0lc//350/275/254/350/257/221/345/231/250/345/267/245/345/205/267.py +49 -0
  32. l0n0lc//350/277/220/350/241/214/346/227/266/345/212/240/350/275/275.py +217 -0
  33. l0n0lc//351/200/232/347/224/250/345/267/245/345/205/267.py +149 -0
  34. l0n0lc-1.0.0.dist-info/METADATA +363 -0
  35. l0n0lc-1.0.0.dist-info/RECORD +39 -0
  36. {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/WHEEL +1 -1
  37. l0n0lc-1.0.0.dist-info/entry_points.txt +2 -0
  38. {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/licenses/LICENSE +0 -0
  39. l0n0lc/StdList.py +0 -24
  40. l0n0lc/StdMap.py +0 -21
  41. l0n0lc/c/345/237/272/347/241/200/345/244/204/347/220/206.py +0 -207
  42. l0n0lc/jit.py +0 -604
  43. l0n0lc//347/274/226/350/257/221.py +0 -58
  44. l0n0lc//351/200/232/347/224/250.py +0 -134
  45. l0n0lc-0.8.4.dist-info/METADATA +0 -241
  46. l0n0lc-0.8.4.dist-info/RECORD +0 -12
  47. {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,599 @@
1
+ import ast
2
+ from typing import Any, Union
3
+ from .cpp类型 import *
4
+ from .容器构建器 import 容器构建器
5
+ from .异常 import Jit错误
6
+ from .std_map import 标准无序映射
7
+ from .std_set import 标准集合
8
+ from .基础混入 import 错误处理混入, 参数处理混入, 类型处理混入
9
+
10
+ class AST访问者基类(错误处理混入, 参数处理混入, 类型处理混入):
11
+ """AST节点访问的基类,提供基础的访问方法"""
12
+
13
+ def __init__(self, transpiler):
14
+ self.transpiler = transpiler
15
+
16
+ def 计算比较(self, node: ast.Compare) -> Any:
17
+ """处理比较运算 (==, !=, <, >, <=, >=, in, not in)"""
18
+ # 调用transpiler的获取值方法
19
+ left = self.transpiler.获取值(node.left)
20
+ comparisons = []
21
+
22
+ curr_left = left
23
+
24
+ for op, comp in zip(node.ops, node.comparators):
25
+ curr_right = self.transpiler.获取值(comp)
26
+
27
+ op_str = ""
28
+ if isinstance(op, ast.Eq):
29
+ op_str = "=="
30
+ elif isinstance(op, ast.NotEq):
31
+ op_str = "!="
32
+ elif isinstance(op, ast.Lt):
33
+ op_str = "<"
34
+ elif isinstance(op, ast.LtE):
35
+ op_str = "<="
36
+ elif isinstance(op, ast.Gt):
37
+ op_str = ">"
38
+ elif isinstance(op, ast.GtE):
39
+ op_str = ">="
40
+
41
+ if op_str:
42
+ comparisons.append(f"({curr_left} {op_str} {curr_right})")
43
+ elif isinstance(op, (ast.In, ast.NotIn)):
44
+ # Check optimized contains
45
+ contains_expr = None
46
+ # Try to use __contains__ if available on the wrapper object
47
+ if hasattr(curr_right, "__contains__"):
48
+ try:
49
+ contains_expr = curr_right.__contains__(curr_left)
50
+ except (AttributeError, TypeError, ValueError):
51
+ pass
52
+ except KeyboardInterrupt:
53
+ raise
54
+
55
+ if not contains_expr:
56
+ # Generic std::find fallback
57
+ self.transpiler.包含头文件.add("<algorithm>")
58
+ self.transpiler.包含头文件.add("<iterator>")
59
+ contains_expr = f"(std::find(std::begin({curr_right}), std::end({curr_right}), {curr_left}) != std::end({curr_right}))"
60
+
61
+ if isinstance(op, ast.In):
62
+ comparisons.append(f"({contains_expr})")
63
+ else:
64
+ comparisons.append(f"!({contains_expr})")
65
+ else:
66
+ self.抛出错误(
67
+ f"Unsupported comparison operator: {type(op).__name__}", node
68
+ )
69
+
70
+ curr_left = curr_right
71
+
72
+ if len(comparisons) == 1:
73
+ return comparisons[0]
74
+ return f'({" && ".join(comparisons)})'
75
+
76
+ def 计算二元运算(self, node: Union[ast.BinOp, ast.AugAssign]):
77
+ """处理二元运算 (+, -, *, /, %, <<, >>, &, |, ^)"""
78
+ if isinstance(node, ast.BinOp):
79
+ left = self.transpiler.获取值(node.left)
80
+ right = self.transpiler.获取值(node.right)
81
+ op = node.op
82
+ elif isinstance(node, ast.AugAssign):
83
+ left = self.transpiler.获取值(node.target)
84
+ right = self.transpiler.获取值(node.value)
85
+ op = node.op
86
+ else:
87
+ return None
88
+
89
+ op_str = ""
90
+ if isinstance(op, ast.Add):
91
+ op_str = "+"
92
+ elif isinstance(op, ast.Sub):
93
+ op_str = "-"
94
+ elif isinstance(op, ast.Mult):
95
+ op_str = "*"
96
+ elif isinstance(op, (ast.Div, ast.FloorDiv)):
97
+ op_str = "/"
98
+ elif isinstance(op, ast.Mod):
99
+ op_str = "%"
100
+ elif isinstance(op, ast.BitAnd):
101
+ op_str = "&"
102
+ elif isinstance(op, ast.BitOr):
103
+ op_str = "|"
104
+ elif isinstance(op, ast.BitXor):
105
+ op_str = "^"
106
+ elif isinstance(op, ast.LShift):
107
+ op_str = "<<"
108
+ elif isinstance(op, ast.RShift):
109
+ op_str = ">>"
110
+
111
+ if op_str:
112
+ return f"({left} {op_str} {right})"
113
+
114
+ self.抛出错误(f"Unsupported operator: {type(op).__name__}", node)
115
+
116
+
117
+ def 处理super调用(self, node: ast.Call):
118
+ """处理super()调用"""
119
+ if not self.transpiler.是否为类:
120
+ self.抛出错误("super() can only be used inside a class", node)
121
+
122
+ # 检查是否有基类
123
+ if not self.transpiler.类基类列表:
124
+ self.抛出错误("super() requires at least one base class", node)
125
+
126
+ # 获取第一个基类(Python的super()通常指MRO中的下一个类)
127
+ base_class = self.transpiler.类基类列表[0]
128
+ base_name = base_class.__name__
129
+
130
+ # 返回一个特殊的SuperCall对象,用于后续的属性访问处理
131
+ return SuperCallWrapper(base_name, self.transpiler)
132
+
133
+
134
+ class 语句访问者(AST访问者基类):
135
+ """处理所有语句类型的访问者"""
136
+
137
+ def visit_FunctionDef(self, node: ast.FunctionDef) -> Any:
138
+ # 这个方法会在TranspilerCore中重写
139
+ pass
140
+
141
+ def visit_ClassDef(self, node: ast.ClassDef) -> Any:
142
+ # 这个方法会在ClassSupport中处理
143
+ pass
144
+
145
+ def visit_Return(self, node: ast.Return) -> Any:
146
+ ret_val = self.transpiler.获取值(node.value) if node.value is not None else ""
147
+ self.transpiler.添加代码带行号(f"return {ret_val};", node)
148
+
149
+ def visit_If(self, node: ast.If) -> Any:
150
+ test = self.transpiler.获取值(node.test)
151
+ self.transpiler.添加代码带行号(f"if ({test})", node)
152
+
153
+ with self.transpiler.代码块上下文:
154
+ for stmt in node.body:
155
+ self.transpiler.visit(stmt)
156
+
157
+ if node.orelse:
158
+ self.transpiler.添加代码带行号("else", node)
159
+ with self.transpiler.代码块上下文:
160
+ for stmt in node.orelse:
161
+ self.transpiler.visit(stmt)
162
+
163
+ def visit_For(self, node: ast.For) -> Any:
164
+ iter_node = node.iter
165
+
166
+ # 特殊处理:Dict.items() 遍历
167
+ if isinstance(iter_node, ast.Call) and isinstance(
168
+ iter_node.func, ast.Attribute
169
+ ):
170
+ attr_node = iter_node.func
171
+ if attr_node.attr == "items":
172
+ # 检查目标是否为元组解包 (key, value)
173
+ if isinstance(node.target, ast.Tuple) and len(node.target.elts) == 2:
174
+ # 获取字典对象
175
+ dict_obj = self.transpiler.获取值(attr_node.value)
176
+
177
+ # 获取键和值的变量名
178
+ key_target = node.target.elts[0]
179
+ value_target = node.target.elts[1]
180
+
181
+ if not isinstance(key_target, ast.Name) or not isinstance(
182
+ value_target, ast.Name
183
+ ):
184
+ self.抛出错误("Dict.items() target must be two names", node)
185
+
186
+ key_var = C变量("auto", key_target.id, False) # type: ignore
187
+ value_var = C变量("auto", value_target.id, False) # type: ignore
188
+
189
+ # 使用 C++17 结构化绑定
190
+ code = (
191
+ f"for (auto& [{key_var.C名称}, {value_var.C名称}] : {dict_obj})"
192
+ )
193
+
194
+ self.transpiler.添加代码(code)
195
+ with self.transpiler.代码块上下文:
196
+ # 注册键和值变量
197
+ self.transpiler.添加C变量(key_var)
198
+ self.transpiler.添加C变量(value_var)
199
+ for stmt in node.body:
200
+ self.transpiler.visit(stmt)
201
+ return
202
+
203
+ # 特殊处理:Dict.keys() 遍历
204
+ elif attr_node.attr == "keys":
205
+ dict_obj = self.transpiler.获取值(attr_node.value)
206
+
207
+ # 对于循环目标,检查是否是已存在的变量
208
+ if isinstance(node.target, ast.Name):
209
+ target = self.transpiler.获取C变量(node.target.id)
210
+ if target is None:
211
+ # 创建新变量
212
+ target = C变量("auto", node.target.id, False)
213
+ self.transpiler.添加C变量(target)
214
+ else:
215
+ self.抛出错误("For loop target must be a name", node)
216
+
217
+ # 遍历键:使用结构化绑定但只使用第一个元素
218
+ code = f"for (auto& [{target.C名称}, _] : {dict_obj})"
219
+
220
+ self.transpiler.添加代码(code)
221
+ with self.transpiler.代码块上下文:
222
+ for stmt in node.body:
223
+ self.transpiler.visit(stmt)
224
+ return
225
+
226
+ # 特殊处理:Dict.values() 遍历
227
+ elif attr_node.attr == "values":
228
+ dict_obj = self.transpiler.获取值(attr_node.value)
229
+
230
+ # 对于循环目标,检查是否是已存在的变量
231
+ if isinstance(node.target, ast.Name):
232
+ target = self.transpiler.获取C变量(node.target.id)
233
+ if target is None:
234
+ # 创建新变量
235
+ target = C变量("auto", node.target.id, False)
236
+ self.transpiler.添加C变量(target)
237
+ else:
238
+ self.抛出错误("For loop target must be a name", node)
239
+
240
+ # 遍历值:使用结构化绑定但只使用第二个元素
241
+ code = f"for (auto& [_, {target.C名称}] : {dict_obj})"
242
+
243
+ self.transpiler.添加代码(code)
244
+ with self.transpiler.代码块上下文:
245
+ for stmt in node.body:
246
+ self.transpiler.visit(stmt)
247
+ return
248
+
249
+ # 原有逻辑:处理其他类型的循环
250
+ # 特殊处理:元组解包目标 (如 for k, v in dict)
251
+ if isinstance(node.target, ast.Tuple):
252
+ # 为元组的每个元素创建变量
253
+ targets = []
254
+ for elt in node.target.elts:
255
+ if isinstance(elt, ast.Name):
256
+ var = C变量("auto", elt.id, False)
257
+ self.transpiler.添加C变量(var)
258
+ targets.append(var)
259
+ else:
260
+ self.抛出错误(
261
+ "For loop target must be a name in tuple unpacking", node
262
+ )
263
+ target = targets
264
+ else:
265
+ # 对于for循环目标,先检查是否是已存在的变量
266
+ if isinstance(node.target, ast.Name):
267
+ target = self.transpiler.获取C变量(node.target.id)
268
+ if target is None:
269
+ # 不存在,创建新变量
270
+ target = C变量("auto", node.target.id, False)
271
+ self.transpiler.添加C变量(target)
272
+ else:
273
+ # 非简单名称的情况,尝试获取值
274
+ target = self.transpiler.获取值(node.target)
275
+ if target is None:
276
+ self.抛出错误("For loop target must be a name", node)
277
+
278
+ # 处理 range() 循环
279
+ if isinstance(iter_node, ast.Call):
280
+ func = self.transpiler.获取值(iter_node.func)
281
+ if func is range:
282
+ args = [self.transpiler.获取值(arg) for arg in iter_node.args]
283
+ if len(args) == 1:
284
+ code = (
285
+ f"for (int64_t {target} = 0; {target} < {args[0]}; ++{target})"
286
+ )
287
+ elif len(args) == 2:
288
+ code = f"for (int64_t {target} = {args[0]}; {target} < {args[1]}; ++{target})"
289
+ elif len(args) == 3:
290
+ code = f"for (int64_t {target} = {args[0]}; {target} < {args[1]}; {target} += {args[2]})"
291
+ else:
292
+ self.抛出错误("Invalid range arguments", node)
293
+ else:
294
+ # 泛型迭代器
295
+ from .表达式处理 import 表达式访问者
296
+
297
+ expr_visitor = 表达式访问者(self.transpiler)
298
+ call_code = expr_visitor.处理调用(iter_node)
299
+ code = f"for (auto {target} : {call_code})"
300
+ # 处理列表/元组字面量循环
301
+ elif isinstance(iter_node, (ast.List, ast.Tuple)):
302
+ l = [self.transpiler.获取值(e) for e in iter_node.elts]
303
+ init_list = 容器构建器._从列表构建初始化列表(l)
304
+ code = f"for (auto {target} : {init_list})"
305
+ else:
306
+ # 处理可迭代对象循环(包括直接遍历 Dict)
307
+ iter_obj = self.transpiler.获取值(iter_node)
308
+
309
+ # 检查是否为 Dict 类型的直接遍历
310
+ if isinstance(iter_obj, 标准无序映射):
311
+ # 直接遍历 Dict 时,遍历键
312
+ if isinstance(target, list) and len(target) == 2:
313
+ # 元组解包:k, v in dict
314
+ code = f"for (auto& [{target[0].C名称}, {target[1].C名称}] : {iter_obj})"
315
+ else:
316
+ # 单变量:k in dict
317
+ target_name = (
318
+ target.C名称 if not isinstance(target, list) else str(target)
319
+ )
320
+ code = f"for (auto& [{target_name}, _] : {iter_obj})"
321
+ else:
322
+ if isinstance(target, list):
323
+ # 列表遍历的元组解包
324
+ target_names = ", ".join([t.C名称 for t in target])
325
+ code = f"for (auto& [{target_names}] : {iter_obj})"
326
+ else:
327
+ code = f"for (auto {target} : {iter_obj})"
328
+
329
+ self.transpiler.添加代码(code)
330
+ with self.transpiler.代码块上下文:
331
+ for stmt in node.body:
332
+ self.transpiler.visit(stmt)
333
+
334
+ def visit_Break(self, node: ast.Break):
335
+ self.transpiler.添加代码("break;")
336
+
337
+ def visit_Continue(self, node: ast.Continue):
338
+ self.transpiler.添加代码("continue;")
339
+
340
+ def visit_While(self, node: ast.While):
341
+ test = self.transpiler.获取值(node.test)
342
+ self.transpiler.添加代码(f"while ({test})")
343
+ with self.transpiler.代码块上下文:
344
+ for stmt in node.body:
345
+ self.transpiler.visit(stmt)
346
+
347
+ def visit_Try(self, node: ast.Try):
348
+ self.transpiler.添加代码("try")
349
+ with self.transpiler.代码块上下文:
350
+ for stmt in node.body:
351
+ self.transpiler.visit(stmt)
352
+
353
+ for handler in node.handlers:
354
+ if handler.type:
355
+ # Catch specific exception
356
+ exc_type = self.transpiler.获取值(handler.type)
357
+ # Map Python exceptions to C++ exceptions if needed
358
+ # For now, we assume standard std::exception or custom C++ classes
359
+
360
+ # Check for 'Exception' which is the base class
361
+ if exc_type == Exception or (
362
+ isinstance(exc_type, str) and exc_type == "Exception"
363
+ ):
364
+ c_exc_type = "const std::exception&"
365
+ else:
366
+ # Attempt to resolve type
367
+ c_exc_type = (
368
+ self.解析类型(exc_type)
369
+ if not isinstance(exc_type, str)
370
+ else exc_type
371
+ )
372
+ if not c_exc_type.endswith("&") and not c_exc_type.endswith("*"):
373
+ c_exc_type = f"const {c_exc_type}&"
374
+
375
+ if handler.name:
376
+ # e.g., except Exception as e:
377
+ self.transpiler.添加代码(f"catch ({c_exc_type} {handler.name})")
378
+ # Register the exception variable in the scope
379
+ # Note: We are entering a scope manually
380
+ else:
381
+ self.transpiler.添加代码(f"catch ({c_exc_type})")
382
+ else:
383
+ # Catch all: except:
384
+ self.transpiler.添加代码("catch (...)")
385
+
386
+ with self.transpiler.代码块上下文:
387
+ if handler.name:
388
+ # If we have a named exception, we need to register it
389
+ # The C++ variable is already declared in the catch clause
390
+ self.transpiler.添加C变量(C变量(c_exc_type, handler.name, False))
391
+
392
+ for stmt in handler.body:
393
+ self.transpiler.visit(stmt)
394
+
395
+ if node.finalbody:
396
+ # C++ doesn't have 'finally', but we can simulate it with RAII or just executing code after try-catch
397
+ self.transpiler.添加代码(
398
+ "// Note: finally block executed here. Warning: does not handle returns inside try/catch correcty without RAII."
399
+ )
400
+ for stmt in node.finalbody:
401
+ self.transpiler.visit(stmt)
402
+
403
+ def visit_Raise(self, node: ast.Raise):
404
+ if node.exc:
405
+ msg = ""
406
+ if isinstance(node.exc, ast.Call):
407
+ # raise Exception("msg")
408
+ func_name = self.transpiler.获取值(node.exc.func)
409
+ if func_name == Exception or (
410
+ isinstance(func_name, str) and func_name == "Exception"
411
+ ):
412
+ if node.exc.args:
413
+ msg_val = self.transpiler.获取值(node.exc.args[0])
414
+ self.transpiler.添加代码(
415
+ f"throw std::runtime_error({msg_val});"
416
+ )
417
+ self.transpiler.包含头文件.add("<stdexcept>")
418
+ return
419
+ elif isinstance(node.exc, ast.Name):
420
+ # raise e
421
+ val = self.transpiler.获取值(node.exc)
422
+ self.transpiler.添加代码(f"throw {val};")
423
+ return
424
+
425
+ # Fallback
426
+ val = self.transpiler.获取值(node.exc)
427
+ self.transpiler.添加代码(f"throw {val};")
428
+ else:
429
+ # re-raise
430
+ self.transpiler.添加代码("throw;")
431
+
432
+ def visit_Assign(self, node: ast.Assign):
433
+ from .代码生成 import 代码生成器
434
+
435
+ generator = 代码生成器(self.transpiler)
436
+ value = self.transpiler.获取值(node.value)
437
+ for target in node.targets:
438
+ generator._assign(target, value, node)
439
+
440
+ def visit_AugAssign(self, node: ast.AugAssign):
441
+ from .代码生成 import 代码生成器
442
+
443
+ generator = 代码生成器(self.transpiler)
444
+ value = self.计算二元运算(node)
445
+ generator._assign(node.target, value, node)
446
+
447
+ def visit_AnnAssign(self, node: ast.AnnAssign):
448
+ from .代码生成 import 代码生成器
449
+ generator = 代码生成器(self.transpiler)
450
+
451
+ target_py_type = self.transpiler.获取值(
452
+ node.annotation, is_type_annotation=True
453
+ )
454
+ c_type = self.解析类型(target_py_type)
455
+ c_type_str = str(c_type)
456
+
457
+ # 特殊处理:Set 和 Map 类型
458
+ # 对于这些类型,我们需要使用类型注解来创建正确的变量包装器
459
+ if c_type_str.startswith("std::unordered_set"):
460
+ # 处理 s: Set[T] = set() 或 s: Set[T] = {1, 2, 3}
461
+ if not isinstance(node.target, ast.Name):
462
+ self.抛出错误("Assignment target must be a name", node)
463
+
464
+ # 获取元素类型
465
+ origin = getattr(target_py_type, "__origin__", None)
466
+ args = getattr(target_py_type, "__args__", [])
467
+ if origin is set and args:
468
+ elem_type_str = str(self.解析类型(args[0]))
469
+ else:
470
+ elem_type_str = "int64_t" # fallback
471
+
472
+ # 处理值
473
+ if isinstance(node.value, ast.Call):
474
+ func = self.transpiler.获取值(node.value.func)
475
+ if func is set:
476
+ # s: Set[T] = set() -> 空集合
477
+ value = "{}"
478
+ else:
479
+ value = self.transpiler.获取值(node.value)
480
+ else:
481
+ value = self.transpiler.获取值(node.value)
482
+
483
+ # 创建集合初始化列表(如果值还不是)
484
+ if isinstance(value, str):
485
+ init_list = 集合初始化列表(value, elem_type_str)
486
+ else:
487
+ init_list = value
488
+
489
+ target_var = 标准集合(init_list, node.target.id, False) # type: ignore
490
+ self.transpiler.包含头文件.add("<unordered_set>")
491
+ self.transpiler.添加代码(target_var.初始化代码(value, None))
492
+ self.transpiler.添加C变量(target_var)
493
+ return
494
+
495
+ elif c_type_str.startswith("std::unordered_map"):
496
+ # 处理 m: Dict[K, V] = {} 或 m: Dict[K, V] = {k: v, ...}
497
+ if not isinstance(node.target, ast.Name):
498
+ self.抛出错误("Assignment target must be a name", node)
499
+
500
+ # 获取键值类型
501
+ origin = getattr(target_py_type, "__origin__", None)
502
+ args = getattr(target_py_type, "__args__", [])
503
+ if origin is dict and len(args) == 2:
504
+ key_type_str = str(self.解析类型(args[0]))
505
+ val_type_str = str(self.解析类型(args[1]))
506
+ else:
507
+ key_type_str = "int64_t" # fallback
508
+ val_type_str = "int64_t"
509
+
510
+ value = self.transpiler.获取值(node.value)
511
+
512
+ # 创建字典初始化列表(如果值还不是)
513
+ if isinstance(value, str):
514
+ init_list = 字典初始化列表(value, key_type_str, val_type_str)
515
+ elif isinstance(value, 字典初始化列表):
516
+ # 如果已经是字典初始化列表但类型是 auto,需要用正确的类型重新创建
517
+ if value.键类型名 == "auto" or value.值类型名 == "auto":
518
+ init_list = 字典初始化列表(value.代码, key_type_str, val_type_str)
519
+ else:
520
+ init_list = value
521
+ else:
522
+ init_list = value
523
+
524
+ target_var = 标准无序映射(init_list, node.target.id, False) # type: ignore
525
+ self.transpiler.包含头文件.add("<unordered_map>")
526
+ # 如果键或值类型包含string,添加string头文件
527
+ if "std::string" in str(target_var.类型名):
528
+ self.transpiler.包含头文件.add("<string>")
529
+ self.transpiler.添加代码(
530
+ target_var.初始化代码(
531
+ (
532
+ init_list.代码
533
+ if isinstance(init_list, 字典初始化列表)
534
+ else init_list
535
+ ),
536
+ None,
537
+ )
538
+ )
539
+ self.transpiler.添加C变量(target_var)
540
+ return
541
+
542
+ # 特殊处理:List[T] 类型(std::vector)
543
+ if c_type_str.startswith("std::vector"):
544
+ if not isinstance(node.target, ast.Name):
545
+ self.抛出错误("Assignment target must be a name", node)
546
+
547
+ # 获取元素类型
548
+ origin = getattr(target_py_type, "__origin__", None)
549
+ args = getattr(target_py_type, "__args__", [])
550
+ if origin is list and args:
551
+ elem_type_str = str(self.解析类型(args[0]))
552
+ else:
553
+ # 从 c_type_str 中提取类型,例如 "std::vector<int>" -> "int"
554
+ if c_type_str.startswith("std::vector<") and c_type_str.endswith(">"):
555
+ elem_type_str = c_type_str[12:-1] # 去掉 "std::vector<" 和 ">"
556
+ else:
557
+ elem_type_str = "int64_t" # fallback
558
+
559
+ # 处理值
560
+ value = self.transpiler.获取值(node.value)
561
+
562
+ # 如果是空列表或类型是 auto,需要使用正确的元素类型
563
+ from .cpp类型 import 列表初始化列表
564
+ if isinstance(value, 列表初始化列表):
565
+ if value.类型名 == "auto" or not value.代码.strip():
566
+ # 创建带有正确类型的空列表初始化
567
+ value = 列表初始化列表("{}", elem_type_str, 0)
568
+
569
+ # 创建列表变量
570
+ from .std_vector import 标准列表
571
+ target_var = 标准列表(value, node.target.id, False) # type: ignore
572
+ self.transpiler.包含头文件.add("<vector>")
573
+ self.transpiler.添加代码(
574
+ target_var.初始化代码(
575
+ value.代码 if isinstance(value, 列表初始化列表) else value,
576
+ None,
577
+ )
578
+ )
579
+ self.transpiler.添加C变量(target_var)
580
+ return
581
+
582
+ # 其他类型正常处理
583
+ value = self.transpiler.获取值(node.value)
584
+ generator._assign(node.target, value, node, c_type_str)
585
+
586
+ def visit_Expr(self, node: ast.Expr):
587
+ # 处理独立表达式,如函数调用语句
588
+ if isinstance(node.value, ast.Call):
589
+ from .表达式处理 import 表达式访问者
590
+
591
+ expr_visitor = 表达式访问者(self.transpiler)
592
+ code = expr_visitor.处理调用(node.value)
593
+ self.transpiler.添加代码(f"{code};")
594
+
595
+ def visit_arguments(self, node: ast.arguments) -> Any:
596
+ from .代码生成 import 参数处理器
597
+
598
+ processor = 参数处理器(self.transpiler)
599
+ processor.处理参数列表(node)