Remove memory allocation bug

This commit is contained in:
2020-10-01 23:19:45 +05:30
parent a1c9e4894f
commit c55a1745a5
4 changed files with 43 additions and 8 deletions

31
.vscode/launch.json vendored
View File

@@ -10,7 +10,7 @@
"name": "Grammar - simple.bfe", "name": "Grammar - simple.bfe",
"input": "samples/simple.bfe", "input": "samples/simple.bfe",
"visualParseTree": true, "visualParseTree": true,
"grammar": "${workspaceFolder}/grammar/bf.g4" "grammar": "${workspaceFolder}/grammar/bfe.g4"
}, },
{ {
"type": "antlr-debug", "type": "antlr-debug",
@@ -18,7 +18,15 @@
"name": "Grammar - group.bfe", "name": "Grammar - group.bfe",
"input": "samples/group.bfe", "input": "samples/group.bfe",
"visualParseTree": true, "visualParseTree": true,
"grammar": "${workspaceFolder}/grammar/bf.g4" "grammar": "${workspaceFolder}/grammar/bfe.g4"
},
{
"type": "antlr-debug",
"request": "launch",
"name": "Grammar - testmem.bfe",
"input": "samples/testmem.bfe",
"visualParseTree": true,
"grammar": "${workspaceFolder}/grammar/bfe.g4"
}, },
{ {
"name": "GDB - simple.bfe", "name": "GDB - simple.bfe",
@@ -76,6 +84,25 @@
"ignoreFailures": true "ignoreFailures": true
} }
] ]
},
{
"name": "GDB - testmem.bfe",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main.out",
"args": ["samples/testmem.bfe"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
} }
] ]
} }

View File

@@ -9,11 +9,11 @@ using namespace antlr4;
using namespace antlrcpp; using namespace antlrcpp;
// using namespace std; // using namespace std;
class executeBGE: public bfeBaseVisitor{ class executeBGE: public bfeBaseVisitor{
private: protected:
std::vector<char> memory; std::vector<char> memory;
int pointer=0; int pointer=0;
public: public:
executeBGE() : bfeBaseVisitor(),memory(100) { executeBGE() : bfeBaseVisitor(),memory(1) {
} }
// Any visitProgram(bfeParser::ProgramContext*) override; // Any visitProgram(bfeParser::ProgramContext*) override;
Any visitNumberedStmt(bfeParser::NumberedStmtContext*) override; Any visitNumberedStmt(bfeParser::NumberedStmtContext*) override;

1
samples/testmem.bfe Normal file
View File

@@ -0,0 +1 @@
(+65.>)65536

View File

@@ -20,9 +20,6 @@ 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();
} }
@@ -33,13 +30,23 @@ Any executeBGE::visitPtrDecr(bfeParser::PtrDecrContext *ctx){
} }
Any executeBGE::visitPtrRight(bfeParser::PtrRightContext *ctx){ 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++; pointer++;
return Any(); return Any();
} }
Any executeBGE::visitPtrLeft(bfeParser::PtrLeftContext *ctx){ Any executeBGE::visitPtrLeft(bfeParser::PtrLeftContext *ctx){
if(pointer==0) throw std::string("Decrement below zero"); if(pointer==0){
throw std::string("Decrement below zero");
}
pointer--; pointer--;
return Any(); return Any();
} }