|
|
|
|
@ -452,4 +452,36 @@ std::any IRGenImpl::visitRelExp(SysYParser::RelExpContext* ctx) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "未知的关系操作符: " + op));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//赋值
|
|
|
|
|
ir::Value* IRGenImpl::EvalAssign(SysYParser::StmtContext* ctx) {
|
|
|
|
|
if (!ctx || !ctx->lVal() || !ctx->exp()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法赋值语句"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算右值
|
|
|
|
|
ir::Value* rhs = EvalExpr(*ctx->exp());
|
|
|
|
|
|
|
|
|
|
// 获取左值地址
|
|
|
|
|
std::string varName = ctx->lVal()->Ident()->getText();
|
|
|
|
|
|
|
|
|
|
// 从语义分析获取变量定义
|
|
|
|
|
auto* decl = sema_.ResolveVarUse(ctx->lVal());
|
|
|
|
|
if (!decl) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("irgen", "变量使用缺少语义绑定: " + varName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从storage_map_获取存储位置
|
|
|
|
|
auto it = storage_map_.find(decl);
|
|
|
|
|
if (it == storage_map_.end()) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("irgen", "变量声明缺少存储槽位: " + varName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成store指令
|
|
|
|
|
builder_.CreateStore(rhs, it->second);
|
|
|
|
|
|
|
|
|
|
return rhs;
|
|
|
|
|
}
|