fix(frontend): 修复部分实现
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// 将 ANTLR parse tree 翻译为极简 IR。
|
||||
// 将语法树翻译为极简 IR。
|
||||
// 实现拆分在 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
|
||||
|
||||
#pragma once
|
||||
@@ -32,7 +32,7 @@ class IRGenImpl {
|
||||
private:
|
||||
void GenFuncDef(SysYParser::FuncDefContext& func);
|
||||
void GenBlock(SysYParser::BlockContext& block);
|
||||
void GenStmt(SysYParser::StmtContext& stmt);
|
||||
bool GenStmt(SysYParser::StmtContext& stmt);
|
||||
void GenVarDecl(SysYParser::VarDeclContext& decl);
|
||||
void GenReturnStmt(SysYParser::ReturnStmtContext& ret);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@
|
||||
|
||||
std::unique_ptr<ir::Module> GenerateIR(antlr4::tree::ParseTree* tree) {
|
||||
if (!tree) {
|
||||
throw std::runtime_error("[irgen] parse tree 为空");
|
||||
throw std::runtime_error("[irgen] 语法树为空");
|
||||
}
|
||||
|
||||
auto* cu = dynamic_cast<SysYParser::CompUnitContext*>(tree);
|
||||
if (!cu) {
|
||||
throw std::runtime_error("[irgen] parse tree 根节点不是 compUnit");
|
||||
throw std::runtime_error("[irgen] 语法树根节点不是 compUnit");
|
||||
}
|
||||
|
||||
auto module = std::make_unique<ir::Module>();
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
#include "SysYParser.h"
|
||||
#include "ir/IR.h"
|
||||
|
||||
void IRGenImpl::GenStmt(SysYParser::StmtContext& stmt) {
|
||||
bool IRGenImpl::GenStmt(SysYParser::StmtContext& stmt) {
|
||||
if (stmt.varDecl()) {
|
||||
GenVarDecl(*stmt.varDecl());
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
if (stmt.returnStmt()) {
|
||||
GenReturnStmt(*stmt.returnStmt());
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
throw std::runtime_error("[irgen] 暂不支持的语句类型");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user