1- use crate :: ast:: { Program , Function , Statement , Expression , BinaryOperator , CompareOperator } ;
1+ use crate :: ast:: { Program , Function , Statement , Expression , BinaryOperator , CompareOperator , NamespaceType } ;
22use crate :: vm:: bytecode:: { ByteCode , CompiledProgram , CompiledFunction , ClassInfo } ;
33use crate :: interpreter:: value:: Value ;
4+ use crate :: interpreter:: library_loader:: { load_library, call_library_function} ;
45use std:: collections:: HashMap ;
6+ use std:: sync:: Arc ;
57
68/// 编译器 - 将AST编译为字节码
79pub struct Compiler {
810 /// 当前编译的函数的字节码
911 bytecode : Vec < ByteCode > ,
10-
12+
1113 /// 常量池
1214 constants : Vec < Value > ,
13-
15+
1416 /// 局部变量映射 (变量名 -> 索引)
1517 locals : HashMap < String , u16 > ,
16-
18+
1719 /// 当前局部变量计数
1820 local_count : u16 ,
19-
21+
2022 /// 跳转标签映射
2123 labels : HashMap < String , u32 > ,
22-
24+
2325 /// 待回填的跳转地址
2426 pending_jumps : Vec < ( usize , String ) > ,
27+
28+ /// 导入的库映射 (库名 -> 函数映射)
29+ imported_libraries : HashMap < String , Arc < HashMap < String , crate :: interpreter:: library_loader:: LibraryFunction > > > ,
30+
31+ /// 函数索引映射 (函数名 -> 索引)
32+ function_indices : HashMap < String , u16 > ,
33+
34+ /// 下一个可用的函数索引
35+ next_function_index : u16 ,
2536}
2637
2738impl Compiler {
@@ -34,20 +45,54 @@ impl Compiler {
3445 local_count : 0 ,
3546 labels : HashMap :: new ( ) ,
3647 pending_jumps : Vec :: new ( ) ,
48+ imported_libraries : HashMap :: new ( ) ,
49+ function_indices : HashMap :: new ( ) ,
50+ next_function_index : 0 ,
3751 }
3852 }
3953
4054 /// 编译整个程序
4155 pub fn compile_program ( & mut self , program : & Program ) -> Result < CompiledProgram , String > {
4256 let mut compiled_functions = HashMap :: new ( ) ;
4357 let mut classes = HashMap :: new ( ) ;
44-
58+
59+ // 处理库导入
60+ for ( ns_type, path) in & program. imported_namespaces {
61+ match ns_type {
62+ NamespaceType :: Library => {
63+ if path. len ( ) != 1 {
64+ return Err ( "库名称应该是单个标识符" . to_string ( ) ) ;
65+ }
66+
67+ let lib_name = & path[ 0 ] ;
68+ println ! ( "🚀 VM: 加载库 {}" , lib_name) ;
69+
70+ // 加载库
71+ match crate :: interpreter:: library_loader:: load_library ( lib_name) {
72+ Ok ( functions) => {
73+ self . imported_libraries . insert ( lib_name. clone ( ) , functions) ;
74+ } ,
75+ Err ( err) => {
76+ return Err ( format ! ( "无法加载库 '{}': {}" , lib_name, err) ) ;
77+ }
78+ }
79+ } ,
80+ NamespaceType :: Code => {
81+ // 代码命名空间暂时跳过
82+ println ! ( "🚀 VM: 跳过代码命名空间 {:?}" , path) ;
83+ }
84+ }
85+ }
86+
87+ // 为所有函数分配索引
88+ self . assign_function_indices ( program) ;
89+
4590 // 编译所有函数
4691 for function in & program. functions {
4792 let compiled_func = self . compile_function ( function) ?;
4893 compiled_functions. insert ( function. name . clone ( ) , compiled_func) ;
4994 }
50-
95+
5196 // 编译所有类
5297 for class in & program. classes {
5398 let class_info = ClassInfo {
@@ -57,12 +102,13 @@ impl Compiler {
57102 } ;
58103 classes. insert ( class. name . clone ( ) , class_info) ;
59104 }
60-
105+
61106 Ok ( CompiledProgram {
62107 functions : compiled_functions,
63108 main_function : "main" . to_string ( ) ,
64109 global_constants : self . constants . clone ( ) ,
65110 classes,
111+ imported_libraries : self . imported_libraries . clone ( ) ,
66112 } )
67113 }
68114
@@ -270,7 +316,29 @@ impl Compiler {
270316 // 生成打印指令
271317 self . emit ( ByteCode :: Print ) ;
272318 } else {
273- return Err ( format ! ( "暂不支持静态方法调用: {}::{}" , class_name, method_name) ) ;
319+ // 检查是否是库函数调用
320+ let full_func_name = format ! ( "{}::{}" , class_name, method_name) ;
321+ let mut found_lib_name = None ;
322+
323+ // 先查找库函数
324+ for ( lib_name, lib_functions) in & self . imported_libraries {
325+ if lib_functions. contains_key ( & full_func_name) {
326+ found_lib_name = Some ( lib_name. clone ( ) ) ;
327+ break ;
328+ }
329+ }
330+
331+ if let Some ( lib_name) = found_lib_name {
332+ // 编译参数
333+ for arg in args {
334+ self . compile_expression ( arg) ?;
335+ }
336+
337+ // 生成库函数调用指令
338+ self . emit ( ByteCode :: CallLibrary ( lib_name, full_func_name, args. len ( ) as u8 ) ) ;
339+ } else {
340+ return Err ( format ! ( "暂不支持静态方法调用: {}::{}" , class_name, method_name) ) ;
341+ }
274342 }
275343 } ,
276344
@@ -287,13 +355,21 @@ impl Compiler {
287355 self . bytecode . push ( instruction) ;
288356 }
289357
290- /// 获取函数索引(简化版本)
291- fn get_function_index ( & self , name : & str ) -> u16 {
292- // 简单的哈希映射,实际应该有完整的函数表
293- match name {
294- "fib" => 1 ,
295- "main" => 0 ,
296- _ => 999 , // 未知函数
358+ /// 为所有函数分配索引
359+ fn assign_function_indices ( & mut self , program : & Program ) {
360+ self . function_indices . insert ( "main" . to_string ( ) , 0 ) ;
361+ self . next_function_index = 1 ;
362+
363+ for function in & program. functions {
364+ if function. name != "main" {
365+ self . function_indices . insert ( function. name . clone ( ) , self . next_function_index ) ;
366+ self . next_function_index += 1 ;
367+ }
297368 }
298369 }
370+
371+ /// 获取函数索引
372+ fn get_function_index ( & self , name : & str ) -> u16 {
373+ self . function_indices . get ( name) . copied ( ) . unwrap_or ( 999 )
374+ }
299375}
0 commit comments