fix(frontend): 修复部分实现

This commit is contained in:
jing
2026-03-09 15:37:36 +08:00
parent 8aec500b5b
commit e01995a33d
21 changed files with 321 additions and 165 deletions

View File

@@ -6,27 +6,22 @@
#include "ir/IR.h"
void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
for (auto* stmt : block.stmt()) {
if (stmt && stmt->varDecl()) {
const std::string name = stmt->varDecl()->Ident()->getText();
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
locals_[name] = slot;
}
}
for (auto* stmt : block.stmt()) {
if (stmt) {
GenStmt(*stmt);
if (GenStmt(*stmt)) {
break;
}
}
}
}
void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
const std::string name = decl.Ident()->getText();
auto it = locals_.find(name);
if (it == locals_.end()) {
throw std::runtime_error("[irgen] 变量栈槽未创建: " + name);
if (locals_.find(name) != locals_.end()) {
throw std::runtime_error("[irgen] 重复定义变量: " + name);
}
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
locals_[name] = slot;
ir::Value* init = nullptr;
if (decl.exp()) {
@@ -34,5 +29,5 @@ void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
} else {
init = ir::DefaultContext().GetConstInt(0);
}
builder_.CreateStore(init, it->second);
builder_.CreateStore(init, slot);
}