feat: 数组+索引 [T;N], arr[i] (P1 #6)

- lexer: TOK_LBRACKET, TOK_RBRACKET
- type: TYPE_ARRAY + TypeInfo扩展(element_type/array_size)
- ast: AST_INDEX_EXPR, AST_ARRAY_ASSIGN_STMT
- parser: parse_type_expr()支持[T;N], Pratt加[索引], 数组元素赋值
- sema: 数组类型检查, 索引必须i64, 元素赋值类型匹配
- codegen: type_info_to_llvm(TYPE_ARRAY), GEP+load/store
- 新增集成测试: 18_array.l

测试: 136 通过 (41+15+59+21)
This commit is contained in:
2026-06-05 14:19:01 +08:00
parent 5237398245
commit 2923e7574d
14 changed files with 512 additions and 58 deletions
+16 -3
View File
@@ -26,6 +26,8 @@ typedef enum {
AST_TYPE_ALIAS, // type Meters = i64
AST_ENUM_DECL, // enum Color { Red, Green, Blue }
AST_ENUM_VARIANT, // Color::Red
AST_INDEX_EXPR, // arr[i]
AST_ARRAY_ASSIGN_STMT,// arr[i] = expr
} AstKind;
typedef enum {
@@ -38,7 +40,10 @@ typedef enum {
// 类型信息(语义分析阶段填充)
typedef struct {
TypeKind kind;
const char* struct_name; // TYPE_STRUCT 时的结构体类型名
const char* struct_name; // TYPE_STRUCT / TYPE_ENUM
TypeKind element_type; // TYPE_ARRAY: 元素类型的 TypeKind
const char* element_struct_name; // TYPE_ARRAY: 元素为 struct 时的类型名
int64_t array_size; // TYPE_ARRAY: 固定大小
} TypeInfo;
// AST 节点
@@ -64,7 +69,8 @@ struct AstNode {
struct { struct AstNode** stmts; size_t stmt_count; } block;
// AST_LET_STMT
struct { const char* name; TypeKind annot_type; bool has_type_annot; bool is_mut; struct AstNode* init;
const char* struct_type_name; } let_stmt;
const char* struct_type_name;
TypeKind annot_element_type; const char* annot_element_struct_name; int64_t annot_array_size; } let_stmt;
// AST_ASSIGN_STMT
struct { const char* name; struct AstNode* value; } assign_stmt;
// AST_IF_STMT
@@ -98,6 +104,10 @@ struct AstNode {
struct { const char* name; const char** variants; size_t variant_count; } enum_decl;
// AST_ENUM_VARIANT
struct { const char* enum_name; const char* variant_name; int variant_index; } enum_variant;
// AST_INDEX_EXPR
struct { struct AstNode* array; struct AstNode* index; } index_expr;
// AST_ARRAY_ASSIGN_STMT
struct { const char* name; struct AstNode* index; struct AstNode* value; } array_assign;
} as;
};
@@ -111,7 +121,8 @@ AstNode* ast_make_function(void* alloc, const char* name, AstNode** params, size
AstNode* ast_make_parameter(void* alloc, const char* name, TypeKind type, const char* struct_type_name, SourceLoc loc);
AstNode* ast_make_block(void* alloc, AstNode** stmts, size_t count, SourceLoc loc);
AstNode* ast_make_let(void* alloc, const char* name, TypeKind annot_type, bool has_type_annot,
bool is_mut, AstNode* init, const char* struct_type_name, SourceLoc loc);
bool is_mut, AstNode* init, const char* struct_type_name,
TypeKind annot_elem_type, const char* annot_elem_struct, int64_t annot_array_size, SourceLoc loc);
AstNode* ast_make_assign(void* alloc, const char* name, AstNode* value, SourceLoc loc);
AstNode* ast_make_if(void* alloc, AstNode* cond, AstNode* then_b, AstNode* else_b, SourceLoc loc);
AstNode* ast_make_while(void* alloc, AstNode* cond, AstNode* body, SourceLoc loc);
@@ -132,5 +143,7 @@ AstNode* ast_make_type_alias(void* alloc, const char* name, TypeKind aliased,
const char* aliased_struct, SourceLoc loc);
AstNode* ast_make_enum_decl(void* alloc, const char* name, const char** variants, size_t count, SourceLoc loc);
AstNode* ast_make_enum_variant(void* alloc, const char* enum_name, const char* variant_name, SourceLoc loc);
AstNode* ast_make_index_expr(void* alloc, AstNode* array, AstNode* index, SourceLoc loc);
AstNode* ast_make_array_assign(void* alloc, const char* name, AstNode* index, AstNode* value, SourceLoc loc);
#endif