|
|
|
|
@ -180,6 +180,9 @@ public:
|
|
|
|
|
if (is_array) {
|
|
|
|
|
// 处理数组维度
|
|
|
|
|
for (auto* dim_exp : ctx->constExp()) {
|
|
|
|
|
// ========== 绑定维度表达式 ==========
|
|
|
|
|
dim_exp->addExp()->accept(this); // 触发常量绑定(如 N)
|
|
|
|
|
|
|
|
|
|
int dim = table_.EvaluateConstExp(dim_exp);
|
|
|
|
|
if (dim <= 0) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "数组维度必须为正整数"));
|
|
|
|
|
@ -212,6 +215,10 @@ public:
|
|
|
|
|
if (is_global && has_init) {
|
|
|
|
|
CheckGlobalInitIsConst(ctx->initVal()); // 全局变量初始化必须是常量表达式
|
|
|
|
|
}
|
|
|
|
|
// ========== 绑定初始化表达式 ==========
|
|
|
|
|
if (ctx->initVal()) {
|
|
|
|
|
BindInitVal(ctx->initVal());
|
|
|
|
|
}
|
|
|
|
|
// 创建符号
|
|
|
|
|
Symbol sym;
|
|
|
|
|
sym.name = name;
|
|
|
|
|
@ -275,9 +282,18 @@ public:
|
|
|
|
|
type = ir::Type::GetArrayType(base_type, dims);
|
|
|
|
|
std::cout << "[DEBUG] 创建数组类型完成,IsArray: " << type->IsArray() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ========== 绑定维度表达式 ==========
|
|
|
|
|
for (auto* dim_exp : ctx->constExp()) {
|
|
|
|
|
dim_exp->addExp()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 求值初始化器
|
|
|
|
|
std::vector<SymbolTable::ConstValue> init_values;
|
|
|
|
|
if (ctx->constInitVal()) {
|
|
|
|
|
// ========== 绑定初始化表达式 ==========
|
|
|
|
|
BindConstInitVal(ctx->constInitVal());
|
|
|
|
|
|
|
|
|
|
init_values = table_.EvaluateConstInitVal(ctx->constInitVal(), dims, base_type);
|
|
|
|
|
std::cout << "[DEBUG] 初始化值数量: " << init_values.size() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
@ -294,6 +310,7 @@ public:
|
|
|
|
|
Symbol sym;
|
|
|
|
|
sym.name = name;
|
|
|
|
|
sym.kind = SymbolKind::Constant;
|
|
|
|
|
std::cout << "CheckConstDef: before addSymbol, sym.kind = " << (int)sym.kind << std::endl;
|
|
|
|
|
sym.type = type;
|
|
|
|
|
sym.scope_level = table_.currentScopeLevel();
|
|
|
|
|
sym.is_initialized = true;
|
|
|
|
|
@ -314,6 +331,10 @@ public:
|
|
|
|
|
std::cout << "[DEBUG] 数组常量,不存储单个常量值" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
table_.addSymbol(sym);
|
|
|
|
|
std::cout << "CheckConstDef: after addSymbol, sym.kind = " << (int)sym.kind << std::endl;
|
|
|
|
|
auto* stored = table_.lookup(name);
|
|
|
|
|
std::cout << "CheckConstDef: after addSymbol, stored const_def_ctx = " << stored->const_def_ctx << std::endl;
|
|
|
|
|
|
|
|
|
|
std::cout << "[DEBUG] 常量符号添加完成" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -1056,6 +1077,8 @@ private:
|
|
|
|
|
if (!sym) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "未定义的变量: " + name));
|
|
|
|
|
}
|
|
|
|
|
std::cout << "CheckLValue: found sym->name = " << sym->name
|
|
|
|
|
<< ", sym->kind = " << (int)sym->kind << std::endl;
|
|
|
|
|
|
|
|
|
|
if (sym->kind == SymbolKind::Variable && sym->var_def_ctx) {
|
|
|
|
|
sema_.BindVarUse(ctx, sym->var_def_ctx);
|
|
|
|
|
@ -1448,6 +1471,29 @@ private:
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "main 函数不能有参数"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindConstInitVal(SysYParser::ConstInitValContext* ctx) {
|
|
|
|
|
if (!ctx) return;
|
|
|
|
|
if (ctx->constExp()) {
|
|
|
|
|
// 遍历表达式树,触发 visitLVal 中的绑定
|
|
|
|
|
ctx->constExp()->addExp()->accept(this);
|
|
|
|
|
} else {
|
|
|
|
|
for (auto* sub : ctx->constInitVal()) {
|
|
|
|
|
BindConstInitVal(sub);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindInitVal(SysYParser::InitValContext* ctx) {
|
|
|
|
|
if (!ctx) return;
|
|
|
|
|
if (ctx->exp()) {
|
|
|
|
|
CheckExp(ctx->exp()); // 触发绑定
|
|
|
|
|
} else {
|
|
|
|
|
for (auto* sub : ctx->initVal()) {
|
|
|
|
|
BindInitVal(sub);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|