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.
31 lines
760 B
31 lines
760 B
// 基于语法树的语义检查与名称绑定。
|
|
#pragma once
|
|
|
|
#include <unordered_map>
|
|
|
|
#include "SysYParser.h"
|
|
|
|
class SemanticContext {
|
|
public:
|
|
void BindVarUse(SysYParser::VarContext* use,
|
|
SysYParser::VarDefContext* decl) {
|
|
var_uses_[use] = decl;
|
|
}
|
|
|
|
SysYParser::VarDefContext* ResolveVarUse(
|
|
const SysYParser::VarContext* use) const {
|
|
auto it = var_uses_.find(use);
|
|
return it == var_uses_.end() ? nullptr : it->second;
|
|
}
|
|
|
|
private:
|
|
std::unordered_map<const SysYParser::VarContext*,
|
|
SysYParser::VarDefContext*>
|
|
var_uses_;
|
|
};
|
|
|
|
// 目前仅检查:
|
|
// - 变量先声明后使用
|
|
// - 局部变量不允许重复定义
|
|
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit);
|