fix(frontend): 规范一下前端实现
This commit is contained in:
@@ -32,6 +32,8 @@ class IRGenImpl {
|
||||
private:
|
||||
void GenFuncDef(SysYParser::FuncDefContext& func);
|
||||
void GenBlock(SysYParser::BlockContext& block);
|
||||
bool GenBlockItem(SysYParser::BlockItemContext& item);
|
||||
void GenDecl(SysYParser::DeclContext& decl);
|
||||
bool GenStmt(SysYParser::StmtContext& stmt);
|
||||
void GenVarDecl(SysYParser::VarDeclContext& decl);
|
||||
void GenReturnStmt(SysYParser::ReturnStmtContext& ret);
|
||||
|
||||
@@ -6,15 +6,34 @@
|
||||
#include "ir/IR.h"
|
||||
|
||||
void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
|
||||
for (auto* stmt : block.stmt()) {
|
||||
if (stmt) {
|
||||
if (GenStmt(*stmt)) {
|
||||
for (auto* item : block.blockItem()) {
|
||||
if (item) {
|
||||
if (GenBlockItem(*item)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool IRGenImpl::GenBlockItem(SysYParser::BlockItemContext& item) {
|
||||
if (item.decl()) {
|
||||
GenDecl(*item.decl());
|
||||
return false;
|
||||
}
|
||||
if (item.stmt()) {
|
||||
return GenStmt(*item.stmt());
|
||||
}
|
||||
throw std::runtime_error("[irgen] 暂不支持的 blockItem 类型");
|
||||
}
|
||||
|
||||
void IRGenImpl::GenDecl(SysYParser::DeclContext& decl) {
|
||||
if (decl.varDecl()) {
|
||||
GenVarDecl(*decl.varDecl());
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("[irgen] 暂不支持的声明类型");
|
||||
}
|
||||
|
||||
void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
|
||||
const std::string name = decl.Ident()->getText();
|
||||
if (locals_.find(name) != locals_.end()) {
|
||||
|
||||
@@ -19,8 +19,11 @@ void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
|
||||
if (!func.block()) {
|
||||
throw std::runtime_error("[irgen] 函数体为空");
|
||||
}
|
||||
if (!func.Ident()) {
|
||||
throw std::runtime_error("[irgen] 缺少函数名");
|
||||
}
|
||||
|
||||
func_ = module_.CreateFunction("main", ir::Type::Int32());
|
||||
func_ = module_.CreateFunction(func.Ident()->getText(), ir::Type::Int32());
|
||||
builder_.SetInsertPoint(func_->entry());
|
||||
locals_.clear();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user