style(sema): 规范符号表代码位置
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// 将语法树翻译为极简 IR。
|
// 将语法树翻译为 IR。
|
||||||
// 实现拆分在 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
|
// 实现拆分在 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
@@ -11,12 +11,6 @@
|
|||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
#include "sem/Sema.h"
|
#include "sem/Sema.h"
|
||||||
|
|
||||||
namespace antlr4 {
|
|
||||||
namespace tree {
|
|
||||||
class ParseTree;
|
|
||||||
}
|
|
||||||
} // namespace antlr4
|
|
||||||
|
|
||||||
namespace ir {
|
namespace ir {
|
||||||
class Module;
|
class Module;
|
||||||
class Function;
|
class Function;
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
|
|||||||
ir::Value* acc = GenPrimary(*terms[0]);
|
ir::Value* acc = GenPrimary(*terms[0]);
|
||||||
for (size_t i = 1; i < terms.size(); ++i) {
|
for (size_t i = 1; i < terms.size(); ++i) {
|
||||||
ir::Value* rhs = GenPrimary(*terms[i]);
|
ir::Value* rhs = GenPrimary(*terms[i]);
|
||||||
std::string name = ir::DefaultContext().NextTemp();
|
std::string name = module_.context().NextTemp();
|
||||||
acc = builder_.CreateBinary(ir::Opcode::Add, acc, rhs, name);
|
acc = builder_.CreateBinary(ir::Opcode::Add, acc, rhs, name);
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
@@ -30,7 +30,7 @@ ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
|
|||||||
|
|
||||||
ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
|
ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
|
||||||
if (primary.Number()) {
|
if (primary.Number()) {
|
||||||
return ir::DefaultContext().GetConstInt(std::stoi(primary.Number()->getText()));
|
return builder_.CreateConstInt(std::stoi(primary.Number()->getText()));
|
||||||
}
|
}
|
||||||
if (primary.Ident()) {
|
if (primary.Ident()) {
|
||||||
auto* decl = sema_.ResolveVarUse(&primary);
|
auto* decl = sema_.ResolveVarUse(&primary);
|
||||||
@@ -43,7 +43,7 @@ ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
|
|||||||
throw std::runtime_error("[irgen] 变量声明缺少存储槽位: " +
|
throw std::runtime_error("[irgen] 变量声明缺少存储槽位: " +
|
||||||
primary.Ident()->getText());
|
primary.Ident()->getText());
|
||||||
}
|
}
|
||||||
return builder_.CreateLoad(it->second, ir::DefaultContext().NextTemp());
|
return builder_.CreateLoad(it->second, module_.context().NextTemp());
|
||||||
}
|
}
|
||||||
if (primary.exp()) {
|
if (primary.exp()) {
|
||||||
return GenExpr(*primary.exp());
|
return GenExpr(*primary.exp());
|
||||||
|
|||||||
@@ -41,8 +41,7 @@ void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func_ = module_.CreateFunction(
|
func_ = module_.CreateFunction(
|
||||||
func.Ident()->getText(),
|
func.Ident()->getText(), module_.context().Int32());
|
||||||
std::make_shared<ir::Type>(ir::Type::Kind::Int32));
|
|
||||||
builder_.SetInsertPoint(func_->entry());
|
builder_.SetInsertPoint(func_->entry());
|
||||||
storage_map_.clear();
|
storage_map_.clear();
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// 基于语法树的极简语义检查与名称绑定。
|
// 基于语法树的语义检查与名称绑定。
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|||||||
@@ -3,3 +3,17 @@
|
|||||||
// - 变量/函数/参数/常量的注册、查找与遮蔽规则
|
// - 变量/函数/参数/常量的注册、查找与遮蔽规则
|
||||||
|
|
||||||
#include "sem/SymbolTable.h"
|
#include "sem/SymbolTable.h"
|
||||||
|
|
||||||
|
void SymbolTable::Add(const std::string& name,
|
||||||
|
SysYParser::VarDeclContext* decl) {
|
||||||
|
table_[name] = decl;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SymbolTable::Contains(const std::string& name) const {
|
||||||
|
return table_.find(name) != table_.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
SysYParser::VarDeclContext* SymbolTable::Lookup(const std::string& name) const {
|
||||||
|
auto it = table_.find(name);
|
||||||
|
return it == table_.end() ? nullptr : it->second;
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,16 +8,9 @@
|
|||||||
|
|
||||||
class SymbolTable {
|
class SymbolTable {
|
||||||
public:
|
public:
|
||||||
void Add(const std::string& name, SysYParser::VarDeclContext* decl) {
|
void Add(const std::string& name, SysYParser::VarDeclContext* decl);
|
||||||
table_[name] = decl;
|
bool Contains(const std::string& name) const;
|
||||||
}
|
SysYParser::VarDeclContext* Lookup(const std::string& name) const;
|
||||||
bool Contains(const std::string& name) const {
|
|
||||||
return table_.find(name) != table_.end();
|
|
||||||
}
|
|
||||||
SysYParser::VarDeclContext* Lookup(const std::string& name) const {
|
|
||||||
auto it = table_.find(name);
|
|
||||||
return it == table_.end() ? nullptr : it->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<std::string, SysYParser::VarDeclContext*> table_;
|
std::unordered_map<std::string, SysYParser::VarDeclContext*> table_;
|
||||||
|
|||||||
Reference in New Issue
Block a user