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:
+45
-6
@@ -4,13 +4,16 @@
|
||||
// 使用宏简化节点创建
|
||||
#define NEW(alloc, k) \
|
||||
AstNode* n = (AstNode*)arena_alloc_impl(alloc, sizeof(AstNode)); \
|
||||
n->kind = (k); n->type.kind = TYPE_UNKNOWN; \
|
||||
n->kind = (k); n->type.kind = TYPE_UNKNOWN; n->type.struct_name = NULL; \
|
||||
n->line = line; n->col = col
|
||||
|
||||
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) {
|
||||
NEW(alloc, AST_PROGRAM);
|
||||
n->as.program.functions = fns;
|
||||
n->as.program.fn_count = count;
|
||||
n->as.program.fn_count = fn_count;
|
||||
n->as.program.structs = structs;
|
||||
n->as.program.struct_count = struct_count;
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -23,9 +26,11 @@ AstNode* ast_make_function(void* alloc, const char* name, AstNode** params, size
|
||||
return n;
|
||||
}
|
||||
|
||||
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) {
|
||||
NEW(alloc, AST_PARAMETER);
|
||||
n->as.parameter.name = name; n->as.parameter.type = type;
|
||||
n->as.parameter.struct_type_name = struct_type_name;
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -35,10 +40,13 @@ AstNode* ast_make_block(void* alloc, AstNode** stmts, size_t count, int line, in
|
||||
return n;
|
||||
}
|
||||
|
||||
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) {
|
||||
NEW(alloc, AST_LET_STMT);
|
||||
n->as.let_stmt.name = name; n->as.let_stmt.annot_type = annot_type;
|
||||
n->as.let_stmt.has_type_annot = has_type_annot; n->as.let_stmt.is_mut = is_mut; n->as.let_stmt.init = init;
|
||||
n->as.let_stmt.has_type_annot = has_type_annot; n->as.let_stmt.is_mut = is_mut;
|
||||
n->as.let_stmt.init = init;
|
||||
n->as.let_stmt.struct_type_name = struct_type_name;
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -124,3 +132,34 @@ AstNode* ast_make_ident(void* alloc, const char* name, int line, int col) {
|
||||
n->as.ident.name = name;
|
||||
return n;
|
||||
}
|
||||
|
||||
// === 结构体相关工厂函数 ===
|
||||
|
||||
AstNode* ast_make_struct_decl(void* alloc, const char* name, AstNode** fields,
|
||||
size_t count, int line, int col) {
|
||||
NEW(alloc, AST_STRUCT_DECL);
|
||||
n->as.struct_decl.name = name;
|
||||
n->as.struct_decl.fields = fields;
|
||||
n->as.struct_decl.field_count = count;
|
||||
return n;
|
||||
}
|
||||
|
||||
AstNode* ast_make_struct_init(void* alloc, const char* type_name,
|
||||
const char** fnames, AstNode** fvals,
|
||||
size_t count, int line, int col) {
|
||||
NEW(alloc, AST_STRUCT_INIT);
|
||||
n->as.struct_init.type_name = type_name;
|
||||
n->as.struct_init.field_names = fnames;
|
||||
n->as.struct_init.field_values = fvals;
|
||||
n->as.struct_init.field_count = count;
|
||||
return n;
|
||||
}
|
||||
|
||||
AstNode* ast_make_field_access(void* alloc, AstNode* object, const char* field,
|
||||
int line, int col) {
|
||||
NEW(alloc, AST_FIELD_ACCESS);
|
||||
n->as.field_access.object = object;
|
||||
n->as.field_access.field = field;
|
||||
n->as.field_access.field_index = -1;
|
||||
return n;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user