You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.4 KiB
49 lines
1.4 KiB
// 基于语法树的语义检查与名称绑定。
|
|
#pragma once
|
|
|
|
#include <list>
|
|
#include <unordered_map>
|
|
|
|
#include "SysYParser.h"
|
|
#include "sem/SymbolTable.h"
|
|
|
|
class SemanticContext {
|
|
public:
|
|
SymbolEntry* RegisterSymbol(SymbolEntry symbol) {
|
|
symbols_.push_back(std::move(symbol));
|
|
return &symbols_.back();
|
|
}
|
|
|
|
void BindLValUse(SysYParser::LValContext* use, const SymbolEntry* symbol) {
|
|
lval_uses_[use] = symbol;
|
|
}
|
|
|
|
const SymbolEntry* ResolveLValUse(const SysYParser::LValContext* use) const {
|
|
auto it = lval_uses_.find(use);
|
|
return it == lval_uses_.end() ? nullptr : it->second;
|
|
}
|
|
|
|
void BindCallUse(SysYParser::UnaryExpContext* call,
|
|
const SymbolEntry* symbol) {
|
|
call_uses_[call] = symbol;
|
|
}
|
|
|
|
const SymbolEntry* ResolveCallUse(
|
|
const SysYParser::UnaryExpContext* call) const {
|
|
auto it = call_uses_.find(call);
|
|
return it == call_uses_.end() ? nullptr : it->second;
|
|
}
|
|
|
|
const std::list<SymbolEntry>& GetSymbols() const { return symbols_; }
|
|
|
|
private:
|
|
std::list<SymbolEntry> symbols_;
|
|
std::unordered_map<const SysYParser::LValContext*, const SymbolEntry*>
|
|
lval_uses_;
|
|
std::unordered_map<const SysYParser::UnaryExpContext*, const SymbolEntry*>
|
|
call_uses_;
|
|
};
|
|
|
|
// 基于 SysY.g4 规则进行语义分析,构建 IR 导向的符号绑定结果。
|
|
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit);
|