style(ir): 纠正代码位置
This commit is contained in:
@@ -7,43 +7,47 @@
|
||||
|
||||
namespace {
|
||||
|
||||
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table);
|
||||
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table,
|
||||
SemanticContext& sema);
|
||||
|
||||
void CheckPrimary(SysYParser::PrimaryContext& primary,
|
||||
const SymbolTable& table) {
|
||||
const SymbolTable& table, SemanticContext& sema) {
|
||||
if (primary.Number()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (primary.Ident()) {
|
||||
const std::string name = primary.Ident()->getText();
|
||||
if (!table.Contains(name)) {
|
||||
auto* decl = table.Lookup(name);
|
||||
if (!decl) {
|
||||
throw std::runtime_error("[sema] 使用了未定义的变量: " + name);
|
||||
}
|
||||
sema.BindVarUse(&primary, decl);
|
||||
return;
|
||||
}
|
||||
|
||||
if (primary.exp()) {
|
||||
CheckExpr(*primary.exp(), table);
|
||||
CheckExpr(*primary.exp(), table, sema);
|
||||
return;
|
||||
}
|
||||
|
||||
throw std::runtime_error("[sema] 暂不支持的 primary 形式");
|
||||
}
|
||||
|
||||
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table) {
|
||||
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table,
|
||||
SemanticContext& sema) {
|
||||
if (!exp.addExp()) {
|
||||
throw std::runtime_error("[sema] 非法表达式");
|
||||
}
|
||||
const auto& terms = exp.addExp()->primary();
|
||||
for (auto* term : terms) {
|
||||
CheckPrimary(*term, table);
|
||||
CheckPrimary(*term, table, sema);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void RunSema(SysYParser::CompUnitContext& comp_unit) {
|
||||
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit) {
|
||||
auto* func = comp_unit.funcDef();
|
||||
if (!func || !func->block()) {
|
||||
throw std::runtime_error("[sema] 缺少 main 函数定义");
|
||||
@@ -53,6 +57,7 @@ void RunSema(SysYParser::CompUnitContext& comp_unit) {
|
||||
}
|
||||
|
||||
SymbolTable table;
|
||||
SemanticContext sema;
|
||||
bool seen_return = false;
|
||||
|
||||
const auto& items = func->block()->blockItem();
|
||||
@@ -74,14 +79,14 @@ void RunSema(SysYParser::CompUnitContext& comp_unit) {
|
||||
throw std::runtime_error("[sema] 重复定义变量: " + name);
|
||||
}
|
||||
if (decl->exp()) {
|
||||
CheckExpr(*decl->exp(), table);
|
||||
CheckExpr(*decl->exp(), table, sema);
|
||||
}
|
||||
table.Add(name);
|
||||
table.Add(name, decl);
|
||||
continue;
|
||||
}
|
||||
if (auto* stmt = item->stmt(); stmt && stmt->returnStmt()) {
|
||||
auto* ret = stmt->returnStmt();
|
||||
CheckExpr(*ret->exp(), table);
|
||||
CheckExpr(*ret->exp(), table, sema);
|
||||
seen_return = true;
|
||||
if (i + 1 != items.size()) {
|
||||
throw std::runtime_error("[sema] return 必须是 main 函数中的最后一条语句");
|
||||
@@ -94,4 +99,6 @@ void RunSema(SysYParser::CompUnitContext& comp_unit) {
|
||||
if (!seen_return) {
|
||||
throw std::runtime_error("[sema] main 函数必须包含 return 语句");
|
||||
}
|
||||
|
||||
return sema;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,30 @@
|
||||
// 基于语法树的极简语义检查。
|
||||
// 基于语法树的极简语义检查与名称绑定。
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "SysYParser.h"
|
||||
|
||||
class SemanticContext {
|
||||
public:
|
||||
void BindVarUse(SysYParser::PrimaryContext* use,
|
||||
SysYParser::VarDeclContext* decl) {
|
||||
var_uses_[use] = decl;
|
||||
}
|
||||
|
||||
SysYParser::VarDeclContext* ResolveVarUse(
|
||||
const SysYParser::PrimaryContext* use) const {
|
||||
auto it = var_uses_.find(use);
|
||||
return it == var_uses_.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<const SysYParser::PrimaryContext*,
|
||||
SysYParser::VarDeclContext*>
|
||||
var_uses_;
|
||||
};
|
||||
|
||||
// 目前仅检查:
|
||||
// - 变量先声明后使用
|
||||
// - 局部变量不允许重复定义
|
||||
void RunSema(SysYParser::CompUnitContext& comp_unit);
|
||||
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit);
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
// 极简符号表:记录局部变量是否定义。
|
||||
// 极简符号表:记录局部变量定义点。
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "SysYParser.h"
|
||||
|
||||
class SymbolTable {
|
||||
public:
|
||||
void Add(const std::string& name) { table_[name] = true; }
|
||||
void Add(const std::string& name, SysYParser::VarDeclContext* decl) {
|
||||
table_[name] = decl;
|
||||
}
|
||||
bool Contains(const std::string& name) const {
|
||||
return table_.find(name) != table_.end();
|
||||
}
|
||||
SysYParser::VarDeclContext* Lookup(const std::string& name) const {
|
||||
auto it = table_.find(name);
|
||||
return it == table_.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, bool> table_;
|
||||
std::unordered_map<std::string, SysYParser::VarDeclContext*> table_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user