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.

25 lines
763 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include "sem/Sema.h"
class SymbolTable {
public:
// 语义检查使用典型的“作用域栈”模型:进入块时压栈,离开块时出栈。
void EnterScope();
void ExitScope();
// 只在当前作用域声明名字;若重名则返回 false由 Sema 决定报错策略。
bool Declare(const std::string& name, const SymbolInfo* symbol);
// 从内向外做词法作用域查找。
const SymbolInfo* Lookup(const std::string& name) const;
// 仅检查当前作用域,常用于发现同层重定义。
const SymbolInfo* LookupCurrent(const std::string& name) const;
private:
std::vector<std::unordered_map<std::string, const SymbolInfo*>> scopes_;
};