Split work
This commit is contained in:
@@ -9,6 +9,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
|
|||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
|
src/toBFListener.cpp
|
||||||
lib/generated/bfLexer.cpp
|
lib/generated/bfLexer.cpp
|
||||||
lib/generated/bfParser.cpp
|
lib/generated/bfParser.cpp
|
||||||
lib/generated/bfBaseVisitor.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_link_libraries(main.out ${PROJECT_SOURCE_DIR}/lib/antlr4/lib/libantlr4-runtime.a)
|
||||||
|
|
||||||
target_include_directories(main.out
|
target_include_directories(main.out
|
||||||
PRIVATE
|
PUBLIC
|
||||||
|
${PROJECT_SOURCE_DIR}/lib/generated
|
||||||
${PROJECT_SOURCE_DIR}/include
|
${PROJECT_SOURCE_DIR}/include
|
||||||
${PROJECT_SOURCE_DIR}/lib/antlr4/include
|
${PROJECT_SOURCE_DIR}/lib/antlr4/include
|
||||||
${PROJECT_SOURCE_DIR}/lib/generated
|
|
||||||
|
|
||||||
)
|
)
|
||||||
|
@@ -1,10 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include<string>
|
#include<string>
|
||||||
|
#include "bfLexer.h"
|
||||||
|
#include "bfParser.h"
|
||||||
#include "bfBaseListener.h"
|
#include "bfBaseListener.h"
|
||||||
|
|
||||||
|
|
||||||
class expressionPrintingListener : public bfBaseListener {
|
class toBFListener : public bfBaseListener {
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::string> printStack;
|
std::vector<std::string> printStack;
|
||||||
public:
|
public:
|
62
src/main.cpp
62
src/main.cpp
@@ -2,70 +2,14 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <antlr4-common.h>
|
#include <antlr4-common.h>
|
||||||
#include "main.hpp"
|
|
||||||
#include "bfLexer.h"
|
#include "bfLexer.h"
|
||||||
#include "bfParser.h"
|
#include "bfParser.h"
|
||||||
#include "bfBaseListener.h"
|
#include "toBFListener.hpp"
|
||||||
|
|
||||||
// #include "bfBaseVisitor.h"
|
// #include "bfBaseVisitor.h"
|
||||||
|
|
||||||
using namespace antlr4;
|
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[])
|
int main(int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
std::ifstream stream;
|
std::ifstream stream;
|
||||||
@@ -82,7 +26,7 @@ int main(int argc, const char *argv[])
|
|||||||
bfParser parser(&tokens);
|
bfParser parser(&tokens);
|
||||||
|
|
||||||
tree::ParseTree *tree = parser.program();
|
tree::ParseTree *tree = parser.program();
|
||||||
expressionPrintingListener listener;
|
toBFListener listener;
|
||||||
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
|
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
63
src/toBFListener.cpp
Normal file
63
src/toBFListener.cpp
Normal file
@@ -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);
|
||||||
|
}
|
Reference in New Issue
Block a user