@@ -472,46 +472,14 @@ impl<'a> Evaluator for Interpreter<'a> {
472472 }
473473 }
474474
475- // 检查是否是std命名空间函数
476- let std_func_name = format ! ( "std::{}" , name) ;
477- if let Some ( lib_name) = self . library_namespaces . get ( "std" ) {
478- debug_println ( & format ! ( "尝试调用std命名空间函数: {} 在库 {}" , std_func_name, lib_name) ) ;
479-
480- // 将参数转换为字符串
481- let string_args = convert_values_to_string_args ( & arg_values) ;
482-
483- // 尝试调用库函数
484- match call_library_function ( lib_name, & std_func_name, string_args) {
485- Ok ( result) => {
486- debug_println ( & format ! ( "库函数调用成功: {}::{}" , lib_name, std_func_name) ) ;
487- // 尝试将结果转换为适当的值类型
488- if let Ok ( int_val) = result. parse :: < i32 > ( ) {
489- return Value :: Int ( int_val) ;
490- } else if let Ok ( float_val) = result. parse :: < f64 > ( ) {
491- return Value :: Float ( float_val) ;
492- } else if result == "true" {
493- return Value :: Bool ( true ) ;
494- } else if result == "false" {
495- return Value :: Bool ( false ) ;
496- } else {
497- return Value :: String ( result) ;
498- }
499- } ,
500- Err ( err) => {
501- debug_println ( & format ! ( "调用库函数失败: {}" , err) ) ;
502- // 继续尝试其他方式
503- }
504- }
505- }
506-
507- // 尝试在所有库中查找该函数作为std命名空间函数
475+ // 尝试在所有库中查找该函数
508476 let string_args = convert_values_to_string_args ( & arg_values) ;
509477 for ( lib_name, lib_functions) in & self . imported_libraries {
510- // 先尝试完整的命名空间函数名 (std::func_name)
511- debug_println ( & format ! ( "尝试在库 '{}' 中查找函数 '{}'" , lib_name, std_func_name ) ) ;
478+ // 尝试直接查找函数名
479+ debug_println ( & format ! ( "尝试在库 '{}' 中查找函数 '{}'" , lib_name, name ) ) ;
512480
513- if let Some ( func) = lib_functions. get ( & std_func_name ) {
514- debug_println ( & format ! ( "在库 '{}' 中找到函数 '{}'" , lib_name, std_func_name ) ) ;
481+ if let Some ( func) = lib_functions. get ( name ) {
482+ debug_println ( & format ! ( "在库 '{}' 中找到函数 '{}'" , lib_name, name ) ) ;
515483 let result = func ( string_args. clone ( ) ) ;
516484 // 尝试将结果转换为适当的值类型
517485 if let Ok ( int_val) = result. parse :: < i32 > ( ) {
@@ -526,6 +494,29 @@ impl<'a> Evaluator for Interpreter<'a> {
526494 return Value :: String ( result) ;
527495 }
528496 }
497+
498+ // 尝试查找命名空间函数
499+ for ns_name in self . library_namespaces . keys ( ) {
500+ let ns_func_name = format ! ( "{}::{}" , ns_name, name) ;
501+ debug_println ( & format ! ( "尝试在库 '{}' 中查找命名空间函数 '{}'" , lib_name, ns_func_name) ) ;
502+
503+ if let Some ( func) = lib_functions. get ( & ns_func_name) {
504+ debug_println ( & format ! ( "在库 '{}' 中找到命名空间函数 '{}'" , lib_name, ns_func_name) ) ;
505+ let result = func ( string_args. clone ( ) ) ;
506+ // 尝试将结果转换为适当的值类型
507+ if let Ok ( int_val) = result. parse :: < i32 > ( ) {
508+ return Value :: Int ( int_val) ;
509+ } else if let Ok ( float_val) = result. parse :: < f64 > ( ) {
510+ return Value :: Float ( float_val) ;
511+ } else if result == "true" {
512+ return Value :: Bool ( true ) ;
513+ } else if result == "false" {
514+ return Value :: Bool ( false ) ;
515+ } else {
516+ return Value :: String ( result) ;
517+ }
518+ }
519+ }
529520 }
530521
531522 // 如果不是导入的函数,再检查全局函数
@@ -580,6 +571,40 @@ impl<'a> Evaluator for Interpreter<'a> {
580571
581572 debug_println ( & format ! ( "调用命名空间函数: {}" , full_path) ) ;
582573
574+ // 检查是否是库命名空间函数
575+ if path. len ( ) >= 2 {
576+ let ns_name = & path[ 0 ] ;
577+ if let Some ( lib_name) = self . library_namespaces . get ( ns_name) {
578+ debug_println ( & format ! ( "检测到库命名空间: {} -> 库: {}" , ns_name, lib_name) ) ;
579+
580+ // 将参数转换为字符串
581+ let string_args = convert_values_to_string_args ( & arg_values) ;
582+
583+ // 尝试调用库函数 - 使用完整的命名空间路径
584+ match call_library_function ( lib_name, & full_path, string_args) {
585+ Ok ( result) => {
586+ debug_println ( & format ! ( "库函数调用成功: {} -> {}" , full_path, result) ) ;
587+ // 尝试将结果转换为适当的值类型
588+ if let Ok ( int_val) = result. parse :: < i32 > ( ) {
589+ return Value :: Int ( int_val) ;
590+ } else if let Ok ( float_val) = result. parse :: < f64 > ( ) {
591+ return Value :: Float ( float_val) ;
592+ } else if result == "true" {
593+ return Value :: Bool ( true ) ;
594+ } else if result == "false" {
595+ return Value :: Bool ( false ) ;
596+ } else {
597+ return Value :: String ( result) ;
598+ }
599+ } ,
600+ Err ( err) => {
601+ debug_println ( & format ! ( "调用库函数失败: {}" , err) ) ;
602+ // 继续尝试其他方式
603+ }
604+ }
605+ }
606+ }
607+
583608 // 查找命名空间函数
584609 if let Some ( function) = self . namespaced_functions . get ( & full_path) {
585610 self . call_function_impl ( function, arg_values)
@@ -612,6 +637,33 @@ impl<'a> Evaluator for Interpreter<'a> {
612637 }
613638 }
614639
640+ // 尝试在所有库中查找该命名空间函数
641+ if !found {
642+ let string_args = convert_values_to_string_args ( & arg_values) ;
643+ for ( lib_name, lib_functions) in & self . imported_libraries {
644+ debug_println ( & format ! ( "尝试在库 '{}' 中查找命名空间函数 '{}'" , lib_name, full_path) ) ;
645+
646+ if let Some ( func) = lib_functions. get ( & full_path) {
647+ debug_println ( & format ! ( "在库 '{}' 中找到命名空间函数 '{}'" , lib_name, full_path) ) ;
648+ let result = func ( string_args. clone ( ) ) ;
649+ found = true ;
650+
651+ // 尝试将结果转换为适当的值类型
652+ if let Ok ( int_val) = result. parse :: < i32 > ( ) {
653+ return Value :: Int ( int_val) ;
654+ } else if let Ok ( float_val) = result. parse :: < f64 > ( ) {
655+ return Value :: Float ( float_val) ;
656+ } else if result == "true" {
657+ return Value :: Bool ( true ) ;
658+ } else if result == "false" {
659+ return Value :: Bool ( false ) ;
660+ } else {
661+ return Value :: String ( result) ;
662+ }
663+ }
664+ }
665+ }
666+
615667 if !found {
616668 panic ! ( "未定义的命名空间函数: {}" , full_path) ;
617669 }
@@ -1175,116 +1227,6 @@ impl<'a> Executor for Interpreter<'a> {
11751227 }
11761228 }
11771229
1178- // 特殊处理std命名空间 (旧代码保留向后兼容)
1179- if path[ 0 ] == "std" {
1180- let func_name = & path[ 1 ] ;
1181- debug_println ( & format ! ( "处理std命名空间函数: {}" , func_name) ) ;
1182-
1183- // 将参数转换为字符串
1184- let string_args = convert_values_to_string_args ( & arg_values) ;
1185-
1186- // 检查是否有库明确注册了std命名空间
1187- if let Some ( lib_name) = self . library_namespaces . get ( "std" ) {
1188- debug_println ( & format ! ( "找到注册了std命名空间的库: {}" , lib_name) ) ;
1189-
1190- // 构建完整的函数名
1191- let full_func_name = format ! ( "std::{}" , func_name) ;
1192-
1193- // 尝试调用库函数
1194- match call_library_function ( lib_name, & full_func_name, string_args. clone ( ) ) {
1195- Ok ( result) => {
1196- debug_println ( & format ! ( "库函数调用成功: {} -> {}" , full_func_name, result) ) ;
1197- return ExecutionResult :: None ;
1198- } ,
1199- Err ( err) => {
1200- debug_println ( & format ! ( "调用库函数失败: {}" , err) ) ;
1201- // 继续尝试其他方式
1202- }
1203- }
1204- }
1205-
1206- // 尝试在所有库中查找该函数
1207- for ( lib_name, lib_functions) in & self . imported_libraries {
1208- // 先尝试完整的命名空间函数名 (std::func_name)
1209- let full_func_name = format ! ( "std::{}" , func_name) ;
1210- debug_println ( & format ! ( "尝试在库 '{}' 中查找函数 '{}'" , lib_name, full_func_name) ) ;
1211-
1212- if let Some ( func) = lib_functions. get ( & full_func_name) {
1213- debug_println ( & format ! ( "在库 '{}' 中找到函数 '{}' (完整路径)" , lib_name, full_func_name) ) ;
1214- let result = func ( string_args. clone ( ) ) ;
1215- debug_println ( & format ! ( "库函数调用成功: {} -> {}" , full_func_name, result) ) ;
1216- return ExecutionResult :: None ;
1217- }
1218-
1219- // 再尝试直接函数名
1220- debug_println ( & format ! ( "尝试在库 '{}' 中查找函数 '{}'" , lib_name, func_name) ) ;
1221- if let Some ( func) = lib_functions. get ( func_name) {
1222- debug_println ( & format ! ( "在库 '{}' 中找到函数 '{}'" , lib_name, func_name) ) ;
1223- let result = func ( string_args. clone ( ) ) ;
1224- debug_println ( & format ! ( "库函数调用成功: {} -> {}" , func_name, result) ) ;
1225- return ExecutionResult :: None ;
1226- }
1227-
1228- // 尝试cn_前缀的函数名
1229- let cn_func_name = format ! ( "cn_{}" , func_name) ;
1230- debug_println ( & format ! ( "尝试在库 '{}' 中查找函数 '{}'" , lib_name, cn_func_name) ) ;
1231- if let Some ( func) = lib_functions. get ( & cn_func_name) {
1232- debug_println ( & format ! ( "在库 '{}' 中找到函数 '{}' (cn_前缀)" , lib_name, cn_func_name) ) ;
1233- let result = func ( string_args. clone ( ) ) ;
1234- debug_println ( & format ! ( "库函数调用成功: {} -> {}" , cn_func_name, result) ) ;
1235- return ExecutionResult :: None ;
1236- }
1237- }
1238-
1239- // 如果在库中找不到,尝试查找全局映射的库函数
1240- if let Some ( ( lib_name, lib_func_name) ) = self . library_functions . get ( func_name) {
1241- debug_println ( & format ! ( "在库函数映射中找到: {} -> {}" , func_name, lib_func_name) ) ;
1242-
1243- // 调用库函数
1244- match call_library_function ( lib_name, lib_func_name, string_args. clone ( ) ) {
1245- Ok ( result) => {
1246- debug_println ( & format ! ( "库函数调用成功: {} -> {}" , lib_func_name, result) ) ;
1247- return ExecutionResult :: None ;
1248- } ,
1249- Err ( err) => {
1250- debug_println ( & format ! ( "调用库函数 {} 失败: {}" , lib_func_name, err) ) ;
1251- // 尝试调用cn_前缀的函数
1252- let cn_func_name = format ! ( "cn_{}" , func_name) ;
1253- match call_library_function ( lib_name, & cn_func_name, string_args. clone ( ) ) {
1254- Ok ( result) => {
1255- debug_println ( & format ! ( "库函数调用成功(cn_前缀): {} -> {}" , cn_func_name, result) ) ;
1256- return ExecutionResult :: None ;
1257- } ,
1258- Err ( _) => { }
1259- }
1260- }
1261- }
1262- }
1263-
1264- // 最后,尝试在io库中查找std命名空间函数
1265- debug_println ( "尝试在io库中查找std命名空间函数" ) ;
1266- let full_func_name = format ! ( "std::{}" , func_name) ;
1267- match call_library_function ( "io" , & full_func_name, string_args. clone ( ) ) {
1268- Ok ( result) => {
1269- debug_println ( & format ! ( "库函数调用成功: {} -> {}" , full_func_name, result) ) ;
1270- return ExecutionResult :: None ;
1271- } ,
1272- Err ( _) => {
1273- // 尝试调用cn_前缀的函数
1274- let cn_func_name = format ! ( "cn_{}" , func_name) ;
1275- match call_library_function ( "io" , & cn_func_name, string_args. clone ( ) ) {
1276- Ok ( result) => {
1277- debug_println ( & format ! ( "库函数调用成功(cn_前缀): {} -> {}" , cn_func_name, result) ) ;
1278- return ExecutionResult :: None ;
1279- } ,
1280- Err ( _) => {
1281- panic ! ( "未找到std命名空间函数: {}" , func_name) ;
1282- }
1283- }
1284- }
1285- }
1286- }
1287-
12881230 // 尝试作为普通命名空间函数调用
12891231 debug_println ( & format ! ( "尝试作为普通命名空间函数调用: {}" , full_path) ) ;
12901232
0 commit comments