refactor(dev): unify user-facing diagnostics

This commit is contained in:
Lane0218
2026-03-11 21:42:33 +08:00
parent 9070775187
commit e9d7b4a058
10 changed files with 56 additions and 40 deletions

View File

@@ -4,6 +4,7 @@
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
for (auto* item : block.blockItem()) {
@@ -24,7 +25,7 @@ bool IRGenImpl::GenBlockItem(SysYParser::BlockItemContext& item) {
if (item.stmt()) {
return GenStmt(*item.stmt());
}
throw std::runtime_error("[irgen] 暂不支持的 blockItem 类型");
throw std::runtime_error(FormatError("irgen", "暂不支持的语句或声明"));
}
void IRGenImpl::GenDecl(SysYParser::DeclContext& decl) {
@@ -32,13 +33,13 @@ void IRGenImpl::GenDecl(SysYParser::DeclContext& decl) {
GenVarDecl(*decl.varDecl());
return;
}
throw std::runtime_error("[irgen] 暂不支持的声明类型");
throw std::runtime_error(FormatError("irgen", "暂不支持的声明类型"));
}
void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
const std::string name = decl.Ident()->getText();
if (locals_.find(name) != locals_.end()) {
throw std::runtime_error("[irgen] 重复定义变量: " + name);
throw std::runtime_error(FormatError("irgen", "重复定义变量: " + name));
}
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
locals_[name] = slot;

View File

@@ -6,15 +6,16 @@
#include "SysYParser.h"
#include "antlr4-runtime.h"
#include "ir/IR.h"
#include "utils/Log.h"
std::unique_ptr<ir::Module> GenerateIR(antlr4::tree::ParseTree* tree) {
if (!tree) {
throw std::runtime_error("[irgen] 语法树为空");
throw std::runtime_error(FormatError("irgen", "语法树为空"));
}
auto* cu = dynamic_cast<SysYParser::CompUnitContext*>(tree);
if (!cu) {
throw std::runtime_error("[irgen] 语法树根节点不是 compUnit");
throw std::runtime_error(FormatError("irgen", "语法树根节点不是 compUnit"));
}
auto module = std::make_unique<ir::Module>();

View File

@@ -4,10 +4,11 @@
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
ir::Value* IRGenImpl::GenExpr(SysYParser::ExpContext& expr) {
if (!expr.addExp()) {
throw std::runtime_error("[irgen] 非法表达式");
throw std::runtime_error(FormatError("irgen", "非法表达式"));
}
return GenAddExpr(*expr.addExp());
}
@@ -16,7 +17,7 @@ ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
// 当前表达式层次仍是最小实现,直接贴合 addExp -> primary 的语法形状。
const auto& terms = add.primary();
if (terms.empty()) {
throw std::runtime_error("[irgen] 空加法表达式");
throw std::runtime_error(FormatError("irgen", "空加法表达式"));
}
ir::Value* acc = GenPrimary(*terms[0]);
@@ -36,12 +37,12 @@ ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
const std::string name = primary.Ident()->getText();
auto it = locals_.find(name);
if (it == locals_.end()) {
throw std::runtime_error("[irgen] 变量未找到: " + name);
throw std::runtime_error(FormatError("irgen", "变量未找到: " + name));
}
return builder_.CreateLoad(it->second, ir::DefaultContext().NextTemp());
}
if (primary.exp()) {
return GenExpr(*primary.exp());
}
throw std::runtime_error("[irgen] 暂不支持的 primary 形式");
throw std::runtime_error(FormatError("irgen", "暂不支持的表达式形式"));
}

View File

@@ -4,6 +4,7 @@
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
namespace {
@@ -11,8 +12,9 @@ void VerifyFunctionStructure(const ir::Function& func) {
// 当前 IRGen 仍是单入口、顺序生成;这里在生成结束后补一层块终结校验。
for (const auto& bb : func.blocks()) {
if (!bb || !bb->HasTerminator()) {
throw std::runtime_error("[irgen] 基本块未正确终结: " +
(bb ? bb->name() : std::string("<null>")));
throw std::runtime_error(
FormatError("irgen", "基本块未正确终结: " +
(bb ? bb->name() : std::string("<null>"))));
}
}
}
@@ -24,17 +26,17 @@ IRGenImpl::IRGenImpl(ir::Module& module)
void IRGenImpl::Gen(SysYParser::CompUnitContext& cu) {
if (!cu.funcDef()) {
throw std::runtime_error("[irgen] 缺少 main 定义");
throw std::runtime_error(FormatError("irgen", "缺少 main 定义"));
}
GenFuncDef(*cu.funcDef());
}
void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
if (!func.block()) {
throw std::runtime_error("[irgen] 函数体为空");
throw std::runtime_error(FormatError("irgen", "函数体为空"));
}
if (!func.Ident()) {
throw std::runtime_error("[irgen] 缺少函数名");
throw std::runtime_error(FormatError("irgen", "缺少函数名"));
}
func_ = module_.CreateFunction(func.Ident()->getText(), ir::Type::Int32());

View File

@@ -4,18 +4,19 @@
#include "SysYParser.h"
#include "ir/IR.h"
#include "utils/Log.h"
bool IRGenImpl::GenStmt(SysYParser::StmtContext& stmt) {
if (stmt.returnStmt()) {
GenReturnStmt(*stmt.returnStmt());
return true;
}
throw std::runtime_error("[irgen] 暂不支持的语句类型");
throw std::runtime_error(FormatError("irgen", "暂不支持的语句类型"));
}
void IRGenImpl::GenReturnStmt(SysYParser::ReturnStmtContext& ret) {
if (!ret.exp()) {
throw std::runtime_error("[irgen] return 缺少表达式");
throw std::runtime_error(FormatError("irgen", "return 缺少表达式"));
}
ir::Value* v = GenExpr(*ret.exp());
builder_.CreateRet(v);