2020-09-29 23:16:42 +05:30
|
|
|
#include <iostream>
|
2020-09-30 16:54:58 +05:30
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2020-09-29 23:16:42 +05:30
|
|
|
#include <antlr4-common.h>
|
|
|
|
#include "main.hpp"
|
|
|
|
#include "bfLexer.h"
|
|
|
|
#include "bfParser.h"
|
|
|
|
#include "bfBaseListener.h"
|
|
|
|
// #include "bfBaseVisitor.h"
|
2020-09-29 20:45:00 +05:30
|
|
|
|
2020-09-29 23:16:42 +05:30
|
|
|
using namespace antlr4;
|
|
|
|
|
2020-09-30 16:54:58 +05:30
|
|
|
void expressionPrintingListener::enterProgram(bfParser::ProgramContext *ctx)
|
2020-09-30 00:50:00 +05:30
|
|
|
{
|
2020-09-30 16:54:58 +05:30
|
|
|
printStack.push_back("");
|
|
|
|
}
|
|
|
|
void expressionPrintingListener::exitProgram(bfParser::ProgramContext *ctx)
|
|
|
|
{
|
|
|
|
std::cout << printStack.front() << std::endl;
|
|
|
|
}
|
|
|
|
void expressionPrintingListener::enterPtrIncr(bfParser::PtrIncrContext *ctx)
|
|
|
|
{
|
|
|
|
printStack.back() += "+";
|
|
|
|
}
|
|
|
|
void expressionPrintingListener::enterPtrDecr(bfParser::PtrDecrContext *ctx)
|
|
|
|
{
|
|
|
|
printStack.back() += ("-");
|
|
|
|
}
|
|
|
|
void expressionPrintingListener::enterPtrLeft(bfParser::PtrLeftContext *ctx)
|
|
|
|
{
|
|
|
|
printStack.back() += ("<");
|
|
|
|
}
|
|
|
|
void expressionPrintingListener::enterPtrRight(bfParser::PtrRightContext *ctx)
|
|
|
|
{
|
|
|
|
printStack.back() += (">");
|
|
|
|
}
|
|
|
|
|
|
|
|
void expressionPrintingListener::enterNumberedStmt(bfParser::NumberedStmtContext *ctx)
|
|
|
|
{
|
|
|
|
printStack.push_back("");
|
|
|
|
}
|
|
|
|
void expressionPrintingListener::exitNumberedStmt(bfParser::NumberedStmtContext *ctx)
|
|
|
|
{
|
|
|
|
std::string s = printStack.back();
|
|
|
|
printStack.pop_back();
|
|
|
|
int n = stoi(ctx->NUMBER()->getText());
|
|
|
|
for (int i = 0; i < n; i++)
|
2020-09-30 00:50:00 +05:30
|
|
|
{
|
2020-09-30 16:54:58 +05:30
|
|
|
printStack.back() += s;
|
2020-09-30 00:50:00 +05:30
|
|
|
}
|
2020-09-30 16:54:58 +05:30
|
|
|
}
|
|
|
|
void expressionPrintingListener::enterLoopStmt(bfParser::LoopStmtContext *ctx){
|
|
|
|
printStack.push_back("");
|
|
|
|
}
|
|
|
|
void expressionPrintingListener::exitLoopStmt(bfParser::LoopStmtContext *ctx){
|
|
|
|
std::string s = printStack.back();
|
|
|
|
printStack.pop_back();
|
|
|
|
printStack.back()+= "["+s+"]";
|
|
|
|
}
|
|
|
|
void expressionPrintingListener::enterGroupedStmt(bfParser::GroupedStmtContext *ctx){
|
|
|
|
printStack.push_back("");
|
|
|
|
}
|
|
|
|
void expressionPrintingListener::exitGroupedStmt(bfParser::GroupedStmtContext *ctx){
|
|
|
|
std::string s = printStack.back();
|
|
|
|
printStack.pop_back();
|
|
|
|
printStack.back()+=(s);
|
|
|
|
}
|
2020-09-29 23:16:42 +05:30
|
|
|
|
2020-09-30 00:50:00 +05:30
|
|
|
int main(int argc, const char *argv[])
|
2020-09-29 23:16:42 +05:30
|
|
|
{
|
|
|
|
std::ifstream stream;
|
2020-09-30 16:54:58 +05:30
|
|
|
bool outFile = false;
|
2020-09-30 01:15:57 +05:30
|
|
|
stream.open(argv[1]);
|
2020-09-30 16:54:58 +05:30
|
|
|
if (stream.fail())
|
|
|
|
{
|
|
|
|
std::cout << "Could not open" << std::endl;
|
2020-09-30 00:50:00 +05:30
|
|
|
return 1;
|
|
|
|
}
|
2020-09-29 23:16:42 +05:30
|
|
|
ANTLRInputStream input(stream);
|
|
|
|
bfLexer lexer(&input);
|
|
|
|
CommonTokenStream tokens(&lexer);
|
|
|
|
bfParser parser(&tokens);
|
|
|
|
|
|
|
|
tree::ParseTree *tree = parser.program();
|
2020-09-30 00:50:00 +05:30
|
|
|
expressionPrintingListener listener;
|
2020-09-29 23:16:42 +05:30
|
|
|
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
|
|
|
|
|
|
|
|
return 0;
|
2020-09-29 20:45:00 +05:30
|
|
|
}
|