feat: 结构体 struct — 最后一项 P0 功能

- lexer: TOK_STRUCT, TOK_DOT 关键字和运算符
- ast: AST_STRUCT_DECL/STRUCT_INIT/FIELD_ACCESS 3 种新节点
- parser: struct 声明 + .field 访问 + Name{field:val} 初始化
- sema: struct 类型符号表,字段类型解析,初始化字段检查
- codegen: LLVMStructType + extractvalue/insertvalue 字段操作
- 新增集成测试: 12_struct.l, 13_struct_nested.l
- 基于 Codex 分析报告 P0 #4

所有 P0 功能已全部完成。
This commit is contained in:
2026-06-05 12:21:22 +08:00
parent 620cec4d57
commit b390d390f3
17 changed files with 1521 additions and 47 deletions
+25 -7
View File
@@ -20,6 +20,9 @@ typedef enum {
AST_CALL_EXPR,
AST_LITERAL_EXPR,
AST_IDENT_EXPR,
AST_STRUCT_DECL, // struct Point { x: i64, y: i64 }
AST_STRUCT_INIT, // Point { x: 10, y: 20 }
AST_FIELD_ACCESS, // p.x
} AstKind;
typedef enum {
@@ -32,6 +35,7 @@ typedef enum {
// 类型信息(语义分析阶段填充)
typedef struct {
TypeKind kind;
const char* struct_name; // TYPE_STRUCT 时的结构体类型名
} TypeInfo;
// AST 节点
@@ -44,16 +48,18 @@ struct AstNode {
// 节点特有数据(按 kind 解释)
union {
// AST_PROGRAM
struct { struct AstNode** functions; size_t fn_count; } program;
struct { struct AstNode** functions; size_t fn_count;
struct AstNode** structs; size_t struct_count; } program;
// AST_FUNCTION
struct { const char* name; struct AstNode** params; size_t param_count;
TypeKind return_type; struct AstNode* body; } function;
// AST_PARAMETER
struct { const char* name; TypeKind type; } parameter;
// AST_PARAMETER (也用作结构体字段: name + type)
struct { const char* name; TypeKind type; const char* struct_type_name; } parameter;
// AST_BLOCK
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; } 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;
// AST_ASSIGN_STMT
struct { const char* name; struct AstNode* value; } assign_stmt;
// AST_IF_STMT
@@ -74,16 +80,25 @@ struct AstNode {
struct { TypeKind lit_type; union { int64_t i64_val; double f64_val; bool bool_val; const char* str_val; }; } literal;
// AST_IDENT_EXPR
struct { const char* name; } ident;
// AST_STRUCT_DECL
struct { const char* name; struct AstNode** fields; size_t field_count; } struct_decl;
// AST_STRUCT_INIT
struct { const char* type_name; const char** field_names;
struct AstNode** field_values; size_t field_count; } struct_init;
// AST_FIELD_ACCESS
struct { struct AstNode* object; const char* field; int field_index; } field_access;
} as;
};
// 创建节点的辅助函数(内存来自 arena,通过 void* 传递避免循环依赖)
AstNode* ast_make_program(void* alloc, AstNode** fns, size_t count, int line, int col);
AstNode* ast_make_program(void* alloc, AstNode** fns, size_t fn_count,
AstNode** structs, size_t struct_count, int line, int col);
AstNode* ast_make_function(void* alloc, const char* name, AstNode** params, size_t pcount,
TypeKind ret, AstNode* body, int line, int col);
AstNode* ast_make_parameter(void* alloc, const char* name, TypeKind type, int line, int col);
AstNode* ast_make_parameter(void* alloc, const char* name, TypeKind type, const char* struct_type_name, int line, int col);
AstNode* ast_make_block(void* alloc, AstNode** stmts, size_t count, int line, int col);
AstNode* ast_make_let(void* alloc, const char* name, TypeKind annot_type, bool has_type_annot, bool is_mut, AstNode* init, int line, int col);
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, int line, int col);
AstNode* ast_make_assign(void* alloc, const char* name, AstNode* value, int line, int col);
AstNode* ast_make_if(void* alloc, AstNode* cond, AstNode* then_b, AstNode* else_b, int line, int col);
AstNode* ast_make_while(void* alloc, AstNode* cond, AstNode* body, int line, int col);
@@ -97,5 +112,8 @@ AstNode* ast_make_literal_f64(void* alloc, double val, int line, int col);
AstNode* ast_make_literal_bool(void* alloc, bool val, int line, int col);
AstNode* ast_make_literal_str(void* alloc, const char* val, int line, int col);
AstNode* ast_make_ident(void* alloc, const char* name, int line, int col);
AstNode* ast_make_struct_decl(void* alloc, const char* name, AstNode** fields, size_t count, int line, int col);
AstNode* ast_make_struct_init(void* alloc, const char* type_name, const char** fnames, AstNode** fvals, size_t count, int line, int col);
AstNode* ast_make_field_access(void* alloc, AstNode* object, const char* field, int line, int col);
#endif