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

Commit 9681ad6

Browse files
committed
增强函数调用处理,支持从导入库中调用函数并添加自动命名空间查找功能,同时优化错误处理逻辑。
1 parent 8d3ec38 commit 9681ad6

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

src/interpreter/function_calls.rs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,34 @@ impl<'a> FunctionCallHandler for Interpreter<'a> {
210210
if paths.len() == 1 {
211211
// 只有一个匹配的函数,直接调用
212212
let full_path = &paths[0];
213+
214+
// 首先检查是否是代码命名空间函数
213215
if let Some(function) = self.namespaced_functions.get(full_path) {
214216
return self.call_function_impl(function, arg_values);
215-
} else {
216-
panic!("未找到函数: {}", full_path);
217217
}
218+
219+
// 然后检查是否是库函数
220+
for (lib_name, lib_functions) in &self.imported_libraries {
221+
if let Some(func) = lib_functions.get(full_path) {
222+
debug_println(&format!("调用导入的库函数: {} 来自库 '{}'", full_path, lib_name));
223+
let string_args = convert_values_to_string_args(&arg_values);
224+
let result = func(string_args);
225+
// 尝试将结果转换为适当的值类型
226+
if let Ok(int_val) = result.parse::<i32>() {
227+
return Value::Int(int_val);
228+
} else if let Ok(float_val) = result.parse::<f64>() {
229+
return Value::Float(float_val);
230+
} else if result == "true" {
231+
return Value::Bool(true);
232+
} else if result == "false" {
233+
return Value::Bool(false);
234+
} else {
235+
return Value::String(result);
236+
}
237+
}
238+
}
239+
240+
panic!("未找到函数: {}", full_path);
218241
} else {
219242
// 有多个匹配的函数,需要解决歧义
220243
panic!("函数名 '{}' 有多个匹配: {:?}", name, paths);
@@ -243,26 +266,29 @@ impl<'a> FunctionCallHandler for Interpreter<'a> {
243266
return Value::String(result);
244267
}
245268
}
246-
247-
// 尝试查找命名空间函数
248-
for ns_name in self.library_namespaces.keys() {
249-
let ns_func_name = format!("{}::{}", ns_name, name);
250-
debug_println(&format!("尝试在库 '{}' 中查找命名空间函数 '{}'", lib_name, ns_func_name));
251-
252-
if let Some(func) = lib_functions.get(&ns_func_name) {
253-
debug_println(&format!("在库 '{}' 中找到命名空间函数 '{}'", lib_name, ns_func_name));
254-
let result = func(string_args.clone());
255-
// 尝试将结果转换为适当的值类型
256-
if let Ok(int_val) = result.parse::<i32>() {
257-
return Value::Int(int_val);
258-
} else if let Ok(float_val) = result.parse::<f64>() {
259-
return Value::Float(float_val);
260-
} else if result == "true" {
261-
return Value::Bool(true);
262-
} else if result == "false" {
263-
return Value::Bool(false);
264-
} else {
265-
return Value::String(result);
269+
270+
// 只有在启用自动命名空间查找时才尝试查找命名空间函数
271+
if self.auto_namespace_lookup {
272+
// 尝试查找命名空间函数
273+
for ns_name in self.library_namespaces.keys() {
274+
let ns_func_name = format!("{}::{}", ns_name, name);
275+
debug_println(&format!("尝试在库 '{}' 中查找命名空间函数 '{}'", lib_name, ns_func_name));
276+
277+
if let Some(func) = lib_functions.get(&ns_func_name) {
278+
debug_println(&format!("在库 '{}' 中找到命名空间函数 '{}'", lib_name, ns_func_name));
279+
let result = func(string_args.clone());
280+
// 尝试将结果转换为适当的值类型
281+
if let Ok(int_val) = result.parse::<i32>() {
282+
return Value::Int(int_val);
283+
} else if let Ok(float_val) = result.parse::<f64>() {
284+
return Value::Float(float_val);
285+
} else if result == "true" {
286+
return Value::Bool(true);
287+
} else if result == "false" {
288+
return Value::Bool(false);
289+
} else {
290+
return Value::String(result);
291+
}
266292
}
267293
}
268294
}

0 commit comments

Comments
 (0)