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

Commit 0fd0305

Browse files
committed
增强命名空间导入功能,支持从库命名空间中查找和调用函数,并优化函数调用的处理逻辑和调试信息输出。
1 parent a8f4291 commit 0fd0305

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/interpreter/statement_executor.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ impl<'a> StatementExecutor for Interpreter<'a> {
331331
// 作用域级别导入:将命名空间下所有函数名映射到完整路径
332332
let ns_path = path.join("::");
333333
let mut import_map = self.namespace_import_stack.last_mut().unwrap();
334+
335+
// 检查代码命名空间中的函数
334336
for (full_path, _) in &self.namespaced_functions {
335337
if full_path.starts_with(&ns_path) {
336338
// 获取函数名
@@ -340,6 +342,24 @@ impl<'a> StatementExecutor for Interpreter<'a> {
340342
}
341343
}
342344
}
345+
346+
// 检查库命名空间中的函数(新增)
347+
if path.len() == 1 {
348+
let ns_name = &path[0];
349+
for (lib_name, lib_functions) in &self.imported_libraries {
350+
let ns_prefix = format!("{}::", ns_name);
351+
for (func_full_path, _) in lib_functions.iter() {
352+
if func_full_path.starts_with(&ns_prefix) {
353+
let parts: Vec<&str> = func_full_path.split("::").collect();
354+
if let Some(func_name) = parts.last() {
355+
import_map.entry(func_name.to_string()).or_insert_with(Vec::new).push(func_full_path.clone());
356+
debug_println(&format!("作用域导入库函数: {} -> {}", func_name, func_full_path));
357+
}
358+
}
359+
}
360+
}
361+
}
362+
343363
ExecutionResult::None
344364
},
345365
_ => handlers::namespace_handler::handle_import_namespace(self, ns_type, path)
@@ -459,9 +479,32 @@ impl<'a> StatementExecutor for Interpreter<'a> {
459479
if let Some(paths) = import_map.get(function_name) {
460480
if paths.len() == 1 {
461481
let full_path = &paths[0];
482+
483+
// 首先检查是否是代码命名空间函数
462484
if let Some(function) = self.namespaced_functions.get(full_path) {
463485
return self.call_function_impl(function, args);
464486
}
487+
488+
// 然后检查是否是库函数
489+
for (lib_name, lib_functions) in &self.imported_libraries {
490+
if let Some(func) = lib_functions.get(full_path) {
491+
debug_println(&format!("调用作用域导入的库函数: {} 来自库 '{}'", full_path, lib_name));
492+
let string_args = convert_values_to_string_args(&args);
493+
let result = func(string_args);
494+
// 尝试将结果转换为适当的值类型
495+
if let Ok(int_val) = result.parse::<i32>() {
496+
return Value::Int(int_val);
497+
} else if let Ok(float_val) = result.parse::<f64>() {
498+
return Value::Float(float_val);
499+
} else if result == "true" {
500+
return Value::Bool(true);
501+
} else if result == "false" {
502+
return Value::Bool(false);
503+
} else {
504+
return Value::String(result);
505+
}
506+
}
507+
}
465508
} else if paths.len() > 1 {
466509
panic!("函数名 '{}' 有多个匹配: {:?}", function_name, paths);
467510
}

0 commit comments

Comments
 (0)