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 <string>
#include "sem/SymbolTable.h"
#include "utils/Log.h"
namespace {
@@ -18,7 +19,7 @@ void CheckPrimary(SysYParser::PrimaryContext& primary,
if (primary.Ident()) {
const std::string name = primary.Ident()->getText();
if (!table.Contains(name)) {
throw std::runtime_error("[sema] 使用了未定义的变量: " + name);
throw std::runtime_error(FormatError("sema", "使用了未定义的变量: " + name));
}
return;
}
@@ -28,12 +29,12 @@ void CheckPrimary(SysYParser::PrimaryContext& primary,
return;
}
throw std::runtime_error("[sema] 暂不支持的 primary 形式");
throw std::runtime_error(FormatError("sema", "暂不支持的表达式形式"));
}
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table) {
if (!exp.addExp()) {
throw std::runtime_error("[sema] 非法表达式");
throw std::runtime_error(FormatError("sema", "非法表达式"));
}
const auto& terms = exp.addExp()->primary();
for (auto* term : terms) {
@@ -46,10 +47,10 @@ void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table) {
void RunSema(SysYParser::CompUnitContext& comp_unit) {
auto* func = comp_unit.funcDef();
if (!func || !func->block()) {
throw std::runtime_error("[sema] 缺少 main 函数定义");
throw std::runtime_error(FormatError("sema", "缺少 main 函数定义"));
}
if (!func->Ident() || func->Ident()->getText() != "main") {
throw std::runtime_error("[sema] 入口函数必须命名为 main");
throw std::runtime_error(FormatError("sema", "入口函数必须命名为 main"));
}
SymbolTable table;
@@ -57,7 +58,8 @@ void RunSema(SysYParser::CompUnitContext& comp_unit) {
const auto& items = func->block()->blockItem();
if (items.empty()) {
throw std::runtime_error("[sema] main 函数不能为空,且必须以 return 结束");
throw std::runtime_error(
FormatError("sema", "main 函数不能为空,且必须以 return 结束"));
}
for (size_t i = 0; i < items.size(); ++i) {
@@ -66,12 +68,13 @@ void RunSema(SysYParser::CompUnitContext& comp_unit) {
continue;
}
if (seen_return) {
throw std::runtime_error("[sema] return 必须是 main 函数中的最后一条语句");
throw std::runtime_error(
FormatError("sema", "return 必须是 main 函数中的最后一条语句"));
}
if (auto* decl = item->decl() ? item->decl()->varDecl() : nullptr) {
const std::string name = decl->Ident()->getText();
if (table.Contains(name)) {
throw std::runtime_error("[sema] 重复定义变量: " + name);
throw std::runtime_error(FormatError("sema", "重复定义变量: " + name));
}
if (decl->exp()) {
CheckExpr(*decl->exp(), table);
@@ -84,14 +87,15 @@ void RunSema(SysYParser::CompUnitContext& comp_unit) {
CheckExpr(*ret->exp(), table);
seen_return = true;
if (i + 1 != items.size()) {
throw std::runtime_error("[sema] return 必须是 main 函数中的最后一条语句");
throw std::runtime_error(
FormatError("sema", "return 必须是 main 函数中的最后一条语句"));
}
continue;
}
throw std::runtime_error("[sema] 暂不支持的 blockItem 类型");
throw std::runtime_error(FormatError("sema", "暂不支持的语句或声明"));
}
if (!seen_return) {
throw std::runtime_error("[sema] main 函数必须包含 return 语句");
throw std::runtime_error(FormatError("sema", "main 函数必须包含 return 语句"));
}
}