fix(frontend): 修复部分实现

This commit is contained in:
jing
2026-03-09 15:37:36 +08:00
parent 8aec500b5b
commit e01995a33d
21 changed files with 321 additions and 165 deletions

View File

@@ -6,5 +6,5 @@ add_library(sem STATIC
target_link_libraries(sem PUBLIC
build_options
ast
${ANTLR4_RUNTIME_TARGET}
)

View File

@@ -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] 暂不支持的语句类型");
}
}

View File

@@ -1,11 +1,9 @@
// 语义检查
// 基于语法树的极简语义检查
#pragma once
#include <memory>
#include "SysYParser.h"
namespace ast {
struct CompUnit;
}
// 返回经过检查的 AST当前直接返回原 AST
std::shared_ptr<ast::CompUnit> RunSema(std::shared_ptr<ast::CompUnit> ast);
// 目前仅检查:
// - 变量先声明后使用
// - 局部变量不允许重复定义
void RunSema(SysYParser::CompUnitContext& comp_unit);