fix(ast): 删掉ast结构

This commit is contained in:
jing
2026-03-09 12:42:12 +08:00
parent d4f4b77b1e
commit 03bd6d88e3
20 changed files with 147 additions and 594 deletions

View File

@@ -1,45 +1,44 @@
// 将 AST 翻译为极简 IR。
// 这里提供一个可运行的最小 IR 生成骨架,并把实现拆分 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
// 同学可以在对应 .cpp 内进一步完善更多语义。
// 将 ANTLR parse tree 翻译为极简 IR。
// 实现拆分 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "SysYParser.h"
#include "ir/IR.h"
namespace ast {
struct CompUnit;
struct Block;
struct VarDecl;
struct Stmt;
struct Expr;
namespace antlr4 {
namespace tree {
class ParseTree;
}
} // namespace antlr4
namespace ir {
class Module;
class Function;
class IRBuilder;
class Value;
class ConstantInt;
}
// 最小 IR 生成器的内部实现,接口分散在各 IRGen*.cpp。
class IRGenImpl {
public:
explicit IRGenImpl(ir::Module& module);
void Gen(const ast::CompUnit& ast);
std::unique_ptr<ir::Module> TakeModule();
void Gen(SysYParser::CompUnitContext& cu);
private:
void GenBlock(const ast::Block& block);
void GenVarDecl(const ast::VarDecl& decl);
void GenStmt(const ast::Stmt& stmt);
ir::Value* GenExpr(const ast::Expr& expr);
void GenFuncDef(SysYParser::FuncDefContext& func);
void GenBlock(SysYParser::BlockContext& block);
void GenStmt(SysYParser::StmtContext& stmt);
void GenVarDecl(SysYParser::VarDeclContext& decl);
void GenReturnStmt(SysYParser::ReturnStmtContext& ret);
ir::Value* GenExpr(SysYParser::ExpContext& expr);
ir::Value* GenAddExpr(SysYParser::AddExpContext& add);
ir::Value* GenPrimary(SysYParser::PrimaryContext& primary);
ir::Module& module_;
ir::Function* func_;
@@ -47,4 +46,4 @@ class IRGenImpl {
std::unordered_map<std::string, ir::Value*> locals_;
};
std::unique_ptr<ir::Module> GenerateIR(const ast::CompUnit& ast);
std::unique_ptr<ir::Module> GenerateIR(antlr4::tree::ParseTree* tree);