Complete Lab2 IR generation and document process
This commit is contained in:
@@ -1,17 +1,30 @@
|
||||
// 极简符号表:记录局部变量定义点。
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "SysYParser.h"
|
||||
|
||||
struct Symbol {
|
||||
enum class Kind { Variable, Constant, Function, Parameter };
|
||||
Kind kind;
|
||||
antlr4::ParserRuleContext* def_ctx;
|
||||
bool is_const = false;
|
||||
bool is_array = false;
|
||||
// For functions, we can store pointers to their parameter types or just the
|
||||
// FuncDefContext*
|
||||
};
|
||||
|
||||
class SymbolTable {
|
||||
public:
|
||||
void Add(const std::string& name, SysYParser::VarDefContext* decl);
|
||||
bool Contains(const std::string& name) const;
|
||||
SysYParser::VarDefContext* Lookup(const std::string& name) const;
|
||||
SymbolTable();
|
||||
void PushScope();
|
||||
void PopScope();
|
||||
bool Add(const std::string& name, const Symbol& symbol);
|
||||
Symbol* Lookup(const std::string& name);
|
||||
bool IsInCurrentScope(const std::string& name) const;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, SysYParser::VarDefContext*> table_;
|
||||
std::vector<std::unordered_map<std::string, Symbol>> scopes_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user