-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoBFListener.cpp
73 lines (65 loc) · 1.83 KB
/
toBFListener.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "bfeParser.h"
#include "bfeBaseListener.h"
#include "toBFListener.hpp"
// #include "bfBaseVisitor.h"
using namespace antlr4;
void toBFListener::enterProgram(bfeParser::ProgramContext *ctx)
{
printStack.push_back("");
}
void toBFListener::exitProgram(bfeParser::ProgramContext *ctx)
{
std::cout << printStack.front() << std::endl;
}
void toBFListener::enterPtrIncr(bfeParser::PtrIncrContext *ctx)
{
printStack.back() += "+";
}
void toBFListener::enterPtrDecr(bfeParser::PtrDecrContext *ctx)
{
printStack.back() += ("-");
}
void toBFListener::enterPtrLeft(bfeParser::PtrLeftContext *ctx)
{
printStack.back() += ("<");
}
void toBFListener::enterPtrRight(bfeParser::PtrRightContext *ctx)
{
printStack.back() += (">");
}
void toBFListener::enterOutputStmt(bfeParser::OutputStmtContext *ctx) {
printStack.back() += (".");
}
void toBFListener::enterInputStmt(bfeParser::InputStmtContext *ctx) {
printStack.back() += (",");
}
void toBFListener::enterNumberedStmt(bfeParser::NumberedStmtContext *ctx)
{
printStack.push_back("");
}
void toBFListener::exitNumberedStmt(bfeParser::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(bfeParser::LoopStmtContext *ctx){
printStack.push_back("");
}
void toBFListener::exitLoopStmt(bfeParser::LoopStmtContext *ctx){
std::string s = printStack.back();
printStack.pop_back();
printStack.back()+= "["+s+"]";
}
void toBFListener::enterGroupedStmt(bfeParser::GroupedStmtContext *ctx){
printStack.push_back("");
}
void toBFListener::exitGroupedStmt(bfeParser::GroupedStmtContext *ctx){
std::string s = printStack.back();
printStack.pop_back();
printStack.back()+=(s);
}