语义检查与符号表
This commit is contained in:
@@ -1,5 +1,58 @@
|
|||||||
// 语义分析主流程:
|
// 极简语义分析:只检查变量是否先声明再使用。
|
||||||
// - 符号解析与绑定、类型检查、控制流规则检查
|
// 如需扩展,可在此基础上加入:
|
||||||
// - 记录/插入必要的隐式转换(或在节点上标注)
|
// - 常量折叠/类型检查
|
||||||
// - 输出为“带类型 / 符号 / 常量信息”的 AST
|
// - 函数签名/参数数量校验
|
||||||
|
// - 控制流相关检查(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 CheckBlock(const ast::Block& block) {
|
||||||
|
for (const auto& decl : block.varDecls) {
|
||||||
|
table_.Add(decl->name);
|
||||||
|
if (decl->init) CheckExpr(*decl->init);
|
||||||
|
}
|
||||||
|
for (const auto& stmt : block.stmts) {
|
||||||
|
if (auto ret = dynamic_cast<ast::ReturnStmt*>(stmt.get())) {
|
||||||
|
CheckExpr(*ret->value);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheckExpr(const ast::Expr& expr) {
|
||||||
|
if (auto var = dynamic_cast<const ast::VarExpr*>(&expr)) {
|
||||||
|
if (!table_.Contains(var->name)) {
|
||||||
|
throw std::runtime_error("使用了未定义的变量: " + var->name);
|
||||||
|
}
|
||||||
|
} else if (auto bin = dynamic_cast<const ast::BinaryExpr*>(&expr)) {
|
||||||
|
CheckExpr(*bin->lhs);
|
||||||
|
CheckExpr(*bin->rhs);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
SymbolTable& table_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::shared_ptr<ast::CompUnit> RunSema(std::shared_ptr<ast::CompUnit> ast) {
|
||||||
|
if (!ast || !ast->func || !ast->func->body) return ast;
|
||||||
|
SymbolTable table;
|
||||||
|
SemaVisitor visitor(table);
|
||||||
|
visitor.CheckBlock(*ast->func->body);
|
||||||
|
return ast;
|
||||||
|
}
|
||||||
|
|||||||
11
src/sem/Sema.h
Normal file
11
src/sem/Sema.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// 语义检查
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace ast {
|
||||||
|
struct CompUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回经过检查的 AST(当前直接返回原 AST)。
|
||||||
|
std::shared_ptr<ast::CompUnit> RunSema(std::shared_ptr<ast::CompUnit> ast);
|
||||||
@@ -2,3 +2,4 @@
|
|||||||
// - 支持嵌套作用域(块/函数/全局)
|
// - 支持嵌套作用域(块/函数/全局)
|
||||||
// - 变量/函数/参数/常量的注册、查找与遮蔽规则
|
// - 变量/函数/参数/常量的注册、查找与遮蔽规则
|
||||||
|
|
||||||
|
#include "sem/SymbolTable.h"
|
||||||
|
|||||||
16
src/sem/SymbolTable.h
Normal file
16
src/sem/SymbolTable.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
// 极简符号表:记录局部变量是否定义。
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
class SymbolTable {
|
||||||
|
public:
|
||||||
|
void Add(const std::string& name) { table_[name] = true; }
|
||||||
|
bool Contains(const std::string& name) const {
|
||||||
|
return table_.find(name) != table_.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<std::string, bool> table_;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user