Introducing execution(beta)

This commit is contained in:
2020-10-01 19:33:47 +05:30
parent 6b37f3987b
commit 2b17338a65
7 changed files with 160 additions and 9 deletions

View File

@@ -4,7 +4,8 @@
"name": "Linux", "name": "Linux",
"includePath": [ "includePath": [
"${workspaceFolder}/include", "${workspaceFolder}/include",
"${workspaceFolder}/**" "${workspaceFolder}/**",
"${workspaceFolder}/lib/generated"
], ],
"defines": [], "defines": [],
"compilerPath": "/usr/lib64/ccache/clang", "compilerPath": "/usr/lib64/ccache/clang",

63
.vscode/settings.json vendored
View File

@@ -9,6 +9,67 @@
}, },
"files.associations": { "files.associations": {
"ostream": "cpp", "ostream": "cpp",
"iostream": "cpp" "iostream": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ranges": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
} }
} }

View File

@@ -74,9 +74,9 @@ LOOPEND:']';
GRPSTART:'('; GRPSTART:'(';
GRPEND:')'; GRPEND:')';
NUMBER: [0-9]+; NUMBER: [0-9]+;
INPUT: '?'; INPUT: ',';
OUTPUT: '.'; OUTPUT: '.';
DEC: '-'; DEC: '-';
INC: '+'; INC: '+';
LEFT: '>'; LEFT: '<';
RIGHT: '<'; RIGHT: '>';

27
include/executeBFE.hpp Normal file
View File

@@ -0,0 +1,27 @@
#include<string>
#include<vector>
#include<antlr4-common.h>
#include "bfeParser.h"
#include "bfeBaseVisitor.h"
using namespace antlr4;
using namespace antlrcpp;
// using namespace std;
class executeBGE: public bfeBaseVisitor{
private:
std::vector<char> memory;
int pointer=0;
public:
executeBGE() : bfeBaseVisitor(),memory(100) {
}
// Any visitProgram(bfeParser::ProgramContext*) override;
Any visitNumberedStmt(bfeParser::NumberedStmtContext*) override;
Any visitPtrIncr(bfeParser::PtrIncrContext *ctx) override;
Any visitPtrDecr(bfeParser::PtrDecrContext *ctx) override;
Any visitPtrRight(bfeParser::PtrRightContext *ctx) override;
Any visitPtrLeft(bfeParser::PtrLeftContext *ctx) override;
Any visitInputStmt(bfeParser::InputStmtContext *ctx) override;
Any visitOutputStmt(bfeParser::OutputStmtContext *ctx) override;
Any visitLoopStmt(bfeParser::LoopStmtContext *ctx) override;
};

View File

@@ -1,2 +1,4 @@
//should print ++++++ //should print AAA
(+2)3 (+65>)3
<<<
(.>)3

57
src/executeBFE.cpp Normal file
View File

@@ -0,0 +1,57 @@
#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){
pointer++;
return Any();
}
Any executeBGE::visitPtrLeft(bfeParser::PtrLeftContext *ctx){
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();
}

View File

@@ -5,6 +5,7 @@
#include "bfeLexer.h" #include "bfeLexer.h"
#include "bfeParser.h" #include "bfeParser.h"
#include "toBFListener.hpp" #include "toBFListener.hpp"
#include "executeBFE.hpp"
using namespace antlr4; using namespace antlr4;
@@ -25,8 +26,10 @@ int main(int argc, const char *argv[])
bfeParser parser(&tokens); bfeParser parser(&tokens);
tree::ParseTree *tree = parser.program(); tree::ParseTree *tree = parser.program();
toBFListener listener; // toBFListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree); // tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
executeBGE* visitor = new executeBGE();
auto resultAny = visitor->visit(tree);
return 0; return 0;
} }