feat: 枚举关联数据 ADT — enum Option { Some(i64), None }
This commit is contained in:
+6
-6
@@ -243,11 +243,11 @@ void test_codegen_enum() {
|
||||
/* 构造 AST: enum Color { Red, Green, Blue }
|
||||
fn main() -> i64 { let c = Color::Green; print_i64(c); return 0; } */
|
||||
const char* cvariants[] = {"Red", "Green", "Blue"};
|
||||
AstNode* enum_decl = ast_make_enum_decl(&a, "Color", cvariants, 3, loc_at(1, 1));
|
||||
AstNode* enum_decl = ast_make_enum_decl(&a, "Color", cvariants, NULL, NULL, 3, loc_at(1, 1));
|
||||
AstNode* enums[] = { enum_decl };
|
||||
|
||||
/* let c = Color::Green; */
|
||||
AstNode* cv = ast_make_enum_variant(&a, "Color", "Green", loc_at(1, 1));
|
||||
AstNode* cv = ast_make_enum_variant(&a, "Color", "Green", NULL, loc_at(1, 1));
|
||||
cv->as.enum_variant.variant_index = 1; /* Green = index 1 */
|
||||
cv->type.kind = TYPE_ENUM;
|
||||
|
||||
@@ -426,7 +426,7 @@ void test_codegen_match() {
|
||||
|
||||
/* enum Color { Red, Green, Blue } */
|
||||
const char* variants[] = {"Red", "Green", "Blue"};
|
||||
AstNode* enum_decl = ast_make_enum_decl(&a, "Color", variants, 3, loc_at(1, 1));
|
||||
AstNode* enum_decl = ast_make_enum_decl(&a, "Color", variants, NULL, NULL, 3, loc_at(1, 1));
|
||||
AstNode* enums[] = { enum_decl };
|
||||
|
||||
/* fn main() -> i64 {
|
||||
@@ -445,7 +445,7 @@ void test_codegen_match() {
|
||||
else { return 0; }
|
||||
}
|
||||
*/
|
||||
AstNode* match_val = ast_make_enum_variant(&a, "Color", "Green", loc_at(1, 1));
|
||||
AstNode* match_val = ast_make_enum_variant(&a, "Color", "Green", NULL, loc_at(1, 1));
|
||||
match_val->type.kind = TYPE_ENUM;
|
||||
|
||||
AstNode* let_stmt = ast_make_let(&a, "__match_val", TYPE_UNKNOWN, false, false,
|
||||
@@ -456,7 +456,7 @@ void test_codegen_match() {
|
||||
match_ident->type.kind = TYPE_ENUM;
|
||||
|
||||
// if __match_val == Color::Red { return 1; }
|
||||
AstNode* red_variant = ast_make_enum_variant(&a, "Color", "Red", loc_at(1, 1));
|
||||
AstNode* red_variant = ast_make_enum_variant(&a, "Color", "Red", NULL, loc_at(1, 1));
|
||||
red_variant->type.kind = TYPE_ENUM;
|
||||
AstNode* red_cond = ast_make_binary(&a, OP_EQ, match_ident, red_variant, loc_at(1, 1));
|
||||
red_cond->type.kind = TYPE_BOOL;
|
||||
@@ -467,7 +467,7 @@ void test_codegen_match() {
|
||||
// if __match_val == Color::Green { return 2; }
|
||||
AstNode* match_ident2 = ast_make_ident(&a, "__match_val", loc_at(1, 1));
|
||||
match_ident2->type.kind = TYPE_ENUM;
|
||||
AstNode* green_variant = ast_make_enum_variant(&a, "Color", "Green", loc_at(1, 1));
|
||||
AstNode* green_variant = ast_make_enum_variant(&a, "Color", "Green", NULL, loc_at(1, 1));
|
||||
green_variant->type.kind = TYPE_ENUM;
|
||||
AstNode* green_cond = ast_make_binary(&a, OP_EQ, match_ident2, green_variant, loc_at(1, 1));
|
||||
green_cond->type.kind = TYPE_BOOL;
|
||||
|
||||
Reference in New Issue
Block a user