forked from NUDT-compiler/nudt-compiler-cpp
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.
42 lines
1.1 KiB
42 lines
1.1 KiB
// 符号表:记录局部变量/常量/参数定义。
|
|
#pragma once
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
#include "SysYParser.h"
|
|
|
|
enum class BaseTypeKind { Int, Float, Void };
|
|
|
|
struct TypeDesc {
|
|
BaseTypeKind base = BaseTypeKind::Int;
|
|
std::vector<int> dims; // 为空表示标量;数组维度允许首维为 -1 表示形参不定长
|
|
bool is_const = false;
|
|
};
|
|
|
|
enum class SymbolKind { Var, Const, Param };
|
|
|
|
struct SymbolEntry {
|
|
SymbolKind kind = SymbolKind::Var;
|
|
SysYParser::VarDefContext* var_decl = nullptr;
|
|
SysYParser::ConstDefContext* const_decl = nullptr;
|
|
SysYParser::FuncFParamContext* param_decl = nullptr;
|
|
TypeDesc type; // 记录类型信息
|
|
bool is_const = false;
|
|
std::optional<int> const_value;
|
|
};
|
|
|
|
class SymbolTable {
|
|
public:
|
|
void EnterScope();
|
|
void ExitScope();
|
|
|
|
bool ContainsInCurrentScope(const std::string& name) const;
|
|
void Add(const std::string& name, const SymbolEntry& entry);
|
|
const SymbolEntry* Lookup(const std::string& name) const;
|
|
|
|
private:
|
|
std::vector<std::unordered_map<std::string, SymbolEntry>> scopes_;
|
|
}; |