fix(ast): 删掉ast结构
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
# src/ 子目录构建脚本:各子目录独立维护 CMakeLists.txt,并在此聚合链接
|
# src/ 子目录构建脚本:各子目录独立维护 CMakeLists.txt,并在此聚合链接
|
||||||
|
|
||||||
add_subdirectory(utils)
|
add_subdirectory(utils)
|
||||||
add_subdirectory(ast)
|
|
||||||
add_subdirectory(sem)
|
|
||||||
add_subdirectory(ir)
|
add_subdirectory(ir)
|
||||||
add_subdirectory(frontend)
|
add_subdirectory(frontend)
|
||||||
add_subdirectory(irgen)
|
add_subdirectory(irgen)
|
||||||
@@ -13,7 +11,6 @@ add_executable(compiler
|
|||||||
)
|
)
|
||||||
target_link_libraries(compiler PRIVATE
|
target_link_libraries(compiler PRIVATE
|
||||||
frontend
|
frontend
|
||||||
sem
|
|
||||||
irgen
|
irgen
|
||||||
mir
|
mir
|
||||||
utils
|
utils
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
|
|
||||||
// AST 节点定义与实现:
|
|
||||||
// - 表达式、语句、声明、函数、类型等节点
|
|
||||||
// - 支持后续阶段在节点上附加信息(类型、符号绑定、常量值等)
|
|
||||||
#include "ast/AstNodes.h"
|
|
||||||
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
namespace ast {
|
|
||||||
|
|
||||||
NumberExpr::NumberExpr(int v) : value(v) {}
|
|
||||||
|
|
||||||
VarExpr::VarExpr(std::string n) : name(std::move(n)) {}
|
|
||||||
|
|
||||||
BinaryExpr::BinaryExpr(BinaryOp op, std::shared_ptr<Expr> lhs,
|
|
||||||
std::shared_ptr<Expr> rhs)
|
|
||||||
: op(op), lhs(std::move(lhs)), rhs(std::move(rhs)) {}
|
|
||||||
|
|
||||||
ReturnStmt::ReturnStmt(std::shared_ptr<Expr> v) : value(std::move(v)) {}
|
|
||||||
|
|
||||||
VarDecl::VarDecl(std::string n, std::shared_ptr<Expr> i)
|
|
||||||
: name(std::move(n)), init(std::move(i)) {}
|
|
||||||
|
|
||||||
FuncDef::FuncDef(std::string n, std::shared_ptr<Block> b)
|
|
||||||
: name(std::move(n)), body(std::move(b)) {}
|
|
||||||
|
|
||||||
CompUnit::CompUnit(std::shared_ptr<FuncDef> f) : func(std::move(f)) {}
|
|
||||||
|
|
||||||
} // namespace ast
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <iosfwd>
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace ast {
|
|
||||||
|
|
||||||
enum class BinaryOp { Add, Sub, Mul, Div };
|
|
||||||
|
|
||||||
struct Expr {
|
|
||||||
virtual ~Expr() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct NumberExpr : Expr {
|
|
||||||
int value{};
|
|
||||||
explicit NumberExpr(int v);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VarExpr : Expr {
|
|
||||||
std::string name;
|
|
||||||
explicit VarExpr(std::string n);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct BinaryExpr : Expr {
|
|
||||||
BinaryOp op;
|
|
||||||
std::shared_ptr<Expr> lhs;
|
|
||||||
std::shared_ptr<Expr> rhs;
|
|
||||||
BinaryExpr(BinaryOp op, std::shared_ptr<Expr> lhs, std::shared_ptr<Expr> rhs);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Stmt {
|
|
||||||
virtual ~Stmt() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ReturnStmt : Stmt {
|
|
||||||
std::shared_ptr<Expr> value;
|
|
||||||
explicit ReturnStmt(std::shared_ptr<Expr> v);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VarDecl {
|
|
||||||
std::string name;
|
|
||||||
std::shared_ptr<Expr> init; // nullptr if no initializer
|
|
||||||
VarDecl(std::string n, std::shared_ptr<Expr> i);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Block {
|
|
||||||
std::vector<std::shared_ptr<VarDecl>> varDecls;
|
|
||||||
std::vector<std::shared_ptr<Stmt>> stmts;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct FuncDef {
|
|
||||||
std::string name;
|
|
||||||
std::shared_ptr<Block> body;
|
|
||||||
FuncDef(std::string n, std::shared_ptr<Block> b);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CompUnit {
|
|
||||||
std::shared_ptr<FuncDef> func;
|
|
||||||
explicit CompUnit(std::shared_ptr<FuncDef> f);
|
|
||||||
};
|
|
||||||
|
|
||||||
// 调试打印
|
|
||||||
void PrintAST(const CompUnit& cu);
|
|
||||||
// 导出 AST 为 Graphviz DOT。
|
|
||||||
void PrintASTDot(const CompUnit& cu, std::ostream& os);
|
|
||||||
|
|
||||||
} // namespace ast
|
|
||||||
@@ -1,198 +0,0 @@
|
|||||||
// 简单 AST 调试打印,便于前端验证。
|
|
||||||
|
|
||||||
#include "ast/AstNodes.h"
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <ostream>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace ast {
|
|
||||||
|
|
||||||
static void PrintIndent(int depth) {
|
|
||||||
for (int i = 0; i < depth; ++i) std::cout << " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PrintLine(int depth, const std::string& text) {
|
|
||||||
PrintIndent(depth);
|
|
||||||
std::cout << text << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
static void DumpExpr(const Expr* expr, int depth) {
|
|
||||||
if (!expr) {
|
|
||||||
PrintLine(depth, "NullExpr");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auto num = dynamic_cast<const NumberExpr*>(expr)) {
|
|
||||||
PrintLine(depth, "NumberExpr value=" + std::to_string(num->value));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (auto var = dynamic_cast<const VarExpr*>(expr)) {
|
|
||||||
PrintLine(depth, "VarExpr name=" + var->name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (auto bin = dynamic_cast<const BinaryExpr*>(expr)) {
|
|
||||||
const char* op = "?";
|
|
||||||
switch (bin->op) {
|
|
||||||
case BinaryOp::Add:
|
|
||||||
op = "Add";
|
|
||||||
break;
|
|
||||||
case BinaryOp::Sub:
|
|
||||||
op = "Sub";
|
|
||||||
break;
|
|
||||||
case BinaryOp::Mul:
|
|
||||||
op = "Mul";
|
|
||||||
break;
|
|
||||||
case BinaryOp::Div:
|
|
||||||
op = "Div";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
PrintLine(depth, std::string("BinaryExpr op=") + op);
|
|
||||||
DumpExpr(bin->lhs.get(), depth + 1);
|
|
||||||
DumpExpr(bin->rhs.get(), depth + 1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PrintLine(depth, "UnknownExpr");
|
|
||||||
}
|
|
||||||
|
|
||||||
void PrintAST(const CompUnit& cu) {
|
|
||||||
PrintLine(0, "CompUnit");
|
|
||||||
if (!cu.func) {
|
|
||||||
PrintLine(1, "<empty>");
|
|
||||||
std::cout << "\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PrintLine(1, "FuncDef name=" + cu.func->name);
|
|
||||||
const auto& body = cu.func->body;
|
|
||||||
if (!body) {
|
|
||||||
PrintLine(2, "Block <null>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PrintLine(2, "Block");
|
|
||||||
for (const auto& decl : body->varDecls) {
|
|
||||||
PrintLine(3, "VarDecl name=" + decl->name);
|
|
||||||
if (decl->init) {
|
|
||||||
PrintLine(4, "Init");
|
|
||||||
DumpExpr(decl->init.get(), 5);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (const auto& stmt : body->stmts) {
|
|
||||||
if (auto ret = dynamic_cast<ReturnStmt*>(stmt.get())) {
|
|
||||||
PrintLine(3, "ReturnStmt");
|
|
||||||
DumpExpr(ret->value.get(), 4);
|
|
||||||
} else {
|
|
||||||
PrintLine(3, "UnknownStmt");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::cout << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
std::string EscapeDotLabel(const std::string& s) {
|
|
||||||
std::string out;
|
|
||||||
out.reserve(s.size());
|
|
||||||
for (char c : s) {
|
|
||||||
if (c == '\\' || c == '"') out.push_back('\\');
|
|
||||||
out.push_back(c);
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
class DotWriter {
|
|
||||||
public:
|
|
||||||
explicit DotWriter(std::ostream& os) : os_(os) {}
|
|
||||||
|
|
||||||
int AddNode(const std::string& label) {
|
|
||||||
int id = next_id_++;
|
|
||||||
os_ << " n" << id << " [label=\"" << EscapeDotLabel(label) << "\"];\n";
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddEdge(int from, int to) { os_ << " n" << from << " -> n" << to << ";\n"; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::ostream& os_;
|
|
||||||
int next_id_ = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
int EmitExpr(const Expr* expr, DotWriter& dot) {
|
|
||||||
if (!expr) return dot.AddNode("NullExpr");
|
|
||||||
|
|
||||||
if (auto num = dynamic_cast<const NumberExpr*>(expr)) {
|
|
||||||
return dot.AddNode("Number(" + std::to_string(num->value) + ")");
|
|
||||||
}
|
|
||||||
if (auto var = dynamic_cast<const VarExpr*>(expr)) {
|
|
||||||
return dot.AddNode("Var(" + var->name + ")");
|
|
||||||
}
|
|
||||||
if (auto bin = dynamic_cast<const BinaryExpr*>(expr)) {
|
|
||||||
const char* op = "?";
|
|
||||||
switch (bin->op) {
|
|
||||||
case BinaryOp::Add:
|
|
||||||
op = "Add";
|
|
||||||
break;
|
|
||||||
case BinaryOp::Sub:
|
|
||||||
op = "Sub";
|
|
||||||
break;
|
|
||||||
case BinaryOp::Mul:
|
|
||||||
op = "Mul";
|
|
||||||
break;
|
|
||||||
case BinaryOp::Div:
|
|
||||||
op = "Div";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
int id = dot.AddNode(std::string("Binary(") + op + ")");
|
|
||||||
int lhs = EmitExpr(bin->lhs.get(), dot);
|
|
||||||
int rhs = EmitExpr(bin->rhs.get(), dot);
|
|
||||||
dot.AddEdge(id, lhs);
|
|
||||||
dot.AddEdge(id, rhs);
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
return dot.AddNode("UnknownExpr");
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void PrintASTDot(const CompUnit& cu, std::ostream& os) {
|
|
||||||
os << "digraph AST {\n";
|
|
||||||
os << " rankdir=TB;\n";
|
|
||||||
os << " node [shape=box, fontname=\"Courier\"];\n";
|
|
||||||
|
|
||||||
DotWriter dot(os);
|
|
||||||
int cu_id = dot.AddNode("CompUnit");
|
|
||||||
if (cu.func) {
|
|
||||||
int func_id = dot.AddNode("FuncDef(" + cu.func->name + ")");
|
|
||||||
dot.AddEdge(cu_id, func_id);
|
|
||||||
if (cu.func->body) {
|
|
||||||
int block_id = dot.AddNode("Block");
|
|
||||||
dot.AddEdge(func_id, block_id);
|
|
||||||
|
|
||||||
for (const auto& decl : cu.func->body->varDecls) {
|
|
||||||
int decl_id = dot.AddNode("VarDecl(" + decl->name + ")");
|
|
||||||
dot.AddEdge(block_id, decl_id);
|
|
||||||
if (decl->init) {
|
|
||||||
int init_id = EmitExpr(decl->init.get(), dot);
|
|
||||||
dot.AddEdge(decl_id, init_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& stmt : cu.func->body->stmts) {
|
|
||||||
if (auto ret = dynamic_cast<ReturnStmt*>(stmt.get())) {
|
|
||||||
int ret_id = dot.AddNode("Return");
|
|
||||||
dot.AddEdge(block_id, ret_id);
|
|
||||||
int value_id = EmitExpr(ret->value.get(), dot);
|
|
||||||
dot.AddEdge(ret_id, value_id);
|
|
||||||
} else {
|
|
||||||
int stmt_id = dot.AddNode("Stmt");
|
|
||||||
dot.AddEdge(block_id, stmt_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
os << "}\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ast
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
add_library(ast STATIC
|
|
||||||
AstNodes.cpp
|
|
||||||
AstPrinter.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
target_link_libraries(ast PUBLIC
|
|
||||||
build_options
|
|
||||||
)
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
// 将 parse tree 转换为 AST。
|
|
||||||
#include "frontend/AstBuilder.h"
|
|
||||||
|
|
||||||
#include <any>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "SysYBaseVisitor.h"
|
|
||||||
#include "SysYParser.h"
|
|
||||||
#include "ast/AstNodes.h"
|
|
||||||
#include "antlr4-runtime.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
using ast::BinaryExpr;
|
|
||||||
using ast::BinaryOp;
|
|
||||||
using ast::Block;
|
|
||||||
using ast::CompUnit;
|
|
||||||
using ast::FuncDef;
|
|
||||||
using ast::NumberExpr;
|
|
||||||
using ast::ReturnStmt;
|
|
||||||
using ast::VarDecl;
|
|
||||||
using ast::VarExpr;
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
T Take(std::any&& value) {
|
|
||||||
if (auto* ptr = std::any_cast<T>(&value)) {
|
|
||||||
return std::move(*ptr);
|
|
||||||
}
|
|
||||||
throw std::runtime_error("AST 构建失败:类型不匹配");
|
|
||||||
}
|
|
||||||
|
|
||||||
class Builder : public SysYBaseVisitor {
|
|
||||||
public:
|
|
||||||
std::any visitCompUnit(SysYParser::CompUnitContext* ctx) override {
|
|
||||||
auto func = Take<std::shared_ptr<FuncDef>>(visit(ctx->funcDef()));
|
|
||||||
return std::make_shared<CompUnit>(std::move(func));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::any visitFuncDef(SysYParser::FuncDefContext* ctx) override {
|
|
||||||
auto body = Take<std::shared_ptr<Block>>(visit(ctx->block()));
|
|
||||||
return std::make_shared<FuncDef>("main", std::move(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::any visitBlock(SysYParser::BlockContext* ctx) override {
|
|
||||||
auto block = std::make_shared<Block>();
|
|
||||||
for (auto stmtCtx : ctx->stmt()) {
|
|
||||||
if (stmtCtx->varDecl()) {
|
|
||||||
block->varDecls.emplace_back(
|
|
||||||
Take<std::shared_ptr<VarDecl>>(visit(stmtCtx->varDecl())));
|
|
||||||
} else if (stmtCtx->returnStmt()) {
|
|
||||||
block->stmts.emplace_back(
|
|
||||||
Take<std::shared_ptr<ReturnStmt>>(visit(stmtCtx->returnStmt())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::any visitVarDecl(SysYParser::VarDeclContext* ctx) override {
|
|
||||||
std::shared_ptr<ast::Expr> init;
|
|
||||||
if (ctx->exp()) {
|
|
||||||
init = Take<std::shared_ptr<ast::Expr>>(visit(ctx->exp()));
|
|
||||||
}
|
|
||||||
return std::make_shared<VarDecl>(ctx->Ident()->getText(), std::move(init));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::any visitReturnStmt(SysYParser::ReturnStmtContext* ctx) override {
|
|
||||||
auto expr = Take<std::shared_ptr<ast::Expr>>(visit(ctx->exp()));
|
|
||||||
return std::make_shared<ReturnStmt>(std::move(expr));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::any visitExp(SysYParser::ExpContext* ctx) override {
|
|
||||||
return visit(ctx->addExp());
|
|
||||||
}
|
|
||||||
|
|
||||||
std::any visitAddExp(SysYParser::AddExpContext* ctx) override {
|
|
||||||
auto node = Take<std::shared_ptr<ast::Expr>>(visit(ctx->primary(0)));
|
|
||||||
for (size_t i = 1; i < ctx->primary().size(); ++i) {
|
|
||||||
auto rhs = Take<std::shared_ptr<ast::Expr>>(visit(ctx->primary(i)));
|
|
||||||
auto opToken = ctx->AddOp(i - 1);
|
|
||||||
BinaryOp op = BinaryOp::Add;
|
|
||||||
if (opToken->getText() == "-") op = BinaryOp::Sub;
|
|
||||||
node = std::make_shared<BinaryExpr>(op, std::move(node), std::move(rhs));
|
|
||||||
}
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::any visitPrimary(SysYParser::PrimaryContext* ctx) override {
|
|
||||||
if (ctx->Number()) {
|
|
||||||
std::shared_ptr<ast::Expr> expr =
|
|
||||||
std::make_shared<NumberExpr>(std::stoi(ctx->Number()->getText()));
|
|
||||||
return expr;
|
|
||||||
}
|
|
||||||
if (ctx->Ident()) {
|
|
||||||
std::shared_ptr<ast::Expr> expr =
|
|
||||||
std::make_shared<VarExpr>(ctx->Ident()->getText());
|
|
||||||
return expr;
|
|
||||||
}
|
|
||||||
return visit(ctx->exp());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
std::shared_ptr<ast::CompUnit> BuildAst(antlr4::tree::ParseTree* tree) {
|
|
||||||
if (!tree) {
|
|
||||||
throw std::runtime_error("parse tree 为空");
|
|
||||||
}
|
|
||||||
Builder visitor;
|
|
||||||
auto result = visitor.visit(tree);
|
|
||||||
return Take<std::shared_ptr<ast::CompUnit>>(std::move(result));
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
// 将 ANTLR parse tree 转换为内部 AST。
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
namespace antlr4 {
|
|
||||||
namespace tree {
|
|
||||||
class ParseTree;
|
|
||||||
}
|
|
||||||
} // namespace antlr4
|
|
||||||
|
|
||||||
namespace ast {
|
|
||||||
struct CompUnit;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<ast::CompUnit> BuildAst(antlr4::tree::ParseTree* tree);
|
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
add_library(frontend STATIC
|
add_library(frontend STATIC
|
||||||
AntlrDriver.cpp
|
AntlrDriver.cpp
|
||||||
AstBuilder.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(frontend PUBLIC
|
target_link_libraries(frontend PUBLIC
|
||||||
build_options
|
build_options
|
||||||
ast
|
|
||||||
"${ANTLR4_RUNTIME_TARGET}"
|
"${ANTLR4_RUNTIME_TARGET}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ add_library(irgen STATIC
|
|||||||
|
|
||||||
target_link_libraries(irgen PUBLIC
|
target_link_libraries(irgen PUBLIC
|
||||||
build_options
|
build_options
|
||||||
ast
|
"${ANTLR4_RUNTIME_TARGET}"
|
||||||
sem
|
|
||||||
ir
|
ir
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,45 +1,44 @@
|
|||||||
// 将 AST 翻译为极简 IR。
|
// 将 ANTLR parse tree 翻译为极简 IR。
|
||||||
// 这里提供一个可运行的最小 IR 生成骨架,并把实现拆分到 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
|
// 实现拆分在 IRGenFunc/IRGenStmt/IRGenExp/IRGenDecl。
|
||||||
// 同学可以在对应 .cpp 内进一步完善更多语义。
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
|
#include "SysYParser.h"
|
||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
|
|
||||||
namespace ast {
|
namespace antlr4 {
|
||||||
struct CompUnit;
|
namespace tree {
|
||||||
struct Block;
|
class ParseTree;
|
||||||
struct VarDecl;
|
|
||||||
struct Stmt;
|
|
||||||
struct Expr;
|
|
||||||
}
|
}
|
||||||
|
} // namespace antlr4
|
||||||
|
|
||||||
namespace ir {
|
namespace ir {
|
||||||
class Module;
|
class Module;
|
||||||
class Function;
|
class Function;
|
||||||
class IRBuilder;
|
class IRBuilder;
|
||||||
class Value;
|
class Value;
|
||||||
class ConstantInt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 最小 IR 生成器的内部实现,接口分散在各 IRGen*.cpp。
|
|
||||||
class IRGenImpl {
|
class IRGenImpl {
|
||||||
public:
|
public:
|
||||||
explicit IRGenImpl(ir::Module& module);
|
explicit IRGenImpl(ir::Module& module);
|
||||||
|
|
||||||
void Gen(const ast::CompUnit& ast);
|
void Gen(SysYParser::CompUnitContext& cu);
|
||||||
std::unique_ptr<ir::Module> TakeModule();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void GenBlock(const ast::Block& block);
|
void GenFuncDef(SysYParser::FuncDefContext& func);
|
||||||
void GenVarDecl(const ast::VarDecl& decl);
|
void GenBlock(SysYParser::BlockContext& block);
|
||||||
void GenStmt(const ast::Stmt& stmt);
|
void GenStmt(SysYParser::StmtContext& stmt);
|
||||||
ir::Value* GenExpr(const ast::Expr& expr);
|
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::Module& module_;
|
||||||
ir::Function* func_;
|
ir::Function* func_;
|
||||||
@@ -47,4 +46,4 @@ class IRGenImpl {
|
|||||||
std::unordered_map<std::string, ir::Value*> locals_;
|
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);
|
||||||
|
|||||||
@@ -1,39 +1,36 @@
|
|||||||
// 声明翻译模块:
|
|
||||||
// - 处理全局变量与局部变量声明
|
|
||||||
// - 处理数组初始化、空间分配与初值生成等
|
|
||||||
|
|
||||||
#include "irgen/IRGen.h"
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "ast/AstNodes.h"
|
#include "SysYParser.h"
|
||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
|
|
||||||
void IRGenImpl::GenBlock(const ast::Block& block) {
|
void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
|
||||||
// 先为所有局部变量创建栈槽,使 alloca 聚集在入口块前部。
|
for (auto* stmt : block.stmt()) {
|
||||||
for (const auto& decl : block.varDecls) {
|
if (stmt && stmt->varDecl()) {
|
||||||
|
const std::string name = stmt->varDecl()->Ident()->getText();
|
||||||
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
|
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
|
||||||
locals_[decl->name] = slot;
|
locals_[name] = slot;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& decl : block.varDecls) {
|
for (auto* stmt : block.stmt()) {
|
||||||
GenVarDecl(*decl);
|
if (stmt) {
|
||||||
}
|
|
||||||
for (const auto& stmt : block.stmts) {
|
|
||||||
GenStmt(*stmt);
|
GenStmt(*stmt);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IRGenImpl::GenVarDecl(const ast::VarDecl& decl) {
|
void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
|
||||||
auto it = locals_.find(decl.name);
|
const std::string name = decl.Ident()->getText();
|
||||||
|
auto it = locals_.find(name);
|
||||||
if (it == locals_.end()) {
|
if (it == locals_.end()) {
|
||||||
throw std::runtime_error("[irgen] 变量栈槽未创建: " + decl.name);
|
throw std::runtime_error("[irgen] 变量栈槽未创建: " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
ir::Value* init = nullptr;
|
ir::Value* init = nullptr;
|
||||||
if (decl.init) {
|
if (decl.exp()) {
|
||||||
init = GenExpr(*decl.init);
|
init = GenExpr(*decl.exp());
|
||||||
} else {
|
} else {
|
||||||
init = ir::DefaultContext().GetConstInt(0);
|
init = ir::DefaultContext().GetConstInt(0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,24 @@
|
|||||||
// 这是一个“可跑”的最小 IR 生成示例,便于对照/调试。
|
|
||||||
// IR 生成驱动(Driver):
|
|
||||||
// - 驱动 Visitor 遍历 AST,调度各子模块完成翻译
|
|
||||||
// - 统一管理模块级翻译入口与上下文(Module/IRBuilder 等)
|
|
||||||
// - 组织函数/语句/表达式/声明等翻译流程
|
|
||||||
|
|
||||||
#include "irgen/IRGen.h"
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "ast/AstNodes.h"
|
#include "SysYParser.h"
|
||||||
|
#include "antlr4-runtime.h"
|
||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
|
|
||||||
std::unique_ptr<ir::Module> GenerateIR(const ast::CompUnit& ast) {
|
std::unique_ptr<ir::Module> GenerateIR(antlr4::tree::ParseTree* tree) {
|
||||||
|
if (!tree) {
|
||||||
|
throw std::runtime_error("[irgen] parse tree 为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto* cu = dynamic_cast<SysYParser::CompUnitContext*>(tree);
|
||||||
|
if (!cu) {
|
||||||
|
throw std::runtime_error("[irgen] parse tree 根节点不是 compUnit");
|
||||||
|
}
|
||||||
|
|
||||||
auto module = std::make_unique<ir::Module>();
|
auto module = std::make_unique<ir::Module>();
|
||||||
IRGenImpl gen(*module);
|
IRGenImpl gen(*module);
|
||||||
gen.Gen(ast);
|
gen.Gen(*cu);
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +1,46 @@
|
|||||||
// 表达式翻译模块:
|
|
||||||
// - 处理算术运算、比较、逻辑运算、函数调用等表达式
|
|
||||||
// - 生成对应的 IR 指令并返回 SSA 值
|
|
||||||
|
|
||||||
#include "irgen/IRGen.h"
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "ast/AstNodes.h"
|
#include "SysYParser.h"
|
||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
|
|
||||||
ir::Value* IRGenImpl::GenExpr(const ast::Expr& expr) {
|
ir::Value* IRGenImpl::GenExpr(SysYParser::ExpContext& expr) {
|
||||||
if (auto num = dynamic_cast<const ast::NumberExpr*>(&expr)) {
|
if (!expr.addExp()) {
|
||||||
return ir::DefaultContext().GetConstInt(num->value);
|
throw std::runtime_error("[irgen] 非法表达式");
|
||||||
}
|
}
|
||||||
if (auto var = dynamic_cast<const ast::VarExpr*>(&expr)) {
|
return GenAddExpr(*expr.addExp());
|
||||||
auto it = locals_.find(var->name);
|
}
|
||||||
if (it == locals_.end()) {
|
|
||||||
throw std::runtime_error("[irgen] 变量未找到: " + var->name);
|
ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
|
||||||
}
|
const auto& terms = add.primary();
|
||||||
std::string name = ir::DefaultContext().NextTemp();
|
if (terms.empty()) {
|
||||||
return builder_.CreateLoad(it->second, name);
|
throw std::runtime_error("[irgen] 空加法表达式");
|
||||||
}
|
}
|
||||||
if (auto bin = dynamic_cast<const ast::BinaryExpr*>(&expr)) {
|
|
||||||
auto* lhs = GenExpr(*bin->lhs);
|
ir::Value* acc = GenPrimary(*terms[0]);
|
||||||
auto* rhs = GenExpr(*bin->rhs);
|
for (size_t i = 1; i < terms.size(); ++i) {
|
||||||
std::string name = ir::DefaultContext().NextTemp();
|
ir::Value* rhs = GenPrimary(*terms[i]);
|
||||||
if (bin->op == ast::BinaryOp::Add) {
|
std::string name = ir::DefaultContext().NextTemp();
|
||||||
return builder_.CreateBinary(ir::Opcode::Add, lhs, rhs, name);
|
acc = builder_.CreateBinary(ir::Opcode::Add, acc, rhs, name);
|
||||||
}
|
}
|
||||||
if (bin->op == ast::BinaryOp::Sub) {
|
return acc;
|
||||||
// 当前子集只需要加法,减法复用 add 但保留分支,便于扩展
|
}
|
||||||
return builder_.CreateBinary(ir::Opcode::Add, lhs, rhs, name);
|
|
||||||
}
|
ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
|
||||||
}
|
if (primary.Number()) {
|
||||||
throw std::runtime_error("[irgen] 暂不支持的表达式类型");
|
return ir::DefaultContext().GetConstInt(std::stoi(primary.Number()->getText()));
|
||||||
|
}
|
||||||
|
if (primary.Ident()) {
|
||||||
|
const std::string name = primary.Ident()->getText();
|
||||||
|
auto it = locals_.find(name);
|
||||||
|
if (it == locals_.end()) {
|
||||||
|
throw std::runtime_error("[irgen] 变量未找到: " + name);
|
||||||
|
}
|
||||||
|
return builder_.CreateLoad(it->second, ir::DefaultContext().NextTemp());
|
||||||
|
}
|
||||||
|
if (primary.exp()) {
|
||||||
|
return GenExpr(*primary.exp());
|
||||||
|
}
|
||||||
|
throw std::runtime_error("[irgen] 暂不支持的 primary 形式");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,28 @@
|
|||||||
// 函数翻译模块:
|
|
||||||
// - 处理函数定义、参数列表与返回值翻译
|
|
||||||
// - 创建并填充对应的 IR Function 对象
|
|
||||||
|
|
||||||
#include "irgen/IRGen.h"
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "ast/AstNodes.h"
|
#include "SysYParser.h"
|
||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
|
|
||||||
IRGenImpl::IRGenImpl(ir::Module& module)
|
IRGenImpl::IRGenImpl(ir::Module& module)
|
||||||
: module_(module),
|
: module_(module), func_(nullptr), builder_(nullptr) {}
|
||||||
func_(module_.CreateFunction("main", ir::Type::Int32())),
|
|
||||||
builder_(func_->entry()) {}
|
|
||||||
|
|
||||||
void IRGenImpl::Gen(const ast::CompUnit& ast) {
|
void IRGenImpl::Gen(SysYParser::CompUnitContext& cu) {
|
||||||
if (!ast.func || !ast.func->body) {
|
if (!cu.funcDef()) {
|
||||||
throw std::runtime_error("[irgen] AST 不完整:缺少 main 定义");
|
throw std::runtime_error("[irgen] 缺少 main 定义");
|
||||||
}
|
}
|
||||||
GenBlock(*ast.func->body);
|
GenFuncDef(*cu.funcDef());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<ir::Module> IRGenImpl::TakeModule() {
|
void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
|
||||||
return std::make_unique<ir::Module>(std::move(module_));
|
if (!func.block()) {
|
||||||
|
throw std::runtime_error("[irgen] 函数体为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
func_ = module_.CreateFunction("main", ir::Type::Int32());
|
||||||
|
builder_.SetInsertPoint(func_->entry());
|
||||||
|
locals_.clear();
|
||||||
|
|
||||||
|
GenBlock(*func.block());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,26 @@
|
|||||||
// 语句翻译模块:
|
|
||||||
// - 处理 if/while/return 等控制流构造
|
|
||||||
// - 负责基本块创建、分支跳转与控制流收束
|
|
||||||
|
|
||||||
#include "irgen/IRGen.h"
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "ast/AstNodes.h"
|
#include "SysYParser.h"
|
||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
|
|
||||||
void IRGenImpl::GenStmt(const ast::Stmt& stmt) {
|
void IRGenImpl::GenStmt(SysYParser::StmtContext& stmt) {
|
||||||
if (auto ret = dynamic_cast<const ast::ReturnStmt*>(&stmt)) {
|
if (stmt.varDecl()) {
|
||||||
ir::Value* v = GenExpr(*ret->value);
|
GenVarDecl(*stmt.varDecl());
|
||||||
builder_.CreateRet(v);
|
return;
|
||||||
|
}
|
||||||
|
if (stmt.returnStmt()) {
|
||||||
|
GenReturnStmt(*stmt.returnStmt());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw std::runtime_error("[irgen] 暂不支持的语句类型");
|
throw std::runtime_error("[irgen] 暂不支持的语句类型");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IRGenImpl::GenReturnStmt(SysYParser::ReturnStmtContext& ret) {
|
||||||
|
if (!ret.exp()) {
|
||||||
|
throw std::runtime_error("[irgen] return 缺少表达式");
|
||||||
|
}
|
||||||
|
ir::Value* v = GenExpr(*ret.exp());
|
||||||
|
builder_.CreateRet(v);
|
||||||
|
}
|
||||||
|
|||||||
23
src/main.cpp
23
src/main.cpp
@@ -1,17 +1,12 @@
|
|||||||
#include <exception>
|
#include <exception>
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#include "frontend/AntlrDriver.h"
|
#include "frontend/AntlrDriver.h"
|
||||||
#include "frontend/AstBuilder.h"
|
|
||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
#include "irgen/IRGen.h"
|
#include "irgen/IRGen.h"
|
||||||
#include "mir/MIR.h"
|
#include "mir/MIR.h"
|
||||||
#include "sem/Sema.h"
|
|
||||||
#include "utils/CLI.h"
|
#include "utils/CLI.h"
|
||||||
#include "utils/Log.h"
|
#include "utils/Log.h"
|
||||||
#include "ast/AstNodes.h"
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
try {
|
try {
|
||||||
@@ -20,25 +15,15 @@ int main(int argc, char** argv) {
|
|||||||
PrintHelp(std::cout);
|
PrintHelp(std::cout);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto antlr = ParseFileWithAntlr(opts.input);
|
auto antlr = ParseFileWithAntlr(opts.input);
|
||||||
auto ast = BuildAst(antlr.tree);
|
|
||||||
bool need_blank_line = false;
|
bool need_blank_line = false;
|
||||||
if (opts.emit_ast) {
|
if (opts.emit_parse_tree) {
|
||||||
ast::PrintAST(*ast); // 调试 AST
|
std::cout << antlr.tree->toStringTree(antlr.parser.get()) << "\n";
|
||||||
need_blank_line = true;
|
need_blank_line = true;
|
||||||
}
|
}
|
||||||
if (!opts.ast_dot_output.empty()) {
|
|
||||||
std::ofstream dot_out(opts.ast_dot_output);
|
|
||||||
if (!dot_out) {
|
|
||||||
throw std::runtime_error("无法写入 AST DOT 文件: " + opts.ast_dot_output);
|
|
||||||
}
|
|
||||||
ast::PrintASTDot(*ast, dot_out);
|
|
||||||
}
|
|
||||||
ast = RunSema(std::move(ast));
|
|
||||||
// IR 生成:当前使用 IRGenDriver.cpp 里的最小实现,
|
|
||||||
// 同学们可以在 irgen/ 目录下按提示自行完善或替换实现。
|
|
||||||
auto module = GenerateIR(*ast);
|
|
||||||
|
|
||||||
|
auto module = GenerateIR(antlr.tree);
|
||||||
if (opts.emit_ir) {
|
if (opts.emit_ir) {
|
||||||
ir::IRPrinter printer;
|
ir::IRPrinter printer;
|
||||||
if (need_blank_line) {
|
if (need_blank_line) {
|
||||||
|
|||||||
@@ -19,14 +19,14 @@ class SemaVisitor {
|
|||||||
explicit SemaVisitor(SymbolTable& table) : table_(table) {}
|
explicit SemaVisitor(SymbolTable& table) : table_(table) {}
|
||||||
|
|
||||||
void CheckBlock(const ast::Block& block) {
|
void CheckBlock(const ast::Block& block) {
|
||||||
for (const auto& decl : block.varDecls) {
|
for (const auto& item : block.items) {
|
||||||
|
if (auto decl = dynamic_cast<ast::VarDecl*>(item.get())) {
|
||||||
table_.Add(decl->name);
|
table_.Add(decl->name);
|
||||||
if (decl->init) CheckExpr(*decl->init);
|
if (decl->init) CheckExpr(*decl->init);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
for (const auto& stmt : block.stmts) {
|
if (auto ret = dynamic_cast<ast::ReturnStmt*>(item.get())) {
|
||||||
if (auto ret = dynamic_cast<ast::ReturnStmt*>(stmt.get())) {
|
|
||||||
CheckExpr(*ret->value);
|
CheckExpr(*ret->value);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ CLIOptions ParseCLI(int argc, char** argv) {
|
|||||||
|
|
||||||
if (argc <= 1) {
|
if (argc <= 1) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"用法: compiler [--help] [--emit-ast] [--emit-ir] [--emit-asm] [--ast-dot <file.dot>] <input.sy>");
|
"用法: compiler [--help] [--emit-parse-tree] [--emit-ir] [--emit-asm] <input.sy>");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 1; i < argc; ++i) {
|
for (int i = 1; i < argc; ++i) {
|
||||||
@@ -25,27 +25,20 @@ CLIOptions ParseCLI(int argc, char** argv) {
|
|||||||
return opt;
|
return opt;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std::strcmp(arg, "--ast-dot") == 0) {
|
if (std::strcmp(arg, "--emit-parse-tree") == 0) {
|
||||||
if (i + 1 >= argc) {
|
|
||||||
throw std::runtime_error("参数错误: --ast-dot 需要一个输出路径");
|
|
||||||
}
|
|
||||||
opt.ast_dot_output = argv[++i];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (std::strcmp(arg, "--emit-ast") == 0) {
|
|
||||||
if (!explicit_emit) {
|
if (!explicit_emit) {
|
||||||
opt.emit_ast = false;
|
opt.emit_parse_tree = false;
|
||||||
opt.emit_ir = false;
|
opt.emit_ir = false;
|
||||||
|
opt.emit_asm = false;
|
||||||
explicit_emit = true;
|
explicit_emit = true;
|
||||||
}
|
}
|
||||||
opt.emit_ast = true;
|
opt.emit_parse_tree = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std::strcmp(arg, "--emit-ir") == 0) {
|
if (std::strcmp(arg, "--emit-ir") == 0) {
|
||||||
if (!explicit_emit) {
|
if (!explicit_emit) {
|
||||||
opt.emit_ast = false;
|
opt.emit_parse_tree = false;
|
||||||
opt.emit_ir = false;
|
opt.emit_ir = false;
|
||||||
opt.emit_asm = false;
|
opt.emit_asm = false;
|
||||||
explicit_emit = true;
|
explicit_emit = true;
|
||||||
@@ -56,7 +49,7 @@ CLIOptions ParseCLI(int argc, char** argv) {
|
|||||||
|
|
||||||
if (std::strcmp(arg, "--emit-asm") == 0) {
|
if (std::strcmp(arg, "--emit-asm") == 0) {
|
||||||
if (!explicit_emit) {
|
if (!explicit_emit) {
|
||||||
opt.emit_ast = false;
|
opt.emit_parse_tree = false;
|
||||||
opt.emit_ir = false;
|
opt.emit_ir = false;
|
||||||
opt.emit_asm = false;
|
opt.emit_asm = false;
|
||||||
explicit_emit = true;
|
explicit_emit = true;
|
||||||
@@ -80,10 +73,9 @@ CLIOptions ParseCLI(int argc, char** argv) {
|
|||||||
if (opt.input.empty() && !opt.show_help) {
|
if (opt.input.empty() && !opt.show_help) {
|
||||||
throw std::runtime_error("缺少输入文件:请提供 <input.sy>(使用 --help 查看用法)");
|
throw std::runtime_error("缺少输入文件:请提供 <input.sy>(使用 --help 查看用法)");
|
||||||
}
|
}
|
||||||
if (!opt.emit_ast && !opt.emit_ir && !opt.emit_asm &&
|
if (!opt.emit_parse_tree && !opt.emit_ir && !opt.emit_asm) {
|
||||||
opt.ast_dot_output.empty()) {
|
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"未选择任何输出:请使用 --emit-ast / --emit-ir / --emit-asm(或使用 --ast-dot 导出图)");
|
"未选择任何输出:请使用 --emit-parse-tree / --emit-ir / --emit-asm");
|
||||||
}
|
}
|
||||||
return opt;
|
return opt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
// 简易命令行解析:支持帮助、输入文件和 AST DOT 导出。
|
// 简易命令行解析:支持帮助、输入文件与输出阶段选择。
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct CLIOptions {
|
struct CLIOptions {
|
||||||
std::string input;
|
std::string input;
|
||||||
std::string ast_dot_output;
|
bool emit_parse_tree = false;
|
||||||
bool emit_ast = true;
|
|
||||||
bool emit_ir = true;
|
bool emit_ir = true;
|
||||||
bool emit_asm = false;
|
bool emit_asm = false;
|
||||||
bool show_help = false;
|
bool show_help = false;
|
||||||
|
|||||||
@@ -10,18 +10,17 @@ void PrintHelp(std::ostream& os) {
|
|||||||
os << "SysY Compiler (课程实验最小可运行示例)\n"
|
os << "SysY Compiler (课程实验最小可运行示例)\n"
|
||||||
<< "\n"
|
<< "\n"
|
||||||
<< "用法:\n"
|
<< "用法:\n"
|
||||||
<< " compiler [--help] [--emit-ast] [--emit-ir] [--emit-asm] [--ast-dot <file.dot>] <input.sy>\n"
|
<< " compiler [--help] [--emit-parse-tree] [--emit-ir] [--emit-asm] <input.sy>\n"
|
||||||
<< "\n"
|
<< "\n"
|
||||||
<< "选项:\n"
|
<< "选项:\n"
|
||||||
<< " -h, --help 打印帮助信息并退出\n"
|
<< " -h, --help 打印帮助信息并退出\n"
|
||||||
<< " --emit-ast 仅在显式模式下启用 AST 文本输出\n"
|
<< " --emit-parse-tree 仅在显式模式下启用 ANTLR 语法树输出\n"
|
||||||
<< " --emit-ir 仅在显式模式下启用 IR 输出\n"
|
<< " --emit-ir 仅在显式模式下启用 IR 输出\n"
|
||||||
<< " --emit-asm 仅在显式模式下启用 AArch64 汇编输出\n"
|
<< " --emit-asm 仅在显式模式下启用 AArch64 汇编输出\n"
|
||||||
<< " --ast-dot <path> 导出 AST Graphviz DOT 到指定文件\n"
|
|
||||||
<< "\n"
|
<< "\n"
|
||||||
<< "说明:\n"
|
<< "说明:\n"
|
||||||
<< " - 默认同时输出 AST 与 IR\n"
|
<< " - 默认输出 IR\n"
|
||||||
<< " - 若使用 --emit-ast/--emit-ir/--emit-asm,则仅输出显式选择的阶段\n"
|
<< " - 若使用 --emit-parse-tree/--emit-ir/--emit-asm,则仅输出显式选择的阶段\n"
|
||||||
<< " - 可使用重定向写入文件:\n"
|
<< " - 可使用重定向写入文件:\n"
|
||||||
<< " compiler --emit-asm test/test_case/simple_add.sy > out.s\n";
|
<< " compiler --emit-asm test/test_case/simple_add.sy > out.s\n";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user