refactor(dev): 移动头文件位置
This commit is contained in:
30
include/sem/Sema.h
Normal file
30
include/sem/Sema.h
Normal file
@@ -0,0 +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_;
|
||||
};
|
||||
|
||||
// 目前仅检查:
|
||||
// - 变量先声明后使用
|
||||
// - 局部变量不允许重复定义
|
||||
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit);
|
||||
17
include/sem/SymbolTable.h
Normal file
17
include/sem/SymbolTable.h
Normal file
@@ -0,0 +1,17 @@
|
||||
// 极简符号表:记录局部变量定义点。
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "SysYParser.h"
|
||||
|
||||
class SymbolTable {
|
||||
public:
|
||||
void Add(const std::string& name, SysYParser::VarDeclContext* decl);
|
||||
bool Contains(const std::string& name) const;
|
||||
SysYParser::VarDeclContext* Lookup(const std::string& name) const;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, SysYParser::VarDeclContext*> table_;
|
||||
};
|
||||
Reference in New Issue
Block a user