-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecuteBFE.cpp
69 lines (58 loc) · 1.59 KB
/
executeBFE.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
#include<iostream>
#include<vector>
#include<string>
#include <antlr4-common.h>
#include "bfeParser.h"
#include "bfeBaseVisitor.h"
#include "executeBFE.hpp"
using namespace antlr4;
using namespace antlrcpp;
using namespace std::__cxx11;
Any executeBGE::visitNumberedStmt(bfeParser::NumberedStmtContext *ctx){
int num = stoi(ctx->NUMBER()->getText());
for(int i=0;i<num;i++){
ctx->stmt()->accept(this);
}
return Any();
}
Any executeBGE::visitPtrIncr(bfeParser::PtrIncrContext *ctx){
memory[pointer]++;
return Any();
}
Any executeBGE::visitPtrDecr(bfeParser::PtrDecrContext *ctx){
memory[pointer]--;
return Any();
}
Any executeBGE::visitPtrRight(bfeParser::PtrRightContext *ctx){
// too much -> throw this program out
if(memory.max_size()==memory.size()){
throw (std::string("Max Size Reached ")+to_string(memory.capacity()));
}
// its about to be incremented -> need to increase
if(memory.size()==pointer+1){
memory.push_back(0);
}
pointer++;
return Any();
}
Any executeBGE::visitPtrLeft(bfeParser::PtrLeftContext *ctx){
if(pointer==0){
throw std::string("Decrement below zero");
}
pointer--;
return Any();
}
Any executeBGE::visitInputStmt(bfeParser::InputStmtContext *ctx){
std::cin>>memory[pointer];
return Any();
}
Any executeBGE::visitOutputStmt(bfeParser::OutputStmtContext *ctx){
std::cout<<memory[pointer];
return Any();
}
Any executeBGE::visitLoopStmt(bfeParser::LoopStmtContext *ctx){
while(memory[pointer]){
ctx->statements()->accept(this);
}
return Any();
}