diff --git a/CMakeLists.txt b/CMakeLists.txt index b53171f..e50c42a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) set(SOURCES src/main.cpp + src/toBFListener.cpp lib/generated/bfLexer.cpp lib/generated/bfParser.cpp lib/generated/bfBaseVisitor.cpp @@ -21,9 +22,9 @@ add_executable(main.out ${SOURCES}) target_link_libraries(main.out ${PROJECT_SOURCE_DIR}/lib/antlr4/lib/libantlr4-runtime.a) target_include_directories(main.out - PRIVATE + PUBLIC + ${PROJECT_SOURCE_DIR}/lib/generated ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/lib/antlr4/include - ${PROJECT_SOURCE_DIR}/lib/generated ) diff --git a/include/main.hpp b/include/toBFListener.hpp similarity index 91% rename from include/main.hpp rename to include/toBFListener.hpp index d30286d..fd31166 100644 --- a/include/main.hpp +++ b/include/toBFListener.hpp @@ -1,10 +1,12 @@ #pragma once #include +#include "bfLexer.h" +#include "bfParser.h" #include "bfBaseListener.h" -class expressionPrintingListener : public bfBaseListener { +class toBFListener : public bfBaseListener { protected: std::vector printStack; public: diff --git a/src/main.cpp b/src/main.cpp index 201f6c4..47459a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,70 +2,14 @@ #include #include #include -#include "main.hpp" #include "bfLexer.h" #include "bfParser.h" -#include "bfBaseListener.h" +#include "toBFListener.hpp" + // #include "bfBaseVisitor.h" using namespace antlr4; -void expressionPrintingListener::enterProgram(bfParser::ProgramContext *ctx) -{ - 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++) - { - printStack.back() += s; - } -} -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); -} - int main(int argc, const char *argv[]) { std::ifstream stream; @@ -82,7 +26,7 @@ int main(int argc, const char *argv[]) bfParser parser(&tokens); tree::ParseTree *tree = parser.program(); - expressionPrintingListener listener; + toBFListener listener; tree::ParseTreeWalker::DEFAULT.walk(&listener, tree); return 0; diff --git a/src/toBFListener.cpp b/src/toBFListener.cpp new file mode 100644 index 0000000..92b075e --- /dev/null +++ b/src/toBFListener.cpp @@ -0,0 +1,63 @@ +#pragma once +#include "bfParser.h" +#include "bfBaseListener.h" +#include "toBFListener.hpp" +// #include "bfBaseVisitor.h" + +using namespace antlr4; + +void toBFListener::enterProgram(bfParser::ProgramContext *ctx) +{ + printStack.push_back(""); +} +void toBFListener::exitProgram(bfParser::ProgramContext *ctx) +{ + std::cout << printStack.front() << std::endl; +} +void toBFListener::enterPtrIncr(bfParser::PtrIncrContext *ctx) +{ + printStack.back() += "+"; +} +void toBFListener::enterPtrDecr(bfParser::PtrDecrContext *ctx) +{ + printStack.back() += ("-"); +} +void toBFListener::enterPtrLeft(bfParser::PtrLeftContext *ctx) +{ + printStack.back() += ("<"); +} +void toBFListener::enterPtrRight(bfParser::PtrRightContext *ctx) +{ + printStack.back() += (">"); +} + +void toBFListener::enterNumberedStmt(bfParser::NumberedStmtContext *ctx) +{ + printStack.push_back(""); +} +void toBFListener::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++) + { + printStack.back() += s; + } +} +void toBFListener::enterLoopStmt(bfParser::LoopStmtContext *ctx){ + printStack.push_back(""); +} +void toBFListener::exitLoopStmt(bfParser::LoopStmtContext *ctx){ + std::string s = printStack.back(); + printStack.pop_back(); + printStack.back()+= "["+s+"]"; +} +void toBFListener::enterGroupedStmt(bfParser::GroupedStmtContext *ctx){ + printStack.push_back(""); +} +void toBFListener::exitGroupedStmt(bfParser::GroupedStmtContext *ctx){ + std::string s = printStack.back(); + printStack.pop_back(); + printStack.back()+=(s); +} \ No newline at end of file