|
|
|
|
@ -11,57 +11,74 @@ namespace {
|
|
|
|
|
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table,
|
|
|
|
|
SemanticContext& sema);
|
|
|
|
|
|
|
|
|
|
void CheckPrimary(SysYParser::PrimaryContext& primary,
|
|
|
|
|
const SymbolTable& table, SemanticContext& sema) {
|
|
|
|
|
if (primary.Number()) {
|
|
|
|
|
return;
|
|
|
|
|
std::string GetLValueName(SysYParser::LValueContext& lvalue) {
|
|
|
|
|
if (!lvalue.ID()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "非法左值"));
|
|
|
|
|
}
|
|
|
|
|
return lvalue.ID()->getText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckVar(SysYParser::VarContext& var, const SymbolTable& table,
|
|
|
|
|
SemanticContext& sema) {
|
|
|
|
|
if (!var.ID()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "非法变量引用"));
|
|
|
|
|
}
|
|
|
|
|
const std::string name = var.ID()->getText();
|
|
|
|
|
auto* decl = table.Lookup(name);
|
|
|
|
|
if (!decl) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "使用了未定义的变量: " + name));
|
|
|
|
|
}
|
|
|
|
|
sema.BindVarUse(&var, decl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (primary.Ident()) {
|
|
|
|
|
const std::string name = primary.Ident()->getText();
|
|
|
|
|
auto* decl = table.Lookup(name);
|
|
|
|
|
if (!decl) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "使用了未定义的变量: " + name));
|
|
|
|
|
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table,
|
|
|
|
|
SemanticContext& sema) {
|
|
|
|
|
if (auto* paren = dynamic_cast<SysYParser::ParenExpContext*>(&exp)) {
|
|
|
|
|
CheckExpr(*paren->exp(), table, sema);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (auto* var = dynamic_cast<SysYParser::VarExpContext*>(&exp)) {
|
|
|
|
|
if (!var->var()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "非法变量表达式"));
|
|
|
|
|
}
|
|
|
|
|
sema.BindVarUse(&primary, decl);
|
|
|
|
|
CheckVar(*var->var(), table, sema);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (primary.exp()) {
|
|
|
|
|
CheckExpr(*primary.exp(), table, sema);
|
|
|
|
|
if (dynamic_cast<SysYParser::NumberExpContext*>(&exp)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (auto* binary = dynamic_cast<SysYParser::AdditiveExpContext*>(&exp)) {
|
|
|
|
|
CheckExpr(*binary->exp(0), table, sema);
|
|
|
|
|
CheckExpr(*binary->exp(1), table, sema);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "暂不支持的表达式形式"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table,
|
|
|
|
|
SemanticContext& sema) {
|
|
|
|
|
if (!exp.addExp()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "非法表达式"));
|
|
|
|
|
}
|
|
|
|
|
const auto& terms = exp.addExp()->primary();
|
|
|
|
|
for (auto* term : terms) {
|
|
|
|
|
CheckPrimary(*term, table, sema);
|
|
|
|
|
SysYParser::FuncDefContext* FindMainFunc(SysYParser::CompUnitContext& comp_unit) {
|
|
|
|
|
auto* func = comp_unit.funcDef();
|
|
|
|
|
if (func && func->ID() && func->ID()->getText() == "main") {
|
|
|
|
|
return func;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit) {
|
|
|
|
|
auto* func = comp_unit.funcDef();
|
|
|
|
|
if (!func || !func->block()) {
|
|
|
|
|
auto* func = FindMainFunc(comp_unit);
|
|
|
|
|
if (!func || !func->blockStmt()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "缺少 main 函数定义"));
|
|
|
|
|
}
|
|
|
|
|
if (!func->Ident() || func->Ident()->getText() != "main") {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "入口函数必须命名为 main"));
|
|
|
|
|
if (!func->funcType() || !func->funcType()->INT()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "当前仅支持 int main"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SymbolTable table;
|
|
|
|
|
SemanticContext sema;
|
|
|
|
|
bool seen_return = false;
|
|
|
|
|
|
|
|
|
|
const auto& items = func->block()->blockItem();
|
|
|
|
|
const auto& items = func->blockStmt()->blockItem();
|
|
|
|
|
if (items.empty()) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("sema", "main 函数不能为空,且必须以 return 结束"));
|
|
|
|
|
@ -76,19 +93,33 @@ SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit) {
|
|
|
|
|
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 (auto* decl = item->decl()) {
|
|
|
|
|
if (!decl->btype() || !decl->btype()->INT()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "当前仅支持局部 int 变量声明"));
|
|
|
|
|
}
|
|
|
|
|
auto* var_def = decl->varDef();
|
|
|
|
|
if (!var_def || !var_def->lValue()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "非法变量声明"));
|
|
|
|
|
}
|
|
|
|
|
const std::string name = GetLValueName(*var_def->lValue());
|
|
|
|
|
if (table.Contains(name)) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "重复定义变量: " + name));
|
|
|
|
|
}
|
|
|
|
|
if (decl->exp()) {
|
|
|
|
|
CheckExpr(*decl->exp(), table, sema);
|
|
|
|
|
if (auto* init = var_def->initValue()) {
|
|
|
|
|
if (!init->exp()) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("sema", "当前不支持聚合初始化"));
|
|
|
|
|
}
|
|
|
|
|
CheckExpr(*init->exp(), table, sema);
|
|
|
|
|
}
|
|
|
|
|
table.Add(name, decl);
|
|
|
|
|
table.Add(name, var_def);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (auto* stmt = item->stmt(); stmt && stmt->returnStmt()) {
|
|
|
|
|
auto* ret = stmt->returnStmt();
|
|
|
|
|
if (!ret->exp()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "return 缺少表达式"));
|
|
|
|
|
}
|
|
|
|
|
CheckExpr(*ret->exp(), table, sema);
|
|
|
|
|
seen_return = true;
|
|
|
|
|
if (i + 1 != items.size()) {
|
|
|
|
|
|