parent
8903bf73f9
commit
c153604c2e
@ -1,4 +1,5 @@
|
||||
|
||||
// AST 节点定义与实现:
|
||||
// - 表达式、语句、声明、函数、类型等节点
|
||||
// - 支持后续阶段在节点上附加信息(类型、符号绑定、常量值等)
|
||||
|
||||
#include "ast/AstNodes.h"
|
||||
|
||||
@ -0,0 +1,70 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#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) : value(v) {}
|
||||
};
|
||||
|
||||
struct VarExpr : Expr {
|
||||
std::string name;
|
||||
explicit VarExpr(std::string n) : name(std::move(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)
|
||||
: op(op), lhs(std::move(lhs)), rhs(std::move(rhs)) {}
|
||||
};
|
||||
|
||||
struct Stmt {
|
||||
virtual ~Stmt() = default;
|
||||
};
|
||||
|
||||
struct ReturnStmt : Stmt {
|
||||
std::shared_ptr<Expr> value;
|
||||
explicit ReturnStmt(std::shared_ptr<Expr> v) : value(std::move(v)) {}
|
||||
};
|
||||
|
||||
struct VarDecl {
|
||||
std::string name;
|
||||
std::shared_ptr<Expr> init; // nullptr if no initializer
|
||||
VarDecl(std::string n, std::shared_ptr<Expr> i)
|
||||
: name(std::move(n)), init(std::move(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)
|
||||
: name(std::move(n)), body(std::move(b)) {}
|
||||
};
|
||||
|
||||
struct CompUnit {
|
||||
std::shared_ptr<FuncDef> func;
|
||||
explicit CompUnit(std::shared_ptr<FuncDef> f) : func(std::move(f)) {}
|
||||
};
|
||||
|
||||
// 调试打印
|
||||
void PrintAST(const CompUnit& cu);
|
||||
|
||||
} // namespace ast
|
||||
@ -1,4 +1,72 @@
|
||||
// AST 调试打印:
|
||||
// - 以可读形式打印 AST 结构
|
||||
// - 用于验证 AST 构建与语义分析结果,便于定位问题
|
||||
// 简单 AST 调试打印,便于前端验证。
|
||||
|
||||
#include "ast/AstNodes.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace ast {
|
||||
|
||||
static void PrintExpr(const Expr* expr);
|
||||
|
||||
static void PrintIndent(int depth) {
|
||||
for (int i = 0; i < depth; ++i) std::cout << " ";
|
||||
}
|
||||
|
||||
static void PrintExpr(const Expr* expr) {
|
||||
if (auto num = dynamic_cast<const NumberExpr*>(expr)) {
|
||||
std::cout << num->value;
|
||||
} else if (auto var = dynamic_cast<const VarExpr*>(expr)) {
|
||||
std::cout << var->name;
|
||||
} else if (auto bin = dynamic_cast<const BinaryExpr*>(expr)) {
|
||||
std::cout << "(";
|
||||
PrintExpr(bin->lhs.get());
|
||||
const char* op = "?";
|
||||
switch (bin->op) {
|
||||
case BinaryOp::Add:
|
||||
op = "+";
|
||||
break;
|
||||
case BinaryOp::Sub:
|
||||
op = "-";
|
||||
break;
|
||||
case BinaryOp::Mul:
|
||||
op = "*";
|
||||
break;
|
||||
case BinaryOp::Div:
|
||||
op = "/";
|
||||
break;
|
||||
}
|
||||
std::cout << " " << op << " ";
|
||||
PrintExpr(bin->rhs.get());
|
||||
std::cout << ")";
|
||||
}
|
||||
}
|
||||
|
||||
void PrintAST(const CompUnit& cu) {
|
||||
if (!cu.func) return;
|
||||
std::cout << "func " << cu.func->name << " () {\n";
|
||||
const auto& body = cu.func->body;
|
||||
if (!body) {
|
||||
std::cout << "}\n";
|
||||
return;
|
||||
}
|
||||
for (const auto& decl : body->varDecls) {
|
||||
PrintIndent(1);
|
||||
std::cout << "var " << decl->name;
|
||||
if (decl->init) {
|
||||
std::cout << " = ";
|
||||
PrintExpr(decl->init.get());
|
||||
}
|
||||
std::cout << ";\n";
|
||||
}
|
||||
for (const auto& stmt : body->stmts) {
|
||||
if (auto ret = dynamic_cast<ReturnStmt*>(stmt.get())) {
|
||||
PrintIndent(1);
|
||||
std::cout << "return ";
|
||||
PrintExpr(ret->value.get());
|
||||
std::cout << ";\n";
|
||||
}
|
||||
}
|
||||
std::cout << "}\n";
|
||||
}
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
// 将 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);
|
||||
Loading…
Reference in new issue