feat: 枚举 enum (P1 #7)

- lexer: TOK_ENUM, TOK_COLON_COLON
- ast: AST_ENUM_DECL, AST_ENUM_VARIANT + AST_PROGRAM enums数组
- parser: enum Name { A, B } + Enum::Variant语法
- sema: SYM_ENUM, 变体验证, enum→i64类型兼容
- codegen: TYPE_ENUM→i64, 变体→ConstInt(索引值)
- 新增集成测试: 17_enum.l

测试: 121 通过 (41+15+47+18)
This commit is contained in:
2026-06-05 14:01:47 +08:00
parent ab88ea2753
commit 5237398245
14 changed files with 267 additions and 21 deletions
+41 -4
View File
@@ -6,14 +6,22 @@ static TypeKind current_return_type = TYPE_VOID;
static const char* current_return_struct_name = NULL;
static TypeKind promote(TypeKind a, TypeKind b) {
// 枚举在算术运算中视为 i64
if (a == TYPE_ENUM) a = TYPE_I64;
if (b == TYPE_ENUM) b = TYPE_I64;
if (a == TYPE_F64 || b == TYPE_F64) return TYPE_F64;
if (a == TYPE_I64 || b == TYPE_I64) return TYPE_I64;
if (a == TYPE_BOOL || b == TYPE_BOOL) return TYPE_BOOL;
return TYPE_ERROR;
}
static bool is_numeric(TypeKind t) { return t == TYPE_I64 || t == TYPE_F64; }
static bool is_comparable(TypeKind a, TypeKind b) { return a == b; }
static bool is_numeric(TypeKind t) { return t == TYPE_I64 || t == TYPE_F64 || t == TYPE_ENUM; }
static bool is_comparable(TypeKind a, TypeKind b) {
if (a == b) return true;
// 枚举可以参与整数比较
if ((a == TYPE_I64 && b == TYPE_ENUM) || (a == TYPE_ENUM && b == TYPE_I64)) return true;
return false;
}
// === 向前声明 ===
static void analyze_node(AstNode* node, Scope* scope, ErrorList* errors, Arena* a);
@@ -169,7 +177,8 @@ static void analyze_expr(AstNode* node, Scope* scope, ErrorList* errors, Arena*
expected_name ? expected_name : "struct",
actual_name ? actual_name : type_name(actual));
}
} else if (actual != expected) {
} else if (actual != expected &&
!(expected == TYPE_I64 && actual == TYPE_ENUM)) {
error_add(errors, "<sema>", node->loc.line, node->loc.col,
"参数 %zu 类型不匹配: 期望 '%s',得到 '%s'",
i + 1, type_name(expected), type_name(actual));
@@ -287,6 +296,26 @@ static void analyze_expr(AstNode* node, Scope* scope, ErrorList* errors, Arena*
break;
}
case AST_ENUM_VARIANT: {
Symbol* enum_sym = scope_lookup_struct(scope, node->as.enum_variant.enum_name);
if (!enum_sym || enum_sym->kind != SYM_ENUM) {
error_add(errors, "<sema>", node->loc.line, node->loc.col,
"未定义的枚举 '%s'", node->as.enum_variant.enum_name);
node->type.kind = TYPE_ERROR; break;
}
int vi = scope_enum_variant_index(enum_sym, node->as.enum_variant.variant_name);
if (vi < 0) {
error_add(errors, "<sema>", node->loc.line, node->loc.col,
"枚举 '%s' 没有变体 '%s'",
node->as.enum_variant.enum_name,
node->as.enum_variant.variant_name);
node->type.kind = TYPE_ERROR; break;
}
node->as.enum_variant.variant_index = vi;
node->type.kind = TYPE_ENUM;
break;
}
default: break;
}
}
@@ -308,6 +337,13 @@ static void analyze_node(AstNode* node, Scope* scope, ErrorList* errors, Arena*
}
}
}
// 注册枚举
for (size_t i = 0; i < node->as.program.enum_count; i++) {
AstNode* ed = node->as.program.enums[i];
scope_insert_enum(scope, a, ed->as.enum_decl.name,
ed->as.enum_decl.variants,
ed->as.enum_decl.variant_count);
}
// 第一遍:收集所有结构体定义
for (size_t i = 0; i < node->as.program.struct_count; i++) {
AstNode* sd = node->as.program.structs[i];
@@ -548,7 +584,8 @@ static void analyze_node(AstNode* node, Scope* scope, ErrorList* errors, Arena*
expected_name ? expected_name : "struct",
actual_name ? actual_name : type_name(actual));
}
} else if (actual != expected) {
} else if (actual != expected &&
!(expected == TYPE_I64 && actual == TYPE_ENUM)) {
error_add(errors, "<sema>", node->loc.line, node->loc.col,
"返回类型不匹配: 期望 '%s',得到 '%s'",
type_name(expected), type_name(actual));