-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoCPPListener.cpp
79 lines (72 loc) · 2.28 KB
/
toCPPListener.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
74
75
76
77
78
79
#include "bfeParser.h"
#include "bfeBaseListener.h"
#include "toCPPListener.hpp"
// #include "bfBaseVisitor.h"
using namespace antlr4;
void toCPPListener::enterProgram(bfeParser::ProgramContext *ctx)
{
printStack.push_back("#include<iostream>\n#include<vector>\nusing namespace std;\nint main(){\nvector<char> vec(1000,0);int ptr = 0;\n");
}
void toCPPListener::exitProgram(bfeParser::ProgramContext *ctx)
{
std::cout << printStack.front() << "}\n"<< std::endl;
}
void toCPPListener::enterPtrIncr(bfeParser::PtrIncrContext *ctx)
{
// printStack.back() += "+";
printStack.back()+= "vec[ptr]++;\n";
}
void toCPPListener::enterPtrDecr(bfeParser::PtrDecrContext *ctx)
{
// printStack.back() += ("-");
printStack.back()+= "vec[ptr]--;\n";
}
void toCPPListener::enterPtrLeft(bfeParser::PtrLeftContext *ctx)
{
// printStack.back() += ("<");
printStack.back() += ("ptr--;\n");
}
void toCPPListener::enterPtrRight(bfeParser::PtrRightContext *ctx)
{
printStack.back() += ("ptr++;\n");
}
void toCPPListener::enterNumberedStmt(bfeParser::NumberedStmtContext *ctx)
{
printStack.push_back("");
}
void toCPPListener::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 toCPPListener::enterLoopStmt(bfeParser::LoopStmtContext *ctx){
printStack.push_back("");
}
void toCPPListener::exitLoopStmt(bfeParser::LoopStmtContext *ctx){
std::string s = printStack.back();
printStack.pop_back();
printStack.back()+= "while(vec[ptr]){\n"+s+"}\n";
}
void toCPPListener::enterOutputStmt(bfeParser::OutputStmtContext *ctx){
std::string s = printStack.back();
// printStack.pop_back();
printStack.back()+= "cout<<vec[ptr];\n";
}
void toCPPListener::enterInputStmt(bfeParser::InputStmtContext *ctx){
// std::string s = printStack.back();
// printStack.pop_back();
printStack.back()+= "cin>>vec[ptr];\n";
}
void toCPPListener::enterGroupedStmt(bfeParser::GroupedStmtContext *ctx){
printStack.push_back("");
}
void toCPPListener::exitGroupedStmt(bfeParser::GroupedStmtContext *ctx){
std::string s = printStack.back();
printStack.pop_back();
printStack.back()+=(s);
}