@@ -148,11 +148,12 @@ impl<'a> ExpressionEvaluator for Interpreter<'a> {
148148 Value :: None
149149 } ,
150150 Expression :: Super => {
151- // TODO: 实现super关键字
151+ // TODO: 实现super关键字,需要当前类上下文
152152 Value :: None
153153 } ,
154154 Expression :: StaticAccess ( class_name, member_name) => {
155- // TODO: 实现静态访问
155+ // TODO: 临时实现静态访问
156+ eprintln ! ( "静态访问 {}::{} 暂未完全实现" , class_name, member_name) ;
156157 Value :: None
157158 } ,
158159 }
@@ -238,6 +239,42 @@ impl<'a> Interpreter<'a> {
238239 }
239240 }
240241
242+ // 收集类的所有字段(包括继承的)
243+ fn collect_all_fields ( & self , class : & crate :: ast:: Class ) -> Vec < crate :: ast:: Field > {
244+ let mut all_fields = Vec :: new ( ) ;
245+
246+ // 递归收集父类字段
247+ if let Some ( ref super_class_name) = class. super_class {
248+ if let Some ( super_class) = self . classes . get ( super_class_name) {
249+ let parent_fields = self . collect_all_fields ( super_class) ;
250+ all_fields. extend ( parent_fields) ;
251+ }
252+ }
253+
254+ // 添加当前类的字段
255+ all_fields. extend ( class. fields . clone ( ) ) ;
256+
257+ all_fields
258+ }
259+
260+ // 查找方法(支持继承)
261+ fn find_method ( & self , class_name : & str , method_name : & str ) -> Option < ( & crate :: ast:: Class , & crate :: ast:: Method ) > {
262+ if let Some ( class) = self . classes . get ( class_name) {
263+ // 首先在当前类中查找
264+ for method in & class. methods {
265+ if method. name == method_name && !method. is_static {
266+ return Some ( ( class, method) ) ;
267+ }
268+ }
269+
270+ // 如果没找到,在父类中查找
271+ if let Some ( ref super_class_name) = class. super_class {
272+ return self . find_method ( super_class_name, method_name) ;
273+ }
274+ }
275+ None
276+ }
277+
241278 fn evaluate_ternary_operation ( & mut self , condition : & Expression , true_expr : & Expression , false_expr : & Expression ) -> Value {
242279 // 三元运算符:先计算条件,然后根据条件计算相应的表达式
243280 let condition_val = self . evaluate_expression ( condition) ;
@@ -619,29 +656,40 @@ impl<'a> Interpreter<'a> {
619656 }
620657 } ;
621658
659+ // 检查是否为抽象类
660+ if class. is_abstract {
661+ eprintln ! ( "错误: 不能实例化抽象类 '{}'" , class_name) ;
662+ return Value :: None ;
663+ }
664+
622665 // 计算构造函数参数
623666 let mut arg_values = Vec :: new ( ) ;
624667 for arg in args {
625668 arg_values. push ( self . evaluate_expression ( arg) ) ;
626669 }
627670
628- // 创建对象实例
671+ // 创建对象实例,包含继承的字段
629672 let mut fields = HashMap :: new ( ) ;
630673
674+ // 收集所有字段(包括继承的)
675+ let all_fields = self . collect_all_fields ( class) ;
676+
631677 // 初始化字段为默认值
632- for field in & class. fields {
633- let default_value = match field. initial_value {
634- Some ( ref expr) => self . evaluate_expression ( expr) ,
635- None => match field. field_type {
636- crate :: ast:: Type :: Int => Value :: Int ( 0 ) ,
637- crate :: ast:: Type :: Float => Value :: Float ( 0.0 ) ,
638- crate :: ast:: Type :: Bool => Value :: Bool ( false ) ,
639- crate :: ast:: Type :: String => Value :: String ( String :: new ( ) ) ,
640- crate :: ast:: Type :: Long => Value :: Long ( 0 ) ,
641- _ => Value :: None ,
642- }
643- } ;
644- fields. insert ( field. name . clone ( ) , default_value) ;
678+ for field in & all_fields {
679+ if !field. is_static { // 只初始化非静态字段
680+ let default_value = match field. initial_value {
681+ Some ( ref expr) => self . evaluate_expression ( expr) ,
682+ None => match field. field_type {
683+ crate :: ast:: Type :: Int => Value :: Int ( 0 ) ,
684+ crate :: ast:: Type :: Float => Value :: Float ( 0.0 ) ,
685+ crate :: ast:: Type :: Bool => Value :: Bool ( false ) ,
686+ crate :: ast:: Type :: String => Value :: String ( String :: new ( ) ) ,
687+ crate :: ast:: Type :: Long => Value :: Long ( 0 ) ,
688+ _ => Value :: None ,
689+ }
690+ } ;
691+ fields. insert ( field. name . clone ( ) , default_value) ;
692+ }
645693 }
646694
647695 // 调用构造函数
@@ -741,24 +789,21 @@ impl<'a> Interpreter<'a> {
741789
742790 match obj_value {
743791 Value :: Object ( obj) => {
744- // 查找类定义
745- let class = match self . classes . get ( & obj. class_name ) {
746- Some ( class) => * class,
747- None => {
748- eprintln ! ( "错误: 未找到类 '{}'" , obj. class_name) ;
749- return Value :: None ;
750- }
751- } ;
752-
753- // 查找方法
754- let method = match class. methods . iter ( ) . find ( |m| m. name == method_name) {
755- Some ( method) => method,
792+ // 使用继承支持的方法查找,克隆方法以避免借用冲突
793+ let method_clone = match self . find_method ( & obj. class_name , method_name) {
794+ Some ( ( _class, method) ) => method. clone ( ) ,
756795 None => {
757796 eprintln ! ( "错误: 类 '{}' 没有方法 '{}'" , obj. class_name, method_name) ;
758797 return Value :: None ;
759798 }
760799 } ;
761800
801+ // 检查抽象方法
802+ if method_clone. is_abstract {
803+ eprintln ! ( "错误: 不能调用抽象方法 '{}'" , method_name) ;
804+ return Value :: None ;
805+ }
806+
762807 // 计算参数
763808 let mut arg_values = Vec :: new ( ) ;
764809 for arg in args {
@@ -767,14 +812,14 @@ impl<'a> Interpreter<'a> {
767812
768813 // 创建方法参数环境
769814 let mut method_env = HashMap :: new ( ) ;
770- for ( i, param) in method . parameters . iter ( ) . enumerate ( ) {
815+ for ( i, param) in method_clone . parameters . iter ( ) . enumerate ( ) {
771816 if i < arg_values. len ( ) {
772817 method_env. insert ( param. name . clone ( ) , arg_values[ i] . clone ( ) ) ;
773818 }
774819 }
775820
776821 // 执行方法体,传递this对象和参数环境
777- self . execute_method_body_with_context ( & method . body , & obj, & method_env)
822+ self . execute_method_body_with_context ( & method_clone . body , & obj, & method_env)
778823 } ,
779824 _ => {
780825 eprintln ! ( "错误: 尝试在非对象上调用方法" ) ;
0 commit comments