Complete Lab2 IR generation and document process
This commit is contained in:
@@ -1,30 +1,40 @@
|
||||
// 基于语法树的语义检查与名称绑定。
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "SysYParser.h"
|
||||
|
||||
class SemanticContext {
|
||||
public:
|
||||
void BindVarUse(SysYParser::VarContext* use,
|
||||
SysYParser::VarDefContext* decl) {
|
||||
var_uses_[use] = decl;
|
||||
void BindLValue(SysYParser::LValueContext* use,
|
||||
antlr4::ParserRuleContext* def) {
|
||||
lvalue_defs_[use] = def;
|
||||
}
|
||||
|
||||
SysYParser::VarDefContext* ResolveVarUse(
|
||||
const SysYParser::VarContext* use) const {
|
||||
auto it = var_uses_.find(use);
|
||||
return it == var_uses_.end() ? nullptr : it->second;
|
||||
void BindFuncCall(SysYParser::FuncCallExpContext* use,
|
||||
SysYParser::FuncDefContext* def) {
|
||||
funccall_defs_[use] = def;
|
||||
}
|
||||
|
||||
antlr4::ParserRuleContext* ResolveLValue(
|
||||
const SysYParser::LValueContext* use) const {
|
||||
auto it = lvalue_defs_.find(const_cast<SysYParser::LValueContext*>(use));
|
||||
return it == lvalue_defs_.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
SysYParser::FuncDefContext* ResolveFuncCall(
|
||||
const SysYParser::FuncCallExpContext* use) const {
|
||||
auto it = funccall_defs_.find(const_cast<SysYParser::FuncCallExpContext*>(use));
|
||||
return it == funccall_defs_.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<const SysYParser::VarContext*,
|
||||
SysYParser::VarDefContext*>
|
||||
var_uses_;
|
||||
std::unordered_map<SysYParser::LValueContext*, antlr4::ParserRuleContext*>
|
||||
lvalue_defs_;
|
||||
std::unordered_map<SysYParser::FuncCallExpContext*,
|
||||
SysYParser::FuncDefContext*>
|
||||
funccall_defs_;
|
||||
};
|
||||
|
||||
// 目前仅检查:
|
||||
// - 变量先声明后使用
|
||||
// - 局部变量不允许重复定义
|
||||
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit);
|
||||
|
||||
@@ -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