add file
This commit is contained in:
@@ -133,69 +133,58 @@ std::any ASTPrinter::visitBlockStmt(SysYParser::BlockStmtContext *ctx){
|
||||
}
|
||||
// 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() <<' ';
|
||||
std::any ASTPrinter::visitAssignStmt(SysYParser::AssignStmtContext *ctx){
|
||||
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){
|
||||
if (ctx->exp()) {
|
||||
ctx->exp()->accept(this);
|
||||
cout << ctx->SEMICOLON()->getText() << endl;
|
||||
}
|
||||
else if(ctx->blockStmt()){
|
||||
ctx->blockStmt()->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() << ' ';
|
||||
ctx->stmt(0)->accept(this);
|
||||
if (ctx->ELSE()) {
|
||||
cout << getIndent() << ctx->ELSE()->getText() << ' ';
|
||||
ctx->stmt(1)->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;
|
||||
}
|
||||
if (ctx->stmt().size() > 1) {
|
||||
cout << getIndent() << "else";
|
||||
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::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;
|
||||
}
|
||||
|
||||
@@ -211,6 +200,12 @@ std::any ASTPrinter::visitLValue(SysYParser::LValueContext *ctx){
|
||||
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();
|
||||
@@ -222,28 +217,15 @@ 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);
|
||||
if (ctx->RPAREN())
|
||||
cout << ctx->RPAREN()->getText();
|
||||
else
|
||||
cout << "<missing \')\'?>";
|
||||
}
|
||||
else if(ctx->unaryExp()){
|
||||
cout << ctx->unaryOp()->getText();
|
||||
ctx->unaryExp()->accept(this);
|
||||
}
|
||||
else
|
||||
ctx->accept(this);
|
||||
// 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;
|
||||
}
|
||||
// std::any ASTPrinter::visitUnaryOp(SysYParser::UnaryOpContext *ctx);
|
||||
|
||||
any ASTPrinter::visitFuncRParams(SysYParser::FuncRParamsContext *ctx) {
|
||||
if (ctx->exp().empty())
|
||||
|
||||
Reference in New Issue
Block a user