@@ -617,6 +617,37 @@ impl<'a> ExpressionParser for ParserBase<'a> {
617617 let var_name = self . consume ( ) . unwrap ( ) ;
618618 self . consume ( ) ; // 消费 "--"
619619 Ok ( Expression :: PostDecrement ( var_name) )
620+ } else if self . peek ( ) == Some ( & "[" . to_string ( ) ) {
621+ // 数组索引访问
622+ self . consume ( ) ; // 消费 "["
623+ let index_expr = self . parse_expression ( ) ?;
624+ self . expect ( "]" ) ?;
625+
626+ let array_expr = Expression :: Variable ( name) ;
627+ let mut result = Expression :: ArrayAccess ( Box :: new ( array_expr) , Box :: new ( index_expr) ) ;
628+
629+ // 检查是否有后续的函数调用
630+ if self . peek ( ) == Some ( & "(" . to_string ( ) ) {
631+ self . consume ( ) ; // 消费 "("
632+
633+ let mut args = Vec :: new ( ) ;
634+ if self . peek ( ) != Some ( & ")" . to_string ( ) ) {
635+ loop {
636+ args. push ( self . parse_expression ( ) ?) ;
637+ if self . peek ( ) != Some ( & "," . to_string ( ) ) {
638+ break ;
639+ }
640+ self . consume ( ) ; // 消费 ","
641+ }
642+ }
643+
644+ self . expect ( ")" ) ?;
645+
646+ // 创建函数调用表达式,但使用数组访问作为函数表达式
647+ result = Expression :: FunctionPointerCall ( Box :: new ( result) , args) ;
648+ }
649+
650+ Ok ( result)
620651 } else if self . peek ( ) == Some ( & "." . to_string ( ) ) {
621652 // 字段访问或方法调用或链式调用
622653 self . consume ( ) ; // 消费 "."
@@ -735,6 +766,48 @@ impl<'a> ExpressionParser for ParserBase<'a> {
735766 self . consume ( ) ;
736767 Ok ( Type :: Auto )
737768 } ,
769+ "[" => {
770+ // 数组类型或函数指针数组类型: []int 或 []*fn(int, int) : int
771+ self . consume ( ) ; // 消费 "["
772+ self . expect ( "]" ) ?; // 期望 "]"
773+
774+ if self . peek ( ) == Some ( & "*" . to_string ( ) ) {
775+ self . consume ( ) ; // 消费 "*"
776+
777+ if self . peek ( ) == Some ( & "fn" . to_string ( ) ) {
778+ // 函数指针数组类型: []*fn(int, int) : int
779+ self . consume ( ) ; // 消费 "fn"
780+ self . expect ( "(" ) ?;
781+
782+ let mut param_types = Vec :: new ( ) ;
783+ if self . peek ( ) != Some ( & ")" . to_string ( ) ) {
784+ loop {
785+ param_types. push ( self . parse_expression_type ( ) ?) ;
786+ if self . peek ( ) != Some ( & "," . to_string ( ) ) {
787+ break ;
788+ }
789+ self . consume ( ) ; // 消费 ","
790+ }
791+ }
792+
793+ self . expect ( ")" ) ?;
794+ self . expect ( ":" ) ?;
795+ let return_type = Box :: new ( self . parse_expression_type ( ) ?) ;
796+
797+ let func_ptr_type = Type :: FunctionPointer ( param_types, return_type) ;
798+ Ok ( Type :: Array ( Box :: new ( func_ptr_type) ) )
799+ } else {
800+ // 指针数组类型: []*int
801+ let target_type = Box :: new ( self . parse_expression_type ( ) ?) ;
802+ let ptr_type = Type :: Pointer ( target_type) ;
803+ Ok ( Type :: Array ( Box :: new ( ptr_type) ) )
804+ }
805+ } else {
806+ // 普通数组类型: []int
807+ let element_type = Box :: new ( self . parse_expression_type ( ) ?) ;
808+ Ok ( Type :: Array ( element_type) )
809+ }
810+ } ,
738811 "*" => {
739812 // 指针类型或函数指针类型
740813 self . consume ( ) ; // 消费 "*"
0 commit comments