@@ -32,9 +32,9 @@ pub fn debug_println(msg: &str) {
3232 }
3333}
3434
35- pub fn interpret ( program : & Program ) -> Value {
35+ pub fn interpret ( program : & Program , auto_namespace : bool ) -> Value {
3636 // 创建解释器
37- let mut interpreter = Interpreter :: new ( program) ;
37+ let mut interpreter = Interpreter :: new ( program, auto_namespace ) ;
3838
3939 // v0.7.4新增:执行变量生命周期分析
4040 interpreter. perform_lifetime_analysis ( ) ;
@@ -129,6 +129,8 @@ pub struct Interpreter<'a> {
129129 pub library_namespaces : HashMap < String , String > ,
130130 // 常量环境,键是常量名,值是常量值
131131 pub constants : HashMap < String , Value > ,
132+ // 自动命名空间查找配置
133+ pub auto_namespace_lookup : bool ,
132134 // 作用域级别命名空间导入栈(每层是一个map: 函数名->完整路径)
133135 pub namespace_import_stack : Vec < HashMap < String , Vec < String > > > ,
134136 // 类定义存储
@@ -153,7 +155,7 @@ pub struct Interpreter<'a> {
153155}
154156
155157impl < ' a > Interpreter < ' a > {
156- pub fn new ( program : & ' a Program ) -> Self {
158+ pub fn new ( program : & ' a Program , auto_namespace : bool ) -> Self {
157159 let mut functions = HashMap :: new ( ) ;
158160 let mut namespaced_functions = HashMap :: new ( ) ;
159161 let library_namespaces = HashMap :: new ( ) ;
@@ -182,6 +184,7 @@ impl<'a> Interpreter<'a> {
182184 global_namespace_imports : Vec :: new ( ) ,
183185 library_namespaces,
184186 constants, // 添加常量环境
187+ auto_namespace_lookup : auto_namespace, // 添加自动命名空间查找配置
185188 namespace_import_stack : vec ! [ HashMap :: new( ) ] , // 初始化栈,最外层一层
186189 classes : HashMap :: new ( ) ,
187190 enums : HashMap :: new ( ) ,
@@ -316,8 +319,8 @@ impl<'a> Interpreter<'a> {
316319 for path in & self . global_namespace_imports {
317320 let namespace_path = path. join ( "::" ) ;
318321 debug_println ( & format ! ( "应用全局命名空间导入: {}" , namespace_path) ) ;
319-
320- // 遍历命名空间中的所有函数
322+
323+ // 检查代码命名空间中的函数
321324 for ( full_path, _) in & self . namespaced_functions {
322325 // 检查函数是否属于指定的命名空间
323326 if full_path. starts_with ( & namespace_path) {
@@ -329,8 +332,28 @@ impl<'a> Interpreter<'a> {
329332 . entry ( func_name. to_string ( ) )
330333 . or_insert_with ( Vec :: new)
331334 . push ( full_path. clone ( ) ) ;
332-
333- debug_println ( & format ! ( " 导入全局函数: {}" , full_path) ) ;
335+
336+ debug_println ( & format ! ( " 导入全局代码函数: {}" , full_path) ) ;
337+ }
338+ }
339+ }
340+
341+ // 检查库命名空间中的函数(新增)
342+ if path. len ( ) == 1 {
343+ let ns_name = & path[ 0 ] ;
344+ for ( lib_name, lib_functions) in & self . imported_libraries {
345+ let ns_prefix = format ! ( "{}::" , ns_name) ;
346+ for ( func_full_path, _) in lib_functions. iter ( ) {
347+ if func_full_path. starts_with ( & ns_prefix) {
348+ let parts: Vec < & str > = func_full_path. split ( "::" ) . collect ( ) ;
349+ if let Some ( func_name) = parts. last ( ) {
350+ self . imported_namespaces
351+ . entry ( func_name. to_string ( ) )
352+ . or_insert_with ( Vec :: new)
353+ . push ( func_full_path. clone ( ) ) ;
354+ debug_println ( & format ! ( " 导入全局库函数: {}" , func_full_path) ) ;
355+ }
356+ }
334357 }
335358 }
336359 }
0 commit comments