Commit for experimental 1
This commit is contained in:
@@ -1,149 +1,205 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include "ASTPrinter.h"
|
||||
#include "SysYParser.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
std::any ASTPrinter::visitNumber(SysYParser::NumberContext *ctx) {
|
||||
cout << ctx->ILITERAL()->getText();
|
||||
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::visitString(SysYParser::StringContext *ctx) {
|
||||
cout << ctx->STRING()->getText();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitModule(SysYParser::ModuleContext *ctx) {
|
||||
for (auto dcl : ctx->dcl()) dcl->accept(this);
|
||||
for (auto func : ctx->funcDef()) func->accept(this);
|
||||
if (ctx->funcRParams()) ctx->funcRParams()->accept(this);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitFuncRParams(SysYParser::FuncRParamsContext *ctx) {
|
||||
bool first = true;
|
||||
for (auto param : ctx->funcRParam()) {
|
||||
if (!first) cout << ", ";
|
||||
param->accept(this);
|
||||
first = false;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitExpAsRParam(SysYParser::ExpAsRParamContext *ctx) {
|
||||
ctx->number()->accept(this);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitStringAsRParam(SysYParser::StringAsRParamContext *ctx) {
|
||||
ctx->string()->accept(this);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitExpsAsRParam(SysYParser::ExpsAsRParamContext *ctx) {
|
||||
bool first = true;
|
||||
for (auto exp : ctx->exp()) {
|
||||
if (!first) cout << ", ";
|
||||
exp->accept(this);
|
||||
first = false;
|
||||
}
|
||||
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() << "const " << ctx->bType()->getText() << " ";
|
||||
bool first = true;
|
||||
for (auto def : ctx->constDef()) {
|
||||
if (!first) cout << ", ";
|
||||
def->accept(this);
|
||||
first = false;
|
||||
}
|
||||
cout << ";" << endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitVarDecl(SysYParser::VarDeclContext *ctx) {
|
||||
cout << getIndent() << ctx->bType()->getText() << " ";
|
||||
bool first = true;
|
||||
for (auto def : ctx->varDef()) {
|
||||
if (!first) cout << ", ";
|
||||
def->accept(this);
|
||||
first = false;
|
||||
}
|
||||
cout << ";" << endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitVarDef(SysYParser::VarDefContext *ctx) {
|
||||
cout << ctx->IDENT()->getText();
|
||||
for (auto exp : ctx->constExp()) {
|
||||
cout << "[";
|
||||
exp->accept(this);
|
||||
cout << "]";
|
||||
}
|
||||
if (ctx->initVal()) {
|
||||
cout << " = ";
|
||||
ctx->initVal()->accept(this);
|
||||
}
|
||||
return nullptr;
|
||||
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();
|
||||
for (auto exp : ctx->constExp()) {
|
||||
cout << "[";
|
||||
exp->accept(this);
|
||||
cout << "]";
|
||||
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->constInitVal()->accept(this);
|
||||
return nullptr;
|
||||
cout << ctx->RBRACE()->getText();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitFuncDef(SysYParser::FuncDefContext *ctx) {
|
||||
cout << getIndent() << ctx->funcType()->getText() << " " << ctx->IDENT()->getText() << "(";
|
||||
if (ctx->funcFParams()) ctx->funcFParams()->accept(this);
|
||||
cout << ")" << endl;
|
||||
ctx->block()->accept(this);
|
||||
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);
|
||||
cout << ctx->RPAREN()->getText();
|
||||
ctx->blockStmt()->accept(this);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitFuncFParams(SysYParser::FuncFParamsContext *ctx) {
|
||||
bool first = true;
|
||||
for (auto param : ctx->funcFParam()) {
|
||||
if (!first) cout << ", ";
|
||||
param->accept(this);
|
||||
first = false;
|
||||
// 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;
|
||||
}
|
||||
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 << "]";
|
||||
}
|
||||
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::visitStmt(SysYParser::StmtContext *ctx){
|
||||
if(ctx->lValue()&& ctx->exp()) {
|
||||
cout << getIndent();
|
||||
ctx->lValue()->accept(this);
|
||||
cout << ' ' << ctx->ASSIGN()->getText() <<' ';
|
||||
ctx->exp()->accept(this);
|
||||
cout << ctx->SEMICOLON()->getText() << endl;
|
||||
}
|
||||
else if(ctx->blockStmt()){
|
||||
ctx->blockStmt()->accept(this);
|
||||
}
|
||||
else if(ctx->IF()){
|
||||
cout << getIndent() << "if (";
|
||||
ctx->cond()->accept(this);
|
||||
cout << ")";
|
||||
// visit ctx->stmt(0) to judge if it is a blockStmt or a Lvale=exp
|
||||
if(ctx->stmt(0)->blockStmt())ctx->stmt(0)->accept(this);
|
||||
else{
|
||||
cout << " {"<< endl;
|
||||
indentLevel++;
|
||||
ctx->stmt(0)->accept(this);
|
||||
indentLevel--;
|
||||
cout << getIndent() << "}" << endl;
|
||||
}
|
||||
return nullptr;
|
||||
if (ctx->stmt().size() > 1) {
|
||||
cout << getIndent() << "else" << endl;
|
||||
if(ctx->stmt(1)->blockStmt())ctx->stmt(1)->accept(this);
|
||||
else{
|
||||
cout << " {"<< endl;
|
||||
indentLevel++;
|
||||
ctx->stmt(1)->accept(this);
|
||||
indentLevel--;
|
||||
cout << getIndent() << "}" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(ctx->WHILE()){
|
||||
cout << getIndent() << "while (";
|
||||
ctx->cond()->accept(this);
|
||||
cout << ")";
|
||||
if(ctx->stmt(0)->blockStmt())ctx->stmt(0)->accept(this);
|
||||
else{
|
||||
cout << " {"<< endl;
|
||||
indentLevel++;
|
||||
ctx->stmt(0)->accept(this);
|
||||
indentLevel--;
|
||||
cout << getIndent() << "}" << endl;
|
||||
}
|
||||
}
|
||||
else if(ctx->BREAK()){
|
||||
cout << getIndent() << "break;" << endl;
|
||||
}
|
||||
else if(ctx->CONTINUE()){
|
||||
cout << getIndent() << "continue;" << endl;
|
||||
}
|
||||
else if(ctx->RETURN()){
|
||||
cout << getIndent() << "return";
|
||||
if(ctx->exp()){
|
||||
cout << ' ';
|
||||
ctx->exp()->accept(this);
|
||||
}
|
||||
cout << ctx->SEMICOLON()->getText() << endl;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitExp(SysYParser::ExpContext *ctx) {
|
||||
ctx->addExp()->accept(this);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitCond(SysYParser::CondContext *ctx) {
|
||||
ctx->lorExp()->accept(this);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitLVal(SysYParser::LValContext *ctx) {
|
||||
cout << ctx->IDENT()->getText();
|
||||
// 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);
|
||||
@@ -151,118 +207,149 @@ std::any ASTPrinter::visitLVal(SysYParser::LValContext *ctx) {
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
// std::any ASTPrinter::visitPrimaryExp(SysYParser::PrimaryExpContext *ctx);
|
||||
|
||||
std::any ASTPrinter::visitAddExp(SysYParser::AddExpContext *ctx) {
|
||||
if (ctx->addExp()) {
|
||||
ctx->addExp()->accept(this);
|
||||
cout << " " << ctx->ADD()->getText() << " ";
|
||||
ctx->mulExp()->accept(this);
|
||||
} else {
|
||||
ctx->mulExp()->accept(this);
|
||||
}
|
||||
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::visitMulExp(SysYParser::MulExpContext *ctx) {
|
||||
auto unaryExps = ctx->unaryExp();
|
||||
if (unaryExps.size() == 1) {
|
||||
unaryExps[0]->accept(this);
|
||||
std::any ASTPrinter::visitString(SysYParser::StringContext *ctx) {
|
||||
cout << ctx->STRING()->getText();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitUnaryExp(SysYParser::UnaryExpContext *ctx){
|
||||
if(ctx->primaryExp())
|
||||
ctx->primaryExp()->accept(this);
|
||||
else if(ctx->Ident()){
|
||||
cout << ctx->Ident()->getText() << ctx->LPAREN()->getText();
|
||||
if(ctx->funcRParams())
|
||||
ctx->funcRParams()->accept(this);
|
||||
cout << ctx->RPAREN()->getText();
|
||||
}
|
||||
else if(ctx->unaryExp()){
|
||||
cout << ctx->unaryOp()->getText();
|
||||
ctx->unaryExp()->accept(this);
|
||||
}
|
||||
else
|
||||
ctx->accept(this);
|
||||
return nullptr;
|
||||
}
|
||||
// std::any ASTPrinter::visitUnaryOp(SysYParser::UnaryOpContext *ctx);
|
||||
|
||||
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 < unaryExps.size() - 1; ++i) {
|
||||
for (size_t i = 0; i < relExps.size() - 1; ++i) {
|
||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
||||
if (opNode) {
|
||||
unaryExps[i]->accept(this);
|
||||
relExps[i]->accept(this);
|
||||
cout << " " << opNode->getText() << " ";
|
||||
}
|
||||
}
|
||||
unaryExps.back()->accept(this);
|
||||
relExps.back()->accept(this);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::any ASTPrinter::visitUnaryExp(SysYParser::UnaryExpContext *ctx) {
|
||||
if (ctx->primaryExp()) {
|
||||
ctx->primaryExp()->accept(this);
|
||||
} else if (ctx->IDENT()) {
|
||||
cout << ctx->IDENT()->getText() << "(";
|
||||
if (ctx->funcRParams()) ctx->funcRParams()->accept(this);
|
||||
cout << ")";
|
||||
} else if (ctx->unaryOp()) {
|
||||
cout << ctx->unaryOp()->getText();
|
||||
ctx->unaryExp()->accept(this);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
// std::any ASTPrinter::visitLorExp(SysYParser::LorExpContext *ctx) {
|
||||
// if (ctx->lorExp()) {
|
||||
// // 左递归部分
|
||||
// ctx->lorExp()->accept(this);
|
||||
// cout << " || ";
|
||||
// ctx->landExp()->accept(this);
|
||||
// } else {
|
||||
// // 基础部分
|
||||
// ctx->landExp()->accept(this);
|
||||
// }
|
||||
// return nullptr;
|
||||
// }
|
||||
|
||||
std::any ASTPrinter::visitStmt(SysYParser::StmtContext *ctx) {
|
||||
if (ctx->lVal() && ctx->exp()) {
|
||||
cout << getIndent();
|
||||
ctx->lVal()->accept(this);
|
||||
cout << " = ";
|
||||
ctx->exp()->accept(this);
|
||||
cout << ";" << endl;
|
||||
}
|
||||
// else if (ctx->exp()) {
|
||||
// cout << getIndent();
|
||||
// ctx->exp()->accept(this);
|
||||
// cout << ";" << endl;
|
||||
// }
|
||||
else if (ctx->block()) {
|
||||
ctx->block()->accept(this);
|
||||
std::any ASTPrinter::visitEqExp(SysYParser::EqExpContext *ctx){
|
||||
auto eqExps = ctx->relExp();
|
||||
if (eqExps.size() == 1) {
|
||||
eqExps[0]->accept(this);
|
||||
} else {
|
||||
for (auto child : ctx->children) {
|
||||
if (auto terminal = dynamic_cast<antlr4::tree::TerminalNode *>(child)) {
|
||||
if (terminal->getText() == "if") {
|
||||
cout << getIndent() << "if (";
|
||||
ctx->cond()->accept(this);
|
||||
cout << ")" << endl;
|
||||
ctx->stmt(0)->accept(this);
|
||||
if (ctx->stmt().size() > 1) {
|
||||
cout << getIndent() << "else" << endl;
|
||||
ctx->stmt(1)->accept(this);
|
||||
}
|
||||
} else if (terminal->getText() == "while") {
|
||||
cout << getIndent() << "while (";
|
||||
ctx->cond()->accept(this);
|
||||
cout << ")" << endl;
|
||||
ctx->stmt(0)->accept(this);
|
||||
} else if (terminal->getText() == "return") {
|
||||
cout << getIndent() << "return";
|
||||
if (ctx->exp()) {
|
||||
cout << " ";
|
||||
ctx->exp()->accept(this);
|
||||
}
|
||||
cout << ";" << endl;
|
||||
} else if (terminal->getText() == "break") {
|
||||
cout << getIndent() << "break;" << endl;
|
||||
} else if (terminal->getText() == "continue") {
|
||||
cout << getIndent() << "continue;" << endl;
|
||||
}
|
||||
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::visitBlock(SysYParser::BlockContext *ctx) {
|
||||
cout << getIndent() << "{" << endl;
|
||||
indentLevel++;
|
||||
for (auto item : ctx->blockItem()) item->accept(this);
|
||||
indentLevel--;
|
||||
cout << getIndent() << "}" << endl;
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user