Prevent pointer going below zero

This commit is contained in:
2020-10-01 19:42:23 +05:30
parent 2b17338a65
commit 278da25c0c
2 changed files with 9 additions and 1 deletions

View File

@@ -20,6 +20,9 @@ Any executeBGE::visitNumberedStmt(bfeParser::NumberedStmtContext *ctx){
} }
Any executeBGE::visitPtrIncr(bfeParser::PtrIncrContext *ctx){ Any executeBGE::visitPtrIncr(bfeParser::PtrIncrContext *ctx){
if(memory.size()<pointer){
memory.push_back(0);
}
memory[pointer]++; memory[pointer]++;
return Any(); return Any();
} }
@@ -35,6 +38,7 @@ Any executeBGE::visitPtrRight(bfeParser::PtrRightContext *ctx){
} }
Any executeBGE::visitPtrLeft(bfeParser::PtrLeftContext *ctx){ Any executeBGE::visitPtrLeft(bfeParser::PtrLeftContext *ctx){
if(pointer==0) throw std::string("Decrement below zero");
pointer--; pointer--;
return Any(); return Any();
} }

View File

@@ -30,6 +30,10 @@ int main(int argc, const char *argv[])
// tree::ParseTreeWalker::DEFAULT.walk(&listener, tree); // tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
executeBGE* visitor = new executeBGE(); executeBGE* visitor = new executeBGE();
auto resultAny = visitor->visit(tree); try{
visitor->visit(tree);
}catch(std::string e){
std::cout<<"Illegal:"<<e;
}
return 0; return 0;
} }