parent
e1c1f2a40d
commit
bb7f42e06e
@ -0,0 +1,50 @@
|
||||
// 将 AST 翻译为极简 IR。
|
||||
// 这里提供一个可运行的最小 IR 生成骨架,并把实现拆分到 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
|
||||
// 同学可以在对应 .cpp 内进一步完善更多语义。
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "ir/IR.h"
|
||||
|
||||
namespace ast {
|
||||
struct CompUnit;
|
||||
struct Block;
|
||||
struct VarDecl;
|
||||
struct Stmt;
|
||||
struct Expr;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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);
|
||||
|
||||
ir::Module& module_;
|
||||
ir::Function* func_;
|
||||
ir::IRBuilder builder_;
|
||||
std::unordered_map<std::string, ir::Value*> locals_;
|
||||
};
|
||||
|
||||
std::unique_ptr<ir::Module> GenerateIR(const ast::CompUnit& ast);
|
||||
@ -0,0 +1,7 @@
|
||||
// 轻量日志接口。
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define LOG_INFO(msg) std::cerr << "[info] " << msg << "\n"
|
||||
#define LOG_ERROR(msg) std::cerr << "[error] " << msg << "\n"
|
||||
Loading…
Reference in new issue