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.
- l0n0lc/Py/350/275/254Cpp/350/275/254/350/257/221/345/231/250.py +579 -0
- l0n0lc/__init__.py +75 -6
- l0n0lc/aot/347/274/226/350/257/221.py +679 -0
- l0n0lc/ast/350/256/277/351/227/256/350/200/205.py +599 -0
- l0n0lc/cpp/347/261/273/345/236/213.py +330 -0
- l0n0lc/cpp/347/274/226/350/257/221/345/231/250.py +317 -0
- l0n0lc/simd/344/274/230/345/214/226.py +260 -0
- l0n0lc/std_map.py +153 -0
- l0n0lc/std_set.py +185 -0
- l0n0lc/std_vector.py +96 -0
- l0n0lc//344/273/243/347/240/201/344/274/230/345/214/226.py +302 -0
- l0n0lc//344/273/243/347/240/201/347/224/237/346/210/220.py +546 -0
- l0n0lc//344/276/235/350/265/226/346/263/250/345/205/245.py +155 -0
- l0n0lc//345/215/263/346/227/266/347/274/226/350/257/221.py +192 -0
- l0n0lc//345/217/230/351/207/217/347/256/241/347/220/206/345/231/250.py +123 -0
- l0n0lc//345/237/272/347/241/200/346/230/240/345/260/204.py +103 -0
- l0n0lc//345/237/272/347/241/200/346/267/267/345/205/245.py +147 -0
- l0n0lc//345/256/271/345/231/250/346/236/204/345/273/272/345/231/250.py +214 -0
- l0n0lc//345/267/245/345/205/267.py +285 -0
- l0n0lc//345/271/266/350/241/214/347/274/226/350/257/221/345/231/250.py +412 -0
- l0n0lc//345/274/202/345/270/270.py +474 -0
- l0n0lc//346/225/260/347/273/204/345/257/271/350/261/241/346/261/240.py +248 -0
- l0n0lc//346/226/207/344/273/266/347/256/241/347/220/206/345/231/250.py +286 -0
- l0n0lc//346/227/245/345/277/227/345/267/245/345/205/267.py +152 -0
- l0n0lc//347/261/273/345/236/213/346/216/250/346/226/255/345/267/245/345/205/267.py +352 -0
- l0n0lc//347/261/273/345/236/213/350/275/254/346/215/242.py +210 -0
- l0n0lc//347/261/273/346/224/257/346/214/201.py +372 -0
- l0n0lc//347/274/226/350/257/221/344/270/212/344/270/213/346/226/207.py +132 -0
- l0n0lc//347/274/226/350/257/221/347/256/241/347/220/206/345/231/250.py +171 -0
- l0n0lc//350/241/250/350/276/276/345/274/217/345/244/204/347/220/206.py +462 -0
- l0n0lc//350/275/254/350/257/221/345/231/250/345/267/245/345/205/267.py +49 -0
- l0n0lc//350/277/220/350/241/214/346/227/266/345/212/240/350/275/275.py +217 -0
- l0n0lc//351/200/232/347/224/250/345/267/245/345/205/267.py +149 -0
- l0n0lc-1.0.0.dist-info/METADATA +363 -0
- l0n0lc-1.0.0.dist-info/RECORD +39 -0
- {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/WHEEL +1 -1
- l0n0lc-1.0.0.dist-info/entry_points.txt +2 -0
- {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/licenses/LICENSE +0 -0
- l0n0lc/StdList.py +0 -24
- l0n0lc/StdMap.py +0 -21
- l0n0lc/c/345/237/272/347/241/200/345/244/204/347/220/206.py +0 -207
- l0n0lc/jit.py +0 -604
- l0n0lc//347/274/226/350/257/221.py +0 -58
- l0n0lc//351/200/232/347/224/250.py +0 -134
- l0n0lc-0.8.4.dist-info/METADATA +0 -241
- l0n0lc-0.8.4.dist-info/RECORD +0 -12
- {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
import inspect
|
|
3
|
+
from typing import Union
|
|
4
|
+
from .工具 import 全局上下文
|
|
5
|
+
from .cpp类型 import C变量, Cpp类型, 字典初始化列表, 列表初始化列表, 集合初始化列表
|
|
6
|
+
from .std_vector import 标准列表
|
|
7
|
+
from .std_map import 标准无序映射
|
|
8
|
+
from .std_set import 标准集合
|
|
9
|
+
from .类型转换 import 类型转换器
|
|
10
|
+
from .基础混入 import 错误处理混入, 类型处理混入
|
|
11
|
+
from .表达式处理 import 表达式访问者
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class 代码生成器(错误处理混入, 类型处理混入):
|
|
15
|
+
"""负责生成C++代码的类"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, transpiler):
|
|
18
|
+
self.transpiler = transpiler
|
|
19
|
+
|
|
20
|
+
def _assign(
|
|
21
|
+
self, target_node, value, context_node, cast_type: Union[str, None] = None
|
|
22
|
+
):
|
|
23
|
+
"""处理赋值操作"""
|
|
24
|
+
try:
|
|
25
|
+
target_var = self.transpiler.获取值(target_node)
|
|
26
|
+
except Exception:
|
|
27
|
+
# 如果获取值失败,说明这是一个复杂的表达式赋值目标
|
|
28
|
+
# 这种情况下我们让代码继续,target_var为None会在后续处理中被捕获
|
|
29
|
+
target_var = None
|
|
30
|
+
except KeyboardInterrupt:
|
|
31
|
+
raise
|
|
32
|
+
|
|
33
|
+
# 处理 Python 变量的直接赋值 (仅在 正在直接调用 模式下)
|
|
34
|
+
if self.transpiler.正在直接调用:
|
|
35
|
+
if isinstance(target_node, ast.Name):
|
|
36
|
+
self.transpiler.本地变量[target_node.id] = value
|
|
37
|
+
else:
|
|
38
|
+
self.抛出错误("Direct assignment only supports simple names", context_node)
|
|
39
|
+
self.transpiler.正在直接调用 = False
|
|
40
|
+
return
|
|
41
|
+
|
|
42
|
+
if target_var is None:
|
|
43
|
+
# 新变量声明
|
|
44
|
+
if isinstance(target_node, ast.Name):
|
|
45
|
+
if isinstance(value, 字典初始化列表):
|
|
46
|
+
target_var = 标准无序映射(value, target_node.id, False)
|
|
47
|
+
self.transpiler.包含头文件.add("<unordered_map>")
|
|
48
|
+
# 如果键或值类型包含string,添加string头文件
|
|
49
|
+
if "std::string" in str(target_var.类型名):
|
|
50
|
+
self.transpiler.包含头文件.add("<string>")
|
|
51
|
+
elif isinstance(value, 列表初始化列表):
|
|
52
|
+
target_var = 标准列表(value, target_node.id, False)
|
|
53
|
+
self.transpiler.包含头文件.add("<vector>")
|
|
54
|
+
if value.类型名 == Cpp类型.任意:
|
|
55
|
+
self.transpiler.包含头文件.add("<any>")
|
|
56
|
+
elif isinstance(value, 集合初始化列表):
|
|
57
|
+
target_var = 标准集合(value, target_node.id, False)
|
|
58
|
+
self.transpiler.包含头文件.add("<unordered_set>")
|
|
59
|
+
else:
|
|
60
|
+
target_var = C变量("auto", target_node.id, False)
|
|
61
|
+
|
|
62
|
+
self.transpiler.添加代码(target_var.初始化代码(value, cast_type),
|
|
63
|
+
getattr(context_node, 'lineno', None))
|
|
64
|
+
self.transpiler.添加C变量(target_var)
|
|
65
|
+
else:
|
|
66
|
+
self.抛出错误("Assignment target must be a name", context_node)
|
|
67
|
+
else:
|
|
68
|
+
# 现有变量赋值
|
|
69
|
+
target_name = (
|
|
70
|
+
target_var.C名称 if hasattr(target_var, "C名称") else str(target_var)
|
|
71
|
+
)
|
|
72
|
+
if cast_type:
|
|
73
|
+
self.transpiler.添加代码(f"{target_name} = ({cast_type})({value});",
|
|
74
|
+
getattr(context_node, 'lineno', None))
|
|
75
|
+
else:
|
|
76
|
+
self.transpiler.添加代码(f"{target_name} = {value};",
|
|
77
|
+
getattr(context_node, 'lineno', None))
|
|
78
|
+
|
|
79
|
+
def 生成函数定义(self):
|
|
80
|
+
"""生成 C 函数定义/声明,或 C++ 类定义"""
|
|
81
|
+
if self.transpiler.是否为类:
|
|
82
|
+
return self.生成类定义()
|
|
83
|
+
else:
|
|
84
|
+
return self.生成函数声明()
|
|
85
|
+
|
|
86
|
+
def 生成函数声明(self):
|
|
87
|
+
"""生成函数声明"""
|
|
88
|
+
params = []
|
|
89
|
+
for name, var in self.transpiler.参数变量.items():
|
|
90
|
+
if name in self.transpiler.列表参数映射:
|
|
91
|
+
ptr_var, len_var = self.transpiler.列表参数映射[name]
|
|
92
|
+
params.append(f"{ptr_var.类型名} {ptr_var.C名称}")
|
|
93
|
+
params.append(f"{len_var.类型名} {len_var.C名称}")
|
|
94
|
+
elif isinstance(var, C变量):
|
|
95
|
+
params.append(f"{var.类型名} {var.C名称}")
|
|
96
|
+
|
|
97
|
+
param_str = ", ".join(params)
|
|
98
|
+
return f'extern "C" {self.transpiler.返回类型} {self.transpiler.C函数名} ({param_str})'
|
|
99
|
+
|
|
100
|
+
def 生成类定义(self):
|
|
101
|
+
"""生成类定义 struct Name { ... };"""
|
|
102
|
+
# 处理继承
|
|
103
|
+
inheritance = ""
|
|
104
|
+
if self.transpiler.类基类列表:
|
|
105
|
+
base_names = []
|
|
106
|
+
for base in self.transpiler.类基类列表:
|
|
107
|
+
base_names.append(f"public {base.__name__}")
|
|
108
|
+
inheritance = " : " + ", ".join(base_names)
|
|
109
|
+
|
|
110
|
+
# 静态成员声明
|
|
111
|
+
static_fields = []
|
|
112
|
+
for name, (type_, value) in self.transpiler.类静态成员.items():
|
|
113
|
+
static_fields.append(f" static {type_} {name};")
|
|
114
|
+
|
|
115
|
+
# 实例成员变量
|
|
116
|
+
fields = []
|
|
117
|
+
for name, type_ in self.transpiler.类成员变量.items():
|
|
118
|
+
fields.append(f" {type_} {name};")
|
|
119
|
+
|
|
120
|
+
# 方法声明
|
|
121
|
+
method_decls = []
|
|
122
|
+
|
|
123
|
+
# 如果有默认值但没有显式构造函数,声明默认构造函数
|
|
124
|
+
has_explicit_init = any(m["is_init"] for m in self.transpiler.类方法列表)
|
|
125
|
+
if self.transpiler.类成员默认值 and not has_explicit_init:
|
|
126
|
+
method_decls.append(f" {self.transpiler.C函数名}();")
|
|
127
|
+
|
|
128
|
+
for m in self.transpiler.类方法列表:
|
|
129
|
+
# 构建方法修饰符
|
|
130
|
+
modifiers = []
|
|
131
|
+
if m.get("is_static", False):
|
|
132
|
+
modifiers.append("static")
|
|
133
|
+
|
|
134
|
+
modifier_str = " ".join(modifiers) + " " if modifiers else ""
|
|
135
|
+
|
|
136
|
+
if m["is_init"]:
|
|
137
|
+
# 构造函数
|
|
138
|
+
method_decls.append(f" {modifier_str}{m['name']}({m['params']});")
|
|
139
|
+
else:
|
|
140
|
+
# 普通方法或运算符
|
|
141
|
+
method_decls.append(
|
|
142
|
+
f" {modifier_str}{m['ret_type']} {m['name']}({m['params']});"
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
# 组合所有部分
|
|
146
|
+
all_members = static_fields + fields
|
|
147
|
+
if all_members and method_decls:
|
|
148
|
+
all_members.append("") # 空行分隔成员和方法
|
|
149
|
+
all_members.extend(method_decls)
|
|
150
|
+
|
|
151
|
+
struct_body = "\n".join(all_members)
|
|
152
|
+
return f"struct {self.transpiler.C函数名}{inheritance} {{\n{struct_body}\n}};"
|
|
153
|
+
|
|
154
|
+
def 生成包含代码(self):
|
|
155
|
+
"""生成包含头文件的代码"""
|
|
156
|
+
return "\n".join([f"#include {d}" for d in sorted(self.transpiler.包含头文件)])
|
|
157
|
+
|
|
158
|
+
def 生成头文件代码(self):
|
|
159
|
+
"""生成头文件完整代码"""
|
|
160
|
+
return f"#pragma once\n{self.生成包含代码()}\n{self.生成函数定义()};"
|
|
161
|
+
|
|
162
|
+
def 生成cpp代码(self):
|
|
163
|
+
"""生成cpp文件完整代码"""
|
|
164
|
+
if self.transpiler.是否为类:
|
|
165
|
+
return self.生成类实现代码()
|
|
166
|
+
else:
|
|
167
|
+
return self.生成函数实现代码()
|
|
168
|
+
|
|
169
|
+
def 生成类实现代码(self):
|
|
170
|
+
"""生成类的实现代码"""
|
|
171
|
+
parts = []
|
|
172
|
+
|
|
173
|
+
# 1. 包含头文件
|
|
174
|
+
parts.append(f'#include "{self.transpiler.获取头文件名()}"')
|
|
175
|
+
|
|
176
|
+
# 2. 静态成员定义
|
|
177
|
+
if self.transpiler.类静态成员:
|
|
178
|
+
parts.append("") # 空行
|
|
179
|
+
for name, (type_, value) in self.transpiler.类静态成员.items():
|
|
180
|
+
# 生成静态成员定义
|
|
181
|
+
if isinstance(value, str):
|
|
182
|
+
parts.append(
|
|
183
|
+
f'{type_} {self.transpiler.C函数名}::{name} = "{value}";'
|
|
184
|
+
)
|
|
185
|
+
elif isinstance(value, bool):
|
|
186
|
+
parts.append(
|
|
187
|
+
f'{type_} {self.transpiler.C函数名}::{name} = {"true" if value else "false"};'
|
|
188
|
+
)
|
|
189
|
+
else:
|
|
190
|
+
parts.append(
|
|
191
|
+
f"{type_} {self.transpiler.C函数名}::{name} = {value};"
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
# 3. 默认构造函数(如果有默认值且没有显式定义构造函数)
|
|
195
|
+
has_explicit_init = any(m["is_init"] for m in self.transpiler.类方法列表)
|
|
196
|
+
if self.transpiler.类成员默认值 and not has_explicit_init:
|
|
197
|
+
# 生成默认构造函数
|
|
198
|
+
parts.append("")
|
|
199
|
+
initializers = []
|
|
200
|
+
for name, default_val in self.transpiler.类成员默认值.items():
|
|
201
|
+
if isinstance(default_val, str):
|
|
202
|
+
initializers.append(f'{name}("{default_val}")')
|
|
203
|
+
else:
|
|
204
|
+
initializers.append(f"{name}({default_val})")
|
|
205
|
+
|
|
206
|
+
if initializers:
|
|
207
|
+
init_list = ", ".join(initializers)
|
|
208
|
+
parts.append(
|
|
209
|
+
f"{self.transpiler.C函数名}::{self.transpiler.C函数名}() : {init_list} {{}}"
|
|
210
|
+
)
|
|
211
|
+
else:
|
|
212
|
+
parts.append(
|
|
213
|
+
f"{self.transpiler.C函数名}::{self.transpiler.C函数名}() {{}}"
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
# 4. 方法实现
|
|
217
|
+
impls = []
|
|
218
|
+
for m in self.transpiler.类方法列表:
|
|
219
|
+
# 静态方法需要 static 修饰符
|
|
220
|
+
modifier = "static " if m.get("is_static", False) else ""
|
|
221
|
+
|
|
222
|
+
full_name = f"{self.transpiler.C函数名}::{m['name']}"
|
|
223
|
+
if m["is_init"]:
|
|
224
|
+
# 构造函数实现
|
|
225
|
+
head = f"{full_name}({m['params']})"
|
|
226
|
+
|
|
227
|
+
# 构建初始化列表
|
|
228
|
+
initializers = []
|
|
229
|
+
|
|
230
|
+
# 如果有基类,首先添加基类构造函数调用
|
|
231
|
+
if self.transpiler.类基类列表:
|
|
232
|
+
base_class = self.transpiler.类基类列表[0]
|
|
233
|
+
base_name = base_class.__name__
|
|
234
|
+
# 对于构造函数参数,我们需要传递第一个参数给基类构造函数
|
|
235
|
+
# 这里假设第一个参数是name,与基类构造函数匹配
|
|
236
|
+
if m["params"]:
|
|
237
|
+
# 提取第一个参数名(去掉类型声明,只保留参数名)
|
|
238
|
+
first_param_full = m["params"].split(",")[0].strip()
|
|
239
|
+
# 参数格式可能是 "std::string name" 或 "int x" 等,我们只需要参数名部分
|
|
240
|
+
if " " in first_param_full:
|
|
241
|
+
first_param = first_param_full.split()[-1]
|
|
242
|
+
else:
|
|
243
|
+
first_param = first_param_full
|
|
244
|
+
initializers.append(f"{base_name}({first_param})")
|
|
245
|
+
else:
|
|
246
|
+
# 如果没有参数,调用基类默认构造函数
|
|
247
|
+
initializers.append(f"{base_name}()")
|
|
248
|
+
|
|
249
|
+
# 如果有默认值,添加成员变量初始化
|
|
250
|
+
if self.transpiler.类成员默认值:
|
|
251
|
+
for name, default_val in self.transpiler.类成员默认值.items():
|
|
252
|
+
if isinstance(default_val, str):
|
|
253
|
+
initializers.append(f'{name}("{default_val}")')
|
|
254
|
+
else:
|
|
255
|
+
initializers.append(f"{name}({default_val})")
|
|
256
|
+
|
|
257
|
+
if initializers:
|
|
258
|
+
init_list = ", ".join(initializers)
|
|
259
|
+
head = f"{full_name}({m['params']}) : {init_list}"
|
|
260
|
+
else:
|
|
261
|
+
# 普通方法或运算符
|
|
262
|
+
head = f"{m['ret_type']} {full_name}({m['params']})"
|
|
263
|
+
|
|
264
|
+
body_lines = [str(line) for line in m["body"]]
|
|
265
|
+
body_str = "\n".join(body_lines)
|
|
266
|
+
impls.append(f"{head}\n{body_str}")
|
|
267
|
+
|
|
268
|
+
if impls:
|
|
269
|
+
parts.append("")
|
|
270
|
+
parts.extend(impls)
|
|
271
|
+
|
|
272
|
+
return "\n".join(parts)
|
|
273
|
+
|
|
274
|
+
def 生成函数实现代码(self):
|
|
275
|
+
"""生成函数的实现代码"""
|
|
276
|
+
# 获取Python源码行,用于生成注释
|
|
277
|
+
source_lines = self.transpiler.源代码.split('\n')
|
|
278
|
+
|
|
279
|
+
# 直接从 StringIO 获取生成的代码
|
|
280
|
+
body_code = self.transpiler.代码缓冲区.getvalue()
|
|
281
|
+
|
|
282
|
+
# 生成完整的C++代码
|
|
283
|
+
parts = []
|
|
284
|
+
parts.append(f'#include "{self.transpiler.获取头文件名()}"')
|
|
285
|
+
parts.append("") # 空行
|
|
286
|
+
parts.append("// === Python 源码 ===")
|
|
287
|
+
for i, line in enumerate(source_lines, 1):
|
|
288
|
+
parts.append(f"// 第{i:2d}行: {line}")
|
|
289
|
+
parts.append("")
|
|
290
|
+
parts.append("// === C++ 实现 ===")
|
|
291
|
+
parts.append(self.生成函数声明())
|
|
292
|
+
parts.append("{")
|
|
293
|
+
if body_code.strip():
|
|
294
|
+
# 将 body_code 按行分割并添加到 parts
|
|
295
|
+
for line in body_code.strip().split('\n'):
|
|
296
|
+
parts.append(line)
|
|
297
|
+
parts.append("}")
|
|
298
|
+
|
|
299
|
+
return "\n".join(parts)
|
|
300
|
+
|
|
301
|
+
def 保存代码到文件(self):
|
|
302
|
+
"""保存代码到文件"""
|
|
303
|
+
# 清理旧文件
|
|
304
|
+
self.transpiler.文件管理器.清理旧文件(self.transpiler.文件前缀)
|
|
305
|
+
|
|
306
|
+
# 保存头文件
|
|
307
|
+
self.transpiler.文件管理器.写入文件(
|
|
308
|
+
self.transpiler.获取头文件名(),
|
|
309
|
+
self.生成头文件代码()
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
# 保存 cpp 文件
|
|
313
|
+
self.transpiler.文件管理器.写入文件(
|
|
314
|
+
self.transpiler.获取cpp文件名(),
|
|
315
|
+
self.生成cpp代码()
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
def 构建当前参数列表字符串(self):
|
|
319
|
+
"""构建当前方法的参数列表字符串(用于类方法)"""
|
|
320
|
+
params = []
|
|
321
|
+
# 使用 当前方法参数 而非 参数变量,确保参数隔离
|
|
322
|
+
param_dict = (
|
|
323
|
+
self.transpiler.当前方法参数
|
|
324
|
+
if self.transpiler.当前方法参数
|
|
325
|
+
else self.transpiler.参数变量
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
for name, var in param_dict.items():
|
|
329
|
+
if name in ["self", "cls"]:
|
|
330
|
+
continue # Skip self/cls if present
|
|
331
|
+
|
|
332
|
+
if name in self.transpiler.列表参数映射:
|
|
333
|
+
ptr_var, len_var = self.transpiler.列表参数映射[name]
|
|
334
|
+
params.append(f"{ptr_var.类型名} {ptr_var.C名称}")
|
|
335
|
+
params.append(f"{len_var.类型名} {len_var.C名称}")
|
|
336
|
+
elif isinstance(var, C变量):
|
|
337
|
+
params.append(f"{var.类型名} {var.C名称}")
|
|
338
|
+
return ", ".join(params)
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
class 参数处理器(错误处理混入, 类型处理混入):
|
|
342
|
+
"""处理函数参数的类"""
|
|
343
|
+
|
|
344
|
+
def __init__(self, transpiler):
|
|
345
|
+
self.transpiler = transpiler
|
|
346
|
+
|
|
347
|
+
def 处理参数列表(self, node: ast.arguments):
|
|
348
|
+
"""处理参数列表"""
|
|
349
|
+
self.transpiler.正在构建参数 = True
|
|
350
|
+
|
|
351
|
+
args = list(node.args)
|
|
352
|
+
if node.vararg:
|
|
353
|
+
# C++ 变长参数处理复杂,暂不支持
|
|
354
|
+
self.抛出错误("*args not supported", node)
|
|
355
|
+
|
|
356
|
+
for idx, arg in enumerate(args):
|
|
357
|
+
default_val = None
|
|
358
|
+
if idx >= len(args) - len(node.defaults):
|
|
359
|
+
default_val = node.defaults[idx - (len(args) - len(node.defaults))]
|
|
360
|
+
self.处理参数(arg, default_val)
|
|
361
|
+
|
|
362
|
+
self.transpiler.正在构建参数 = False
|
|
363
|
+
|
|
364
|
+
def 处理参数(self, node: ast.arg, default_val=None):
|
|
365
|
+
"""处理单个参数"""
|
|
366
|
+
name = node.arg
|
|
367
|
+
|
|
368
|
+
# 记录参数名称
|
|
369
|
+
self.transpiler.参数名称.append(name)
|
|
370
|
+
|
|
371
|
+
if default_val is not None:
|
|
372
|
+
# C++ 默认参数在声明中支持,此处简化处理,暂忽略
|
|
373
|
+
pass
|
|
374
|
+
|
|
375
|
+
# 处理self/cls参数
|
|
376
|
+
if node.annotation is None:
|
|
377
|
+
# 对于类方法的self/cls参数,不需要类型注解
|
|
378
|
+
if self.transpiler.是否为类 and name in ["self", "cls"]:
|
|
379
|
+
# self/cls参数不会被添加到参数列表中(已在visit_FunctionDef中处理)
|
|
380
|
+
return
|
|
381
|
+
else:
|
|
382
|
+
self.抛出错误(f"Argument '{name}' must have type annotation", node)
|
|
383
|
+
|
|
384
|
+
expr_visitor = 表达式访问者(self.transpiler)
|
|
385
|
+
py_type = expr_visitor.获取值(node.annotation, is_type_annotation=True)
|
|
386
|
+
|
|
387
|
+
# 处理字符串类型注解(如 'Vector2D')
|
|
388
|
+
if isinstance(py_type, str) and py_type not in [
|
|
389
|
+
"int",
|
|
390
|
+
"float",
|
|
391
|
+
"str",
|
|
392
|
+
"bool",
|
|
393
|
+
"void",
|
|
394
|
+
"bytes",
|
|
395
|
+
]:
|
|
396
|
+
# 尝试从全局变量中查找对应的类
|
|
397
|
+
if py_type in self.transpiler.全局变量:
|
|
398
|
+
potential_class = self.transpiler.全局变量[py_type]
|
|
399
|
+
if inspect.isclass(potential_class):
|
|
400
|
+
py_type = potential_class
|
|
401
|
+
|
|
402
|
+
# 使用类型转换器处理
|
|
403
|
+
|
|
404
|
+
# 处理自定义类类型
|
|
405
|
+
if inspect.isclass(py_type):
|
|
406
|
+
# 首先检查是否有类型映射
|
|
407
|
+
mapped_type = 全局上下文.类型映射表.get(py_type)
|
|
408
|
+
if mapped_type:
|
|
409
|
+
# 使用映射的类型
|
|
410
|
+
c_type = mapped_type.目标类型
|
|
411
|
+
# 添加需要包含的头文件
|
|
412
|
+
if mapped_type.包含目录:
|
|
413
|
+
self.transpiler.包含头文件.update(mapped_type.包含目录)
|
|
414
|
+
# 添加需要链接的库
|
|
415
|
+
if mapped_type.库:
|
|
416
|
+
self.transpiler.链接库.update(mapped_type.库)
|
|
417
|
+
# 添加库搜索目录
|
|
418
|
+
if mapped_type.库目录:
|
|
419
|
+
self.transpiler.库搜索目录.update(mapped_type.库目录)
|
|
420
|
+
elif py_type.__module__ == "builtins":
|
|
421
|
+
# 内置类型如str, int等,需要正常转换
|
|
422
|
+
c_type = self.解析类型(py_type)
|
|
423
|
+
else:
|
|
424
|
+
# 用户自定义类,使用类名作为C++类型
|
|
425
|
+
c_type = py_type.__name__
|
|
426
|
+
else:
|
|
427
|
+
c_type = self.解析类型(py_type)
|
|
428
|
+
|
|
429
|
+
if c_type is None:
|
|
430
|
+
self.抛出错误(f"Unsupported type {py_type}", node)
|
|
431
|
+
|
|
432
|
+
# 检测是否为 std::vector 类型 (即 Python 的 List[T])
|
|
433
|
+
# 如果是,则将其拆分为 指针 和 长度 两个参数传递,以兼容 ctypes
|
|
434
|
+
if str(c_type).startswith("std::vector"):
|
|
435
|
+
self.transpiler.包含头文件.add("<vector>")
|
|
436
|
+
# 提取基础类型 T: std::vector<int> -> int
|
|
437
|
+
base_type = str(c_type)[12:-1]
|
|
438
|
+
|
|
439
|
+
ptr_name = f"{name}_ptr"
|
|
440
|
+
len_name = f"{name}_len"
|
|
441
|
+
|
|
442
|
+
ptr_var = C变量(f"{base_type}*", ptr_name, True)
|
|
443
|
+
len_var = C变量("int64_t", len_name, True)
|
|
444
|
+
|
|
445
|
+
self.transpiler.列表参数映射[name] = (ptr_var, len_var)
|
|
446
|
+
|
|
447
|
+
# 逻辑上的参数变量 (在 C++ 函数内部作为局部变量使用)
|
|
448
|
+
# 对于List类型,创建标准列表对象以支持方法调用
|
|
449
|
+
from .std_vector import 标准列表
|
|
450
|
+
from .cpp类型 import 列表初始化列表
|
|
451
|
+
|
|
452
|
+
# 直接使用已提取的 base_type(第449行已定义)
|
|
453
|
+
# 创建列表初始化列表和标准列表对象
|
|
454
|
+
init_list = 列表初始化列表("", base_type, 0) # 空初始化,后续会重建
|
|
455
|
+
impl_var = 标准列表(init_list, name, False)
|
|
456
|
+
|
|
457
|
+
self.transpiler.参数变量[name] = impl_var
|
|
458
|
+
|
|
459
|
+
if not self.transpiler.可执行文件名:
|
|
460
|
+
# 设置 ctypes 参数类型
|
|
461
|
+
origin = getattr(py_type, "__origin__", None)
|
|
462
|
+
args = getattr(py_type, "__args__", [])
|
|
463
|
+
if origin is list and args:
|
|
464
|
+
elem_type = args[0]
|
|
465
|
+
ctypes_elem = 类型转换器.Python类型转ctypes(elem_type)
|
|
466
|
+
import ctypes
|
|
467
|
+
|
|
468
|
+
self.transpiler.ctypes参数类型.append(ctypes.POINTER(ctypes_elem))
|
|
469
|
+
self.transpiler.ctypes参数类型.append(ctypes.c_int64)
|
|
470
|
+
else:
|
|
471
|
+
self.抛出错误(f"Complex list type {py_type} not supported for JIT args", node)
|
|
472
|
+
|
|
473
|
+
else:
|
|
474
|
+
# 为 Set 和 Map 类型添加相应的头文件
|
|
475
|
+
if str(c_type).startswith("std::unordered_set"):
|
|
476
|
+
self.transpiler.包含头文件.add("<unordered_set>")
|
|
477
|
+
|
|
478
|
+
# 对于Set类型,创建标准集合对象以支持方法调用
|
|
479
|
+
from .std_set import 标准集合
|
|
480
|
+
from .cpp类型 import 集合初始化列表
|
|
481
|
+
|
|
482
|
+
# 获取元素类型
|
|
483
|
+
origin = getattr(py_type, "__origin__", None)
|
|
484
|
+
args = getattr(py_type, "__args__", [])
|
|
485
|
+
if origin is set and args:
|
|
486
|
+
elem_type = self.解析类型(args[0])
|
|
487
|
+
elem_type_str = str(elem_type)
|
|
488
|
+
else:
|
|
489
|
+
# 从 c_type 中提取类型,例如 "std::unordered_set<int>" -> "int"
|
|
490
|
+
if str(c_type).startswith("std::unordered_set<") and str(c_type).endswith(">"):
|
|
491
|
+
elem_type_str = str(c_type)[20:-1] # 去掉 "std::unordered_set<" 和 ">"
|
|
492
|
+
else:
|
|
493
|
+
elem_type_str = "int64_t" # fallback
|
|
494
|
+
|
|
495
|
+
# 创建集合初始化列表和标准集合对象
|
|
496
|
+
init_list = 集合初始化列表("", elem_type_str)
|
|
497
|
+
var = 标准集合(init_list, name, True)
|
|
498
|
+
self.transpiler.参数变量[name] = var
|
|
499
|
+
|
|
500
|
+
elif str(c_type).startswith("std::unordered_map"):
|
|
501
|
+
self.transpiler.包含头文件.add("<unordered_map>")
|
|
502
|
+
# 如果键或值类型包含string,添加string头文件
|
|
503
|
+
if "std::string" in str(c_type) or "string" in str(c_type):
|
|
504
|
+
self.transpiler.包含头文件.add("<string>")
|
|
505
|
+
|
|
506
|
+
# 对于Map类型,创建标准字典对象以支持方法调用
|
|
507
|
+
from .std_map import 标准无序映射
|
|
508
|
+
from .cpp类型 import 字典初始化列表
|
|
509
|
+
|
|
510
|
+
# 获取键值类型
|
|
511
|
+
origin = getattr(py_type, "__origin__", None)
|
|
512
|
+
args = getattr(py_type, "__args__", [])
|
|
513
|
+
if origin is dict and len(args) == 2:
|
|
514
|
+
key_type = self.解析类型(args[0])
|
|
515
|
+
val_type = self.解析类型(args[1])
|
|
516
|
+
key_type_str = str(key_type)
|
|
517
|
+
val_type_str = str(val_type)
|
|
518
|
+
else:
|
|
519
|
+
# 从 c_type 中提取类型,例如 "std::unordered_map<int, std::string>" -> "int, std::string"
|
|
520
|
+
if str(c_type).startswith("std::unordered_map<") and str(c_type).endswith(">"):
|
|
521
|
+
type_part = str(c_type)[18:-1] # 去掉 "std::unordered_map<" 和 ">"
|
|
522
|
+
if "," in type_part:
|
|
523
|
+
key_type_str, val_type_str = type_part.split(",", 1)
|
|
524
|
+
key_type_str = key_type_str.strip()
|
|
525
|
+
val_type_str = val_type_str.strip()
|
|
526
|
+
else:
|
|
527
|
+
key_type_str = "int64_t"
|
|
528
|
+
val_type_str = "int64_t"
|
|
529
|
+
else:
|
|
530
|
+
key_type_str = "int64_t"
|
|
531
|
+
val_type_str = "int64_t"
|
|
532
|
+
|
|
533
|
+
# 创建字典初始化列表和标准字典对象
|
|
534
|
+
init_list = 字典初始化列表("", key_type_str, val_type_str)
|
|
535
|
+
var = 标准无序映射(init_list, name, True)
|
|
536
|
+
self.transpiler.参数变量[name] = var
|
|
537
|
+
|
|
538
|
+
else:
|
|
539
|
+
# 其他类型,创建普通C变量
|
|
540
|
+
var = C变量(str(c_type), name, True)
|
|
541
|
+
self.transpiler.参数变量[name] = var
|
|
542
|
+
|
|
543
|
+
if not self.transpiler.可执行文件名:
|
|
544
|
+
self.transpiler.ctypes参数类型.append(
|
|
545
|
+
类型转换器.Python类型转ctypes(py_type)
|
|
546
|
+
)
|