Add more samples, simplify grammar, add groups
This commit is contained in:
88
src/main.cpp
88
src/main.cpp
@@ -1,4 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <antlr4-common.h>
|
||||
#include "main.hpp"
|
||||
#include "bfLexer.h"
|
||||
@@ -8,44 +10,70 @@
|
||||
|
||||
using namespace antlr4;
|
||||
|
||||
class expressionPrintingListener : public bfBaseListener
|
||||
void expressionPrintingListener::enterProgram(bfParser::ProgramContext *ctx)
|
||||
{
|
||||
public:
|
||||
void enterStatement(bfParser::StatementContext *ctx) override
|
||||
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++)
|
||||
{
|
||||
std::cout << ctx->getText();
|
||||
printStack.back() += s;
|
||||
}
|
||||
void enterNumberedStatement(bfParser::NumberedStatementContext *ctx) override
|
||||
{
|
||||
int num = 0;
|
||||
try{
|
||||
num = stoi(ctx->NUMBER()->getText());
|
||||
}catch(std::exception e){
|
||||
num = 1;
|
||||
}
|
||||
for (int i = 0; i < num - 1; i++)
|
||||
{
|
||||
std::cout << ctx->statement()->getText();
|
||||
}
|
||||
}
|
||||
void exitProgram(bfParser::ProgramContext *ctx) override{
|
||||
std::cout<<std::endl;
|
||||
}
|
||||
void enterLoopStmt(bfParser::LoopStmtContext *ctx) override{
|
||||
std::cout<<'[';
|
||||
}
|
||||
void exitLoopStmt(bfParser::LoopStmtContext *ctx) override{
|
||||
std::cout<<']';
|
||||
}
|
||||
};
|
||||
}
|
||||
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;
|
||||
|
||||
bool outFile = false;
|
||||
stream.open(argv[1]);
|
||||
if(stream.fail()){
|
||||
std::cout<<"Could not open"<<std::endl;
|
||||
if (stream.fail())
|
||||
{
|
||||
std::cout << "Could not open" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
ANTLRInputStream input(stream);
|
||||
|
Reference in New Issue
Block a user