Add translation and cxxopts

This commit is contained in:
2020-10-01 22:03:09 +05:30
parent b893608a57
commit 036fefcd1d
8 changed files with 86 additions and 25 deletions

View File

@@ -4,13 +4,14 @@
"name": "Linux", "name": "Linux",
"includePath": [ "includePath": [
"${workspaceFolder}/include", "${workspaceFolder}/include",
"${workspaceFolder}/**", "${workspaceFolder}/lib/antlr4/include",
"${workspaceFolder}/lib/cxxopts/include",
"${workspaceFolder}/lib/generated" "${workspaceFolder}/lib/generated"
], ],
"defines": [], "defines": [],
"compilerPath": "/usr/lib64/ccache/clang", "compilerPath": "/usr/lib64/ccache/clang",
"cStandard": "c11", "cStandard": "c11",
"cppStandard": "c++14", "cppStandard": "c++11",
"intelliSenseMode": "clang-x64", "intelliSenseMode": "clang-x64",
"configurationProvider": "ms-vscode.cmake-tools" "configurationProvider": "ms-vscode.cmake-tools"
} }

19
.vscode/launch.json vendored
View File

@@ -57,6 +57,25 @@
"ignoreFailures": true "ignoreFailures": true
} }
] ]
},
{
"name": "GDB - error.bfe",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main.out",
"args": ["samples/error.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

@@ -70,6 +70,8 @@
"streambuf": "cpp", "streambuf": "cpp",
"thread": "cpp", "thread": "cpp",
"cinttypes": "cpp", "cinttypes": "cpp",
"typeinfo": "cpp" "typeinfo": "cpp",
"regex": "cpp",
"shared_mutex": "cpp"
} }
} }

View File

@@ -27,5 +27,6 @@ target_include_directories(main.out
${PROJECT_SOURCE_DIR}/lib/generated ${PROJECT_SOURCE_DIR}/lib/generated
${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/lib/antlr4/include ${PROJECT_SOURCE_DIR}/lib/antlr4/include
${PROJECT_SOURCE_DIR}/lib/cxxopts/include
) )

View File

@@ -2,6 +2,11 @@
An extension of a language that shall not be named An extension of a language that shall not be named
Requires:
1. cxxopts -> v2.2.1
2. antlr4
*HelloWorld.bfe* *HelloWorld.bfe*
``` ```
-[-7>+<]>-.-[->+5<]>++.+7..+3.[-3>+<]>-5.--[->+4<]>-.-8.+3.-6.-8. -[-7>+<]>-.-[->+5<]>++.+7..+3.[-3>+<]>-5.--[->+4<]>-.-8.+3.-6.-8.

View File

@@ -0,0 +1 @@
+65.<

View File

@@ -38,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"); if(pointer==0) throw std::string("Decrement below zero");
pointer--; pointer--;
return Any(); return Any();

View File

@@ -1,20 +1,37 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string> #include <string>
#include <antlr4-common.h> #include <cxxopts.hpp>
#include "antlr4-common.h"
#include "cxxopts.hpp"
#include "bfeLexer.h" #include "bfeLexer.h"
#include "bfeParser.h" #include "bfeParser.h"
#include "toBFListener.hpp" #include "toBFListener.hpp"
#include "executeBFE.hpp" #include "executeBFE.hpp"
using namespace antlr4; using namespace antlr4;
int main(int argc, const char *argv[]) int main(int argc, char **argv)
{ {
cxxopts::Options options(argv[0], "An interpreter/translator for bfe");
options.add_options()
("t,translate", "Translate to bf", cxxopts::value<bool>()->default_value("false"))
("i,input", "Input(First argument)", cxxopts::value<std::string>())
("h,help", "Print usage");
options.parse_positional({"input"});
auto results = options.parse(argc, argv);
if (results.count("help"))
{
std::cout << options.help() << std::endl;
return 0;
}
if (results.count("input"))
{
// std::cout << results["input"].as<std::string>()<<std::endl;
std::ifstream stream; std::ifstream stream;
bool outFile = false; bool outFile = false;
stream.open(argv[1]); stream.open(results["input"].as<std::string>());
if (stream.fail()) if (stream.fail())
{ {
std::cout << "Could not open" << std::endl; std::cout << "Could not open" << std::endl;
@@ -26,14 +43,28 @@ 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; if(results["translate"].as<bool>()){
// tree::ParseTreeWalker::DEFAULT.walk(&listener, tree); toBFListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
}else{
executeBGE *visitor = new executeBGE(); executeBGE *visitor = new executeBGE();
try{ try
{
visitor->visit(tree); visitor->visit(tree);
std::cout << std::endl; std::cout << std::endl;
}catch(std::string e){
std::cout<<"Illegal:"<<e<<std::endl;
} }
catch (std::string e)
{
std::cout << "\nIllegal:" << e<<std::endl;
}
}
}else{
std::cout<<"?:Expected file"<<std::endl;
return 1;
}
// if(argc>2 && )
return 0; return 0;
} }