Skip to content

Commit

Permalink
Add translation and cxxopts
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvrose committed Oct 1, 2020
1 parent b893608 commit 036fefc
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 25 deletions.
5 changes: 3 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"name": "Linux",
"includePath": [
"${workspaceFolder}/include",
"${workspaceFolder}/**",
"${workspaceFolder}/lib/antlr4/include",
"${workspaceFolder}/lib/cxxopts/include",
"${workspaceFolder}/lib/generated"
],
"defines": [],
"compilerPath": "/usr/lib64/ccache/clang",
"cStandard": "c11",
"cppStandard": "c++14",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
Expand Down
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@
"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 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"regex": "cpp",
"shared_mutex": "cpp"
}
}
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ target_include_directories(main.out
${PROJECT_SOURCE_DIR}/lib/generated
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/lib/antlr4/include
${PROJECT_SOURCE_DIR}/lib/cxxopts/include

)
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

An extension of a language that shall not be named

Requires:
1. cxxopts -> v2.2.1
2. antlr4


*HelloWorld.bfe*
```
-[-7>+<]>-.-[->+5<]>++.+7..+3.[-3>+<]>-5.--[->+4<]>-.-8.+3.-6.-8.
Expand Down
1 change: 1 addition & 0 deletions samples/error.bfe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+65.<
1 change: 1 addition & 0 deletions src/executeBFE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Any executeBGE::visitPtrRight(bfeParser::PtrRightContext *ctx){
}

Any executeBGE::visitPtrLeft(bfeParser::PtrLeftContext *ctx){

if(pointer==0) throw std::string("Decrement below zero");
pointer--;
return Any();
Expand Down
75 changes: 53 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,70 @@
#include <iostream>
#include <vector>
#include <string>
#include <antlr4-common.h>
#include <cxxopts.hpp>
#include "antlr4-common.h"
#include "cxxopts.hpp"
#include "bfeLexer.h"
#include "bfeParser.h"
#include "toBFListener.hpp"
#include "executeBFE.hpp"


using namespace antlr4;

int main(int argc, const char *argv[])
int main(int argc, char **argv)
{
std::ifstream stream;
bool outFile = false;
stream.open(argv[1]);
if (stream.fail())
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 << "Could not open" << std::endl;
return 1;
std::cout << options.help() << std::endl;
return 0;
}
ANTLRInputStream input(stream);
bfeLexer lexer(&input);
CommonTokenStream tokens(&lexer);
bfeParser parser(&tokens);
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();
// 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;
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;
}



// if(argc>2 && )
return 0;
}

0 comments on commit 036fefc

Please sign in to comment.