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:
@@ -0,0 +1,6 @@
|
||||
type Meters = i64;
|
||||
fn main() -> i64 {
|
||||
let x: Meters = 100;
|
||||
print_i64(x);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
struct Point { x: i64, y: i64 }
|
||||
type P = Point;
|
||||
fn main() -> i64 {
|
||||
let p: P = Point { x: 10, y: 20 };
|
||||
print_i64(p.x);
|
||||
print_i64(p.y);
|
||||
return 0;
|
||||
}
|
||||
+6
-6
@@ -13,7 +13,7 @@ void test_codegen_simple_function() {
|
||||
AstNode* body = ast_make_block(&a, stmts, 1, loc_at(1, 1));
|
||||
AstNode* fn = ast_make_function(&a, "main", NULL, 0, TYPE_I64, NULL, body, loc_at(1, 1));
|
||||
AstNode* fns[] = { fn };
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, NULL, 0, loc_at(1, 1));
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, NULL, 0, NULL, 0, loc_at(1, 1));
|
||||
|
||||
const char* err = NULL;
|
||||
LLVMContextRef ctx = NULL;
|
||||
@@ -47,7 +47,7 @@ void test_codegen_if_else() {
|
||||
AstNode* body = ast_make_block(&a, stmts, 1, loc_at(1, 1));
|
||||
AstNode* fn = ast_make_function(&a, "main", NULL, 0, TYPE_I64, NULL, body, loc_at(1, 1));
|
||||
AstNode* fns[] = { fn };
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, NULL, 0, loc_at(1, 1));
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, NULL, 0, NULL, 0, loc_at(1, 1));
|
||||
|
||||
const char* err = NULL;
|
||||
LLVMContextRef ctx2 = NULL;
|
||||
@@ -78,7 +78,7 @@ void test_codegen_binary_ops() {
|
||||
AstNode* body = ast_make_block(&a, stmts, 1, loc_at(1, 1));
|
||||
AstNode* fn = ast_make_function(&a, "main", NULL, 0, TYPE_I64, NULL, body, loc_at(1, 1));
|
||||
AstNode* fns[] = { fn };
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, NULL, 0, loc_at(1, 1));
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, NULL, 0, NULL, 0, loc_at(1, 1));
|
||||
|
||||
const char* err = NULL;
|
||||
LLVMContextRef ctx3 = NULL;
|
||||
@@ -108,7 +108,7 @@ void test_codegen_while_loop() {
|
||||
AstNode* fn_body = ast_make_block(&a, stmts, 2, loc_at(1, 1));
|
||||
AstNode* fn = ast_make_function(&a, "main", NULL, 0, TYPE_I64, NULL, fn_body, loc_at(1, 1));
|
||||
AstNode* fns[] = { fn };
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, NULL, 0, loc_at(1, 1));
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, NULL, 0, NULL, 0, loc_at(1, 1));
|
||||
|
||||
const char* err = NULL;
|
||||
LLVMContextRef ctx4 = NULL;
|
||||
@@ -162,7 +162,7 @@ void test_codegen_struct_decl() {
|
||||
AstNode* fn = ast_make_function(&a, "main", NULL, 0, TYPE_I64, NULL, body, loc_at(1, 1));
|
||||
AstNode* fns[] = { fn };
|
||||
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, structs, 1, loc_at(1, 1));
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, structs, 1, NULL, 0, loc_at(1, 1));
|
||||
|
||||
const char* err = NULL;
|
||||
LLVMContextRef ctx = NULL;
|
||||
@@ -217,7 +217,7 @@ void test_codegen_struct_field_access() {
|
||||
AstNode* fn = ast_make_function(&a, "main", NULL, 0, TYPE_I64, NULL, body, loc_at(1, 1));
|
||||
AstNode* fns[] = { fn };
|
||||
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, structs, 1, loc_at(1, 1));
|
||||
AstNode* prog = ast_make_program(&a, fns, 1, structs, 1, NULL, 0, loc_at(1, 1));
|
||||
|
||||
const char* err = NULL;
|
||||
LLVMContextRef ctx = NULL;
|
||||
|
||||
@@ -168,6 +168,44 @@ void test_struct_field_count_mismatch() {
|
||||
arena_destroy(&a);
|
||||
}
|
||||
|
||||
/* === 类型别名测试 === */
|
||||
|
||||
void test_type_alias_ok() {
|
||||
Arena a = arena_create(1);
|
||||
size_t tc; ErrorInfo lex_err = {0};
|
||||
Token* toks = lex(&a,
|
||||
"type Meters = i64; fn main() { let x: Meters = 100; print_i64(x); return; }",
|
||||
"test", &tc, &lex_err);
|
||||
ASSERT(toks != NULL);
|
||||
ErrorInfo parse_err = {0};
|
||||
AstNode* ast = parse(&a, toks, tc, "test", &parse_err);
|
||||
ASSERT(ast != NULL);
|
||||
ASSERT(ast->as.program.alias_count == 1);
|
||||
|
||||
ErrorList errors; error_init(&errors);
|
||||
sema_analyze(ast, &errors, &a);
|
||||
ASSERT(errors.count == 0);
|
||||
arena_destroy(&a);
|
||||
}
|
||||
|
||||
void test_type_alias_struct() {
|
||||
Arena a = arena_create(1);
|
||||
size_t tc; ErrorInfo lex_err = {0};
|
||||
Token* toks = lex(&a,
|
||||
"struct Point { x: i64, y: i64 } type P = Point; fn main() { let p: P = Point { x: 1, y: 2 }; return; }",
|
||||
"test", &tc, &lex_err);
|
||||
ASSERT(toks != NULL);
|
||||
ErrorInfo parse_err = {0};
|
||||
AstNode* ast = parse(&a, toks, tc, "test", &parse_err);
|
||||
ASSERT(ast != NULL);
|
||||
ASSERT(ast->as.program.alias_count == 1);
|
||||
|
||||
ErrorList errors; error_init(&errors);
|
||||
sema_analyze(ast, &errors, &a);
|
||||
ASSERT(errors.count == 0);
|
||||
arena_destroy(&a);
|
||||
}
|
||||
|
||||
void test_struct_nested_type_ok() {
|
||||
Arena a = arena_create(1);
|
||||
size_t tc; ErrorInfo lex_err = {0};
|
||||
@@ -197,5 +235,7 @@ int main(void) {
|
||||
TEST_RUN(test_struct_undefined);
|
||||
TEST_RUN(test_struct_field_count_mismatch);
|
||||
TEST_RUN(test_struct_nested_type_ok);
|
||||
TEST_RUN(test_type_alias_ok);
|
||||
TEST_RUN(test_type_alias_struct);
|
||||
return test_summary();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user