Add translation and cxxopts
This commit is contained in:
5
.vscode/c_cpp_properties.json
vendored
5
.vscode/c_cpp_properties.json
vendored
@@ -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
19
.vscode/launch.json
vendored
@@ -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
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -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
|
||||||
|
|
||||||
)
|
)
|
||||||
|
@@ -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.
|
||||||
|
@@ -0,0 +1 @@
|
|||||||
|
+65.<
|
@@ -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();
|
||||||
|
75
src/main.cpp
75
src/main.cpp
@@ -1,39 +1,70 @@
|
|||||||
#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)
|
||||||
{
|
{
|
||||||
std::ifstream stream;
|
cxxopts::Options options(argv[0], "An interpreter/translator for bfe");
|
||||||
bool outFile = false;
|
|
||||||
stream.open(argv[1]);
|
options.add_options()
|
||||||
if (stream.fail())
|
("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 << "Could not open" << std::endl;
|
std::cout << options.help() << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (results.count("input"))
|
||||||
|
{
|
||||||
|
// std::cout << results["input"].as<std::string>()<<std::endl;
|
||||||
|
std::ifstream stream;
|
||||||
|
bool outFile = false;
|
||||||
|
stream.open(results["input"].as<std::string>());
|
||||||
|
if (stream.fail())
|
||||||
|
{
|
||||||
|
std::cout << "Could not open" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
ANTLRInputStream input(stream);
|
||||||
|
bfeLexer lexer(&input);
|
||||||
|
CommonTokenStream tokens(&lexer);
|
||||||
|
bfeParser parser(&tokens);
|
||||||
|
|
||||||
|
tree::ParseTree *tree = parser.program();
|
||||||
|
if(results["translate"].as<bool>()){
|
||||||
|
toBFListener listener;
|
||||||
|
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
|
||||||
|
}else{
|
||||||
|
executeBGE *visitor = new executeBGE();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
visitor->visit(tree);
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
catch (std::string e)
|
||||||
|
{
|
||||||
|
std::cout << "\nIllegal:" << e<<std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
std::cout<<"?:Expected file"<<std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
ANTLRInputStream input(stream);
|
|
||||||
bfeLexer lexer(&input);
|
|
||||||
CommonTokenStream tokens(&lexer);
|
|
||||||
bfeParser parser(&tokens);
|
|
||||||
|
|
||||||
tree::ParseTree *tree = parser.program();
|
// if(argc>2 && )
|
||||||
// toBFListener listener;
|
|
||||||
// tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
|
|
||||||
executeBGE* visitor = new executeBGE();
|
|
||||||
try{
|
|
||||||
visitor->visit(tree);
|
|
||||||
std::cout<<std::endl;
|
|
||||||
}catch(std::string e){
|
|
||||||
std::cout<<"Illegal:"<<e<<std::endl;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Reference in New Issue
Block a user