feat: 类型别名 type alias (P1 #10)

- lexer: TOK_TYPE 关键字
- ast: AST_TYPE_ALIAS + AST_PROGRAM aliases数组
- parser: parse_type_expr() 抽取, type Name = Type; 解析
- sema: 别名注册+解析, 类型标注/struct字段/函数参数均支持
- 新增测试: 15_type_alias.l, 16_type_alias_struct.l

测试: 112 通过 (41+15+41+15)
This commit is contained in:
2026-06-05 13:54:58 +08:00
parent da9a7065dd
commit ab88ea2753
13 changed files with 235 additions and 79 deletions
+9 -2
View File
@@ -23,6 +23,7 @@ typedef enum {
AST_STRUCT_DECL, // struct Point { x: i64, y: i64 }
AST_STRUCT_INIT, // Point { x: 10, y: 20 }
AST_FIELD_ACCESS, // p.x
AST_TYPE_ALIAS, // type Meters = i64
} AstKind;
typedef enum {
@@ -48,7 +49,8 @@ struct AstNode {
union {
// AST_PROGRAM
struct { struct AstNode** functions; size_t fn_count;
struct AstNode** structs; size_t struct_count; } program;
struct AstNode** structs; size_t struct_count;
struct AstNode** type_aliases; size_t alias_count; } program;
// AST_FUNCTION
struct { const char* name; struct AstNode** params; size_t param_count;
TypeKind return_type; const char* return_struct_type_name;
@@ -87,12 +89,15 @@ struct AstNode {
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;
// AST_TYPE_ALIAS
struct { const char* name; TypeKind aliased_type; const char* aliased_struct_name; } type_alias;
} as;
};
// 创建节点的辅助函数(内存来自 arena,通过 void* 传递避免循环依赖)
AstNode* ast_make_program(void* alloc, AstNode** fns, size_t fn_count,
AstNode** structs, size_t struct_count, SourceLoc loc);
AstNode** structs, size_t struct_count,
AstNode** aliases, size_t alias_count, SourceLoc loc);
AstNode* ast_make_function(void* alloc, const char* name, AstNode** params, size_t pcount,
TypeKind ret, const char* ret_struct_name, AstNode* body, SourceLoc loc);
AstNode* ast_make_parameter(void* alloc, const char* name, TypeKind type, const char* struct_type_name, SourceLoc loc);
@@ -115,5 +120,7 @@ AstNode* ast_make_ident(void* alloc, const char* name, SourceLoc loc);
AstNode* ast_make_struct_decl(void* alloc, const char* name, AstNode** fields, size_t count, SourceLoc loc);
AstNode* ast_make_struct_init(void* alloc, const char* type_name, const char** fnames, AstNode** fvals, size_t count, SourceLoc loc);
AstNode* ast_make_field_access(void* alloc, AstNode* object, const char* field, SourceLoc loc);
AstNode* ast_make_type_alias(void* alloc, const char* name, TypeKind aliased,
const char* aliased_struct, SourceLoc loc);
#endif