fix(frontend): 修复部分实现
This commit is contained in:
@@ -1,58 +1,75 @@
|
||||
// 极简语义分析:只检查变量是否先声明再使用。
|
||||
// 如需扩展,可在此基础上加入:
|
||||
// - 常量折叠/类型检查
|
||||
// - 函数签名/参数数量校验
|
||||
// - 控制流相关检查(return 覆盖、break/continue 合法性等)
|
||||
#include "sem/Sema.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "ast/AstNodes.h"
|
||||
#include "sem/SymbolTable.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class SemaVisitor {
|
||||
public:
|
||||
explicit SemaVisitor(SymbolTable& table) : table_(table) {}
|
||||
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table);
|
||||
|
||||
void CheckBlock(const ast::Block& block) {
|
||||
for (const auto& item : block.items) {
|
||||
if (auto decl = dynamic_cast<ast::VarDecl*>(item.get())) {
|
||||
table_.Add(decl->name);
|
||||
if (decl->init) CheckExpr(*decl->init);
|
||||
continue;
|
||||
}
|
||||
if (auto ret = dynamic_cast<ast::ReturnStmt*>(item.get())) {
|
||||
CheckExpr(*ret->value);
|
||||
}
|
||||
}
|
||||
void CheckPrimary(SysYParser::PrimaryContext& primary,
|
||||
const SymbolTable& table) {
|
||||
if (primary.Number()) {
|
||||
return;
|
||||
}
|
||||
|
||||
void CheckExpr(const ast::Expr& expr) {
|
||||
if (auto var = dynamic_cast<const ast::VarExpr*>(&expr)) {
|
||||
if (!table_.Contains(var->name)) {
|
||||
throw std::runtime_error("[sema] 使用了未定义的变量: " + var->name);
|
||||
}
|
||||
} else if (auto bin = dynamic_cast<const ast::BinaryExpr*>(&expr)) {
|
||||
CheckExpr(*bin->lhs);
|
||||
CheckExpr(*bin->rhs);
|
||||
|
||||
if (primary.Ident()) {
|
||||
const std::string name = primary.Ident()->getText();
|
||||
if (!table.Contains(name)) {
|
||||
throw std::runtime_error("[sema] 使用了未定义的变量: " + name);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private:
|
||||
SymbolTable& table_;
|
||||
};
|
||||
if (primary.exp()) {
|
||||
CheckExpr(*primary.exp(), table);
|
||||
return;
|
||||
}
|
||||
|
||||
throw std::runtime_error("[sema] 暂不支持的 primary 形式");
|
||||
}
|
||||
|
||||
void CheckExpr(SysYParser::ExpContext& exp, const SymbolTable& table) {
|
||||
if (!exp.addExp()) {
|
||||
throw std::runtime_error("[sema] 非法表达式");
|
||||
}
|
||||
const auto& terms = exp.addExp()->primary();
|
||||
for (auto* term : terms) {
|
||||
CheckPrimary(*term, table);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<ast::CompUnit> RunSema(std::shared_ptr<ast::CompUnit> ast) {
|
||||
if (!ast || !ast->func || !ast->func->body) return ast;
|
||||
void RunSema(SysYParser::CompUnitContext& comp_unit) {
|
||||
auto* func = comp_unit.funcDef();
|
||||
if (!func || !func->block()) {
|
||||
throw std::runtime_error("[sema] 缺少 main 函数定义");
|
||||
}
|
||||
|
||||
SymbolTable table;
|
||||
SemaVisitor visitor(table);
|
||||
visitor.CheckBlock(*ast->func->body);
|
||||
return ast;
|
||||
|
||||
for (auto* stmt : func->block()->stmt()) {
|
||||
if (!stmt) {
|
||||
continue;
|
||||
}
|
||||
if (auto* decl = stmt->varDecl()) {
|
||||
const std::string name = decl->Ident()->getText();
|
||||
if (table.Contains(name)) {
|
||||
throw std::runtime_error("[sema] 重复定义变量: " + name);
|
||||
}
|
||||
if (decl->exp()) {
|
||||
CheckExpr(*decl->exp(), table);
|
||||
}
|
||||
table.Add(name);
|
||||
continue;
|
||||
}
|
||||
if (auto* ret = stmt->returnStmt()) {
|
||||
CheckExpr(*ret->exp(), table);
|
||||
break;
|
||||
}
|
||||
throw std::runtime_error("[sema] 暂不支持的语句类型");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user