fix(ast): 删掉ast结构
This commit is contained in:
@@ -1,39 +1,36 @@
|
||||
// 声明翻译模块:
|
||||
// - 处理全局变量与局部变量声明
|
||||
// - 处理数组初始化、空间分配与初值生成等
|
||||
|
||||
#include "irgen/IRGen.h"
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "ast/AstNodes.h"
|
||||
#include "SysYParser.h"
|
||||
#include "ir/IR.h"
|
||||
|
||||
void IRGenImpl::GenBlock(const ast::Block& block) {
|
||||
// 先为所有局部变量创建栈槽,使 alloca 聚集在入口块前部。
|
||||
for (const auto& decl : block.varDecls) {
|
||||
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
|
||||
locals_[decl->name] = slot;
|
||||
void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
|
||||
for (auto* stmt : block.stmt()) {
|
||||
if (stmt && stmt->varDecl()) {
|
||||
const std::string name = stmt->varDecl()->Ident()->getText();
|
||||
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
|
||||
locals_[name] = slot;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& decl : block.varDecls) {
|
||||
GenVarDecl(*decl);
|
||||
}
|
||||
for (const auto& stmt : block.stmts) {
|
||||
GenStmt(*stmt);
|
||||
for (auto* stmt : block.stmt()) {
|
||||
if (stmt) {
|
||||
GenStmt(*stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IRGenImpl::GenVarDecl(const ast::VarDecl& decl) {
|
||||
auto it = locals_.find(decl.name);
|
||||
void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
|
||||
const std::string name = decl.Ident()->getText();
|
||||
auto it = locals_.find(name);
|
||||
if (it == locals_.end()) {
|
||||
throw std::runtime_error("[irgen] 变量栈槽未创建: " + decl.name);
|
||||
throw std::runtime_error("[irgen] 变量栈槽未创建: " + name);
|
||||
}
|
||||
|
||||
ir::Value* init = nullptr;
|
||||
if (decl.init) {
|
||||
init = GenExpr(*decl.init);
|
||||
if (decl.exp()) {
|
||||
init = GenExpr(*decl.exp());
|
||||
} else {
|
||||
init = ir::DefaultContext().GetConstInt(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user