355 lines
11 KiB
C++
355 lines
11 KiB
C++
#include <algorithm>
|
|
#include <iostream>
|
|
using namespace std;
|
|
#include "ASTPrinter.h"
|
|
#include "SysYParser.h"
|
|
|
|
|
|
any ASTPrinter::visitCompUnit(SysYParser::CompUnitContext *ctx) {
|
|
if(ctx->decl().empty() && ctx->funcDef().empty())
|
|
return nullptr;
|
|
for (auto dcl : ctx->decl()) {dcl->accept(this);cout << '\n';}cout << '\n';
|
|
for (auto func : ctx->funcDef()) {func->accept(this);cout << "\n";}
|
|
return nullptr;
|
|
}
|
|
|
|
// std::any ASTPrinter::visitBType(SysYParser::BTypeContext *ctx);
|
|
// std::any ASTPrinter::visitDecl(SysYParser::DeclContext *ctx);
|
|
|
|
std::any ASTPrinter::visitConstDecl(SysYParser::ConstDeclContext *ctx) {
|
|
cout << getIndent() << ctx->CONST()->getText() << ' ' << ctx->bType()->getText() << ' ';
|
|
auto numConstDefs = ctx->constDef().size();
|
|
ctx->constDef(0)->accept(this);
|
|
for (int i = 1; i < numConstDefs; ++i) {
|
|
cout << ctx->COMMA(i - 1)->getText() << ' ';
|
|
ctx->constDef(i)->accept(this);
|
|
}
|
|
cout << ctx->SEMICOLON()->getText() << '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitConstDef(SysYParser::ConstDefContext *ctx) {
|
|
cout << ctx->Ident()->getText();
|
|
auto numConstExps = ctx->constExp().size();
|
|
for (int i = 0; i < numConstExps; ++i) {
|
|
cout << ctx->LBRACK(i)->getText();
|
|
ctx->constExp(i)->accept(this);
|
|
cout << ctx->RBRACK(i)->getText();
|
|
}
|
|
cout << ' ' << ctx->ASSIGN()->getText() << ' ';
|
|
ctx->constInitVal()->accept(this);
|
|
return nullptr;
|
|
}
|
|
|
|
// std::any ASTPrinter::visitConstInitVal(SysYParser::ConstInitValContext *ctx);
|
|
|
|
std::any ASTPrinter::visitVarDecl(SysYParser::VarDeclContext *ctx){
|
|
cout << getIndent() << ctx->bType()->getText() << ' ';
|
|
auto numVarDefs = ctx->varDef().size();
|
|
ctx->varDef(0)->accept(this);
|
|
for (int i = 1; i < numVarDefs; ++i) {
|
|
cout << ", ";
|
|
ctx->varDef(i)->accept(this);
|
|
}
|
|
cout << ctx->SEMICOLON()->getText() << '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitVarDef(SysYParser::VarDefContext *ctx){
|
|
cout << ctx->Ident()->getText();
|
|
auto numConstExps = ctx->constExp().size();
|
|
for (int i = 0; i < numConstExps; ++i) {
|
|
cout << ctx->LBRACK(i)->getText();
|
|
ctx->constExp(i)->accept(this);
|
|
cout << ctx->RBRACK(i)->getText();
|
|
}
|
|
if (ctx->initVal()) {
|
|
cout << ' ' << ctx->ASSIGN()->getText() << ' ';
|
|
ctx->initVal()->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitInitVal(SysYParser::InitValContext *ctx){
|
|
if (ctx->exp()) {
|
|
ctx->exp()->accept(this);
|
|
} else {
|
|
cout << ctx->LBRACE()->getText();
|
|
auto numInitVals = ctx->initVal().size();
|
|
ctx->initVal(0)->accept(this);
|
|
for (int i = 1; i < numInitVals; ++i) {
|
|
cout << ctx->COMMA(i - 1)->getText() << ' ';
|
|
ctx->initVal(i)->accept(this);
|
|
}
|
|
cout << ctx->RBRACE()->getText();
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
|
cout << getIndent() << ctx->funcType()->getText() << ' ' << ctx->Ident()->getText();
|
|
cout << ctx->LPAREN()->getText();
|
|
if (ctx->funcFParams()) ctx->funcFParams()->accept(this);
|
|
if(ctx->RPAREN())
|
|
cout << ctx->RPAREN()->getText();
|
|
else
|
|
cout << "<missing \')\'?>";
|
|
ctx->blockStmt()->accept(this);
|
|
return nullptr;
|
|
}
|
|
|
|
// std::any ASTPrinter::visitFuncType(SysYParser::FuncTypeContext *ctx);
|
|
|
|
std::any ASTPrinter::visitFuncFParams(SysYParser::FuncFParamsContext *ctx){
|
|
auto numFuncFParams = ctx->funcFParam().size();
|
|
ctx->funcFParam(0)->accept(this);
|
|
for (int i = 1; i < numFuncFParams; ++i) {
|
|
cout << ctx->COMMA(i - 1)->getText() << ' ';
|
|
ctx->funcFParam(i)->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitFuncFParam(SysYParser::FuncFParamContext *ctx){
|
|
cout << ctx->bType()->getText() << ' ' << ctx->Ident()->getText();
|
|
if (!ctx->exp().empty()) {
|
|
cout << "[]";
|
|
for (auto exp : ctx->exp()) {
|
|
cout << '[';
|
|
exp->accept(this);
|
|
cout << ']';
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitBlockStmt(SysYParser::BlockStmtContext *ctx){
|
|
cout << ctx->LBRACE()->getText() << endl;
|
|
indentLevel++;
|
|
for (auto item : ctx->blockItem()) item->accept(this);
|
|
indentLevel--;
|
|
cout << getIndent() << ctx->RBRACE()->getText() << endl;
|
|
return nullptr;
|
|
}
|
|
// std::any ASTPrinter::visitBlockItem(SysYParser::BlockItemContext *ctx);
|
|
|
|
std::any ASTPrinter::visitAssignStmt(SysYParser::AssignStmtContext *ctx){
|
|
cout << getIndent();
|
|
ctx->lValue()->accept(this);
|
|
cout << ' ' << ctx->ASSIGN()->getText() << ' ';
|
|
ctx->exp()->accept(this);
|
|
cout << ctx->SEMICOLON()->getText() << '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitExpStmt(SysYParser::ExpStmtContext *ctx){
|
|
cout << getIndent();
|
|
if (ctx->exp()) {
|
|
ctx->exp()->accept(this);
|
|
}
|
|
cout << ctx->SEMICOLON()->getText() << '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitIfStmt(SysYParser::IfStmtContext *ctx){
|
|
cout << getIndent() << ctx->IF()->getText() << ' ' << ctx->LPAREN()->getText();
|
|
ctx->cond()->accept(this);
|
|
cout << ctx->RPAREN()->getText() << ' ';
|
|
//格式化有问题
|
|
if(ctx->stmt(0)) {
|
|
ctx->stmt(0)->accept(this);
|
|
}
|
|
else {
|
|
cout << '{' << endl;
|
|
indentLevel++;
|
|
ctx->stmt(0)->accept(this);
|
|
indentLevel--;
|
|
cout << getIndent() << '}' << endl;
|
|
}
|
|
if (ctx->ELSE()) {
|
|
cout << getIndent() << ctx->ELSE()->getText() << ' ';
|
|
ctx->stmt(1)->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitWhileStmt(SysYParser::WhileStmtContext *ctx){
|
|
cout << getIndent() << ctx->WHILE()->getText() << ' ' << ctx->LPAREN()->getText();
|
|
ctx->cond()->accept(this);
|
|
cout << ctx->RPAREN()->getText() << ' ';
|
|
ctx->stmt()->accept(this);
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitBreakStmt(SysYParser::BreakStmtContext *ctx){
|
|
cout << getIndent() << ctx->BREAK()->getText() << ctx->SEMICOLON()->getText() << '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitContinueStmt(SysYParser::ContinueStmtContext *ctx){
|
|
cout << getIndent() << ctx->CONTINUE()->getText() << ctx->SEMICOLON()->getText() << '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitReturnStmt(SysYParser::ReturnStmtContext *ctx){
|
|
cout << getIndent() << ctx->RETURN()->getText() << ' ';
|
|
if (ctx->exp()) {
|
|
ctx->exp()->accept(this);
|
|
}
|
|
cout << ctx->SEMICOLON()->getText() << '\n';
|
|
return nullptr;
|
|
}
|
|
|
|
// std::any ASTPrinter::visitExp(SysYParser::ExpContext *ctx);
|
|
// std::any ASTPrinter::visitCond(SysYParser::CondContext *ctx);
|
|
std::any ASTPrinter::visitLValue(SysYParser::LValueContext *ctx){
|
|
cout << ctx->Ident()->getText();
|
|
for (auto exp : ctx->exp()) {
|
|
cout << "[";
|
|
exp->accept(this);
|
|
cout << "]";
|
|
}
|
|
return nullptr;
|
|
}
|
|
// std::any ASTPrinter::visitPrimaryExp(SysYParser::PrimaryExpContext *ctx);
|
|
std::any ASTPrinter::visitParenExp(SysYParser::ParenExpContext *ctx){
|
|
cout << ctx->LPAREN()->getText();
|
|
ctx->exp()->accept(this);
|
|
cout << ctx->RPAREN()->getText();
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitNumber(SysYParser::NumberContext *ctx) {
|
|
if(ctx->ILITERAL())cout << ctx->ILITERAL()->getText();
|
|
if(ctx->FLITERAL())cout << ctx->FLITERAL()->getText();
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitString(SysYParser::StringContext *ctx) {
|
|
cout << ctx->STRING()->getText();
|
|
return nullptr;
|
|
}
|
|
// std::any visitUnaryExp(SysYParser::UnaryExpContext *ctx);
|
|
// std::any ASTPrinter::visitUnaryOp(SysYParser::UnaryOpContext *ctx);
|
|
std::any ASTPrinter::visitCall(SysYParser::CallContext *ctx){
|
|
cout << ctx->Ident()->getText() << ctx->LPAREN()->getText();
|
|
if(ctx->funcRParams())
|
|
ctx->funcRParams()->accept(this);
|
|
cout << ctx->RPAREN()->getText();
|
|
return nullptr;
|
|
}
|
|
|
|
any ASTPrinter::visitFuncRParams(SysYParser::FuncRParamsContext *ctx) {
|
|
if (ctx->exp().empty())
|
|
return nullptr;
|
|
auto numParams = ctx->exp().size();
|
|
ctx->exp(0)->accept(this);
|
|
for (int i = 1; i < numParams; ++i) {
|
|
cout << ctx->COMMA(i - 1)->getText() << ' ';
|
|
ctx->exp(i)->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitMulExp(SysYParser::MulExpContext *ctx){
|
|
auto unaryExps = ctx->unaryExp();
|
|
if (unaryExps.size() == 1) {
|
|
unaryExps[0]->accept(this);
|
|
} else {
|
|
for (size_t i = 0; i < unaryExps.size() - 1; ++i) {
|
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
|
if (opNode) {
|
|
unaryExps[i]->accept(this);
|
|
cout << " " << opNode->getText() << " ";
|
|
}
|
|
}
|
|
unaryExps.back()->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::any ASTPrinter::visitAddExp(SysYParser::AddExpContext *ctx){
|
|
auto mulExps = ctx->mulExp();
|
|
if (mulExps.size() == 1) {
|
|
mulExps[0]->accept(this);
|
|
} else {
|
|
for (size_t i = 0; i < mulExps.size() - 1; ++i) {
|
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
|
if (opNode) {
|
|
mulExps[i]->accept(this);
|
|
cout << " " << opNode->getText() << " ";
|
|
}
|
|
}
|
|
mulExps.back()->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
// 以下表达式待补全形式同addexp mulexp
|
|
std::any ASTPrinter::visitRelExp(SysYParser::RelExpContext *ctx){
|
|
auto relExps = ctx->addExp();
|
|
if (relExps.size() == 1) {
|
|
relExps[0]->accept(this);
|
|
} else {
|
|
for (size_t i = 0; i < relExps.size() - 1; ++i) {
|
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
|
if (opNode) {
|
|
relExps[i]->accept(this);
|
|
cout << " " << opNode->getText() << " ";
|
|
}
|
|
}
|
|
relExps.back()->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
std::any ASTPrinter::visitEqExp(SysYParser::EqExpContext *ctx){
|
|
auto eqExps = ctx->relExp();
|
|
if (eqExps.size() == 1) {
|
|
eqExps[0]->accept(this);
|
|
} else {
|
|
for (size_t i = 0; i < eqExps.size() - 1; ++i) {
|
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
|
if (opNode) {
|
|
eqExps[i]->accept(this);
|
|
cout << " " << opNode->getText() << " ";
|
|
}
|
|
}
|
|
eqExps.back()->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
std::any ASTPrinter::visitLAndExp(SysYParser::LAndExpContext *ctx){
|
|
auto lAndExps = ctx->eqExp();
|
|
if (lAndExps.size() == 1) {
|
|
lAndExps[0]->accept(this);
|
|
} else {
|
|
for (size_t i = 0; i < lAndExps.size() - 1; ++i) {
|
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
|
if (opNode) {
|
|
lAndExps[i]->accept(this);
|
|
cout << " " << opNode->getText() << " ";
|
|
}
|
|
}
|
|
lAndExps.back()->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
std::any ASTPrinter::visitLOrExp(SysYParser::LOrExpContext *ctx){
|
|
auto lOrExps = ctx->lAndExp();
|
|
if (lOrExps.size() == 1) {
|
|
lOrExps[0]->accept(this);
|
|
} else {
|
|
for (size_t i = 0; i < lOrExps.size() - 1; ++i) {
|
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
|
if (opNode) {
|
|
lOrExps[i]->accept(this);
|
|
cout << " " << opNode->getText() << " ";
|
|
}
|
|
}
|
|
lOrExps.back()->accept(this);
|
|
}
|
|
return nullptr;
|
|
}
|
|
std::any ASTPrinter::visitConstExp(SysYParser::ConstExpContext *ctx){
|
|
ctx->addExp()->accept(this);
|
|
return nullptr;
|
|
} |