feat: const 编译期常量 — const N = 42;

Token(77): +TOK_CONST, AST_LET_STMT 新增 is_const, Symbol 新增 const_value
sema: 字面量初始化自动折叠为编译期常量值

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-07 14:26:59 +08:00
parent f7710ede9d
commit 0a0667776a
8 changed files with 19 additions and 8 deletions
+4
View File
@@ -319,6 +319,10 @@ void analyze_node(AstNode* node, Scope* scope, ErrorList* errors, Arena* a) {
"变量 '%s' 重复定义", node->as.let_stmt.name);
} else {
sym->is_mut = node->as.let_stmt.is_mut;
sym->is_const = node->as.let_stmt.is_const;
if (node->as.let_stmt.is_const && node->as.let_stmt.init->kind == AST_LITERAL_EXPR) {
sym->const_value = node->as.let_stmt.init->as.literal.i64_val;
}
if (var_struct_name) {
sym->type = TYPE_STRUCT;
sym->struct_type_name = var_struct_name;
+2
View File
@@ -11,6 +11,8 @@ typedef struct Symbol {
SymbolKind kind;
TypeKind type; // 变量/参数的类型
bool is_mut; // 变量是否可变(可被赋值)
bool is_const; // 编译期常量 (const 声明)
int64_t const_value; // 编译期常量值
// 函数特有
TypeKind return_type;
const char* return_struct_type_name; // 返回类型为 struct 时的类型名