Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit 8d3ec38

Browse files
committed
增强命名空间导入功能,支持从库命名空间导入函数,并优化调试信息输出。
1 parent b807d0f commit 8d3ec38

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/interpreter/handlers/namespace_handler.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,39 @@ pub fn handle_import_namespace(interpreter: &mut Interpreter, ns_type: Namespace
1111
// 导入代码命名空间
1212
let namespace_path = path.join("::");
1313
debug_println(&format!("导入代码命名空间: {}", namespace_path));
14-
15-
// 遍历命名空间中的所有函数
14+
1615
let mut found = false;
16+
17+
// 首先检查是否是库命名空间(如 std)
18+
if path.len() == 1 {
19+
let ns_name = &path[0];
20+
21+
// 检查是否有对应的库命名空间
22+
for (lib_name, lib_functions) in &interpreter.imported_libraries {
23+
debug_println(&format!("检查库 '{}' 中的命名空间函数", lib_name));
24+
25+
// 查找以 namespace:: 开头的函数
26+
let ns_prefix = format!("{}::", ns_name);
27+
for (func_full_path, _) in lib_functions.iter() {
28+
if func_full_path.starts_with(&ns_prefix) {
29+
// 获取函数名(路径的最后一部分)
30+
let parts: Vec<&str> = func_full_path.split("::").collect();
31+
if let Some(func_name) = parts.last() {
32+
// 将函数添加到导入的命名空间列表
33+
interpreter.imported_namespaces
34+
.entry(func_name.to_string())
35+
.or_insert_with(Vec::new)
36+
.push(func_full_path.clone());
37+
38+
found = true;
39+
debug_println(&format!(" 导入库函数: {}", func_full_path));
40+
}
41+
}
42+
}
43+
}
44+
}
45+
46+
// 然后检查代码命名空间中的函数
1747
for (full_path, _) in &interpreter.namespaced_functions {
1848
// 检查函数是否属于指定的命名空间
1949
if full_path.starts_with(&namespace_path) {
@@ -25,13 +55,13 @@ pub fn handle_import_namespace(interpreter: &mut Interpreter, ns_type: Namespace
2555
.entry(func_name.to_string())
2656
.or_insert_with(Vec::new)
2757
.push(full_path.clone());
28-
58+
2959
found = true;
30-
debug_println(&format!(" 导入函数: {}", full_path));
60+
debug_println(&format!(" 导入代码函数: {}", full_path));
3161
}
3262
}
3363
}
34-
64+
3565
if !found {
3666
debug_println(&format!("警告: 命名空间 '{}' 中没有找到函数", namespace_path));
3767
}

0 commit comments

Comments
 (0)