Skip to content

Commit

Permalink
introduce transpile to cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvrose committed Oct 30, 2020
1 parent e6e225b commit 75f7125
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 40 deletions.
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@
}
]
},
{
"name": "GDB - simple.bfe - Translate to BF",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main.out",
"args": ["samples/simple.bfe","-a","bf"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "GDB - group.bfe",
"type": "cppdbg",
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
set(SOURCES
src/main.cpp
src/toBFListener.cpp
src/toCPPListener.cpp
src/executeBFE.cpp
lib/generated/bfeLexer.cpp
lib/generated/bfeParser.cpp
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# (+97>)3<3+>+5>+2<2(.>)3 (*.bfe)

An extension of a language that shall not be named
A slight syntactic superset of BF.

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

Expand All @@ -12,12 +12,20 @@ Requires:
-[-7>+<]>-.-[->+5<]>++.+7..+3.[-3>+<]>-5.--[->+4<]>-.-8.+3.-6.-8.
```

## Functions

1. Interpret bfe(and bf) files
2. Convert bf/bfe to bf
3. Convert bf/bfe to C++ (not optimized)


## Usage

```sh
./bfc -h #Help
./bfc prog.bfe #Execute
./bfc prog.bfe -t #Translate file to bf
./bfc prog.bfe -a bf #Translate file to bf
./bfc prog.bfe -a cpp # Translate file to cpp
```

## Syntax
Expand Down
2 changes: 2 additions & 0 deletions include/toBFListener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ class toBFListener : public bfeBaseListener {
void exitLoopStmt(bfeParser::LoopStmtContext *ctx) override;
void enterGroupedStmt(bfeParser::GroupedStmtContext *ctx) override;
void exitGroupedStmt(bfeParser::GroupedStmtContext *ctx) override;
void enterOutputStmt(bfeParser::OutputStmtContext *ctx) override;
void enterInputStmt(bfeParser::InputStmtContext *ctx) override;
};
27 changes: 27 additions & 0 deletions include/toCPPListener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include<string>
#include "bfeLexer.h"
#include "bfeParser.h"
#include "bfeBaseListener.h"


class toCPPListener : public bfeBaseListener {
protected:
std::vector<std::string> printStack;
public:
void enterProgram(bfeParser::ProgramContext *ctx) override;
void exitProgram(bfeParser::ProgramContext *ctx) override ;
void enterPtrIncr(bfeParser::PtrIncrContext *ctx) override;
void enterPtrDecr(bfeParser::PtrDecrContext *ctx) override;
void enterPtrLeft(bfeParser::PtrLeftContext *ctx) override;
void enterPtrRight(bfeParser::PtrRightContext *ctx) override;
void enterNumberedStmt(bfeParser::NumberedStmtContext *ctx) override;
void exitNumberedStmt(bfeParser::NumberedStmtContext *ctx) override;
void enterLoopStmt(bfeParser::LoopStmtContext *ctx) override;
void exitLoopStmt(bfeParser::LoopStmtContext *ctx) override;
void enterGroupedStmt(bfeParser::GroupedStmtContext *ctx) override;
void exitGroupedStmt(bfeParser::GroupedStmtContext *ctx) override;
void enterOutputStmt(bfeParser::OutputStmtContext *ctx) override;
void enterInputStmt(bfeParser::InputStmtContext *ctx) override;
};
48 changes: 48 additions & 0 deletions samples/test.bfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
>,<>>,>,<[->-
>+<><<]+->>[-<<+>>]
<+-><><<<[-+->-<+-<+>]<
[->+<]><>>>>>><>++->>+->>
>>>+[>><>>>[-]<[-<>-+]<<>[-
+-]-++-+-< [-]<><<>[+
---+]<><< <<+-<<<<>
-+<><<<> <+-<<[->
>>>>>>+ >>>>>-+
>+<<<>< < <><<<<<
<<<<]>[ ->> >>><><>
><>>+>> >>>>+ <<<<<><
<<<<+-< <<<]> [->>>>>
>><>+
>>>>-
+><>>
+<<<<
<<+-<
<<<><<<< -+><] >>-++-+>+-+[<-] <[->>
<>++-<<<]>>>> [-<<< +--+<<<<+>><>>> >>>]>
[-<<-+<<<<<+<>> +->>> >>>]>[-<<<<><<< ><<+>
>>>>>>]>>>>>><>>+ [-<<< <+--+<><+-<<<<< <[->>
+>>+<<<<]+->[->>+>> +-+<< <<]>[-<<+>>]>[- <<+>>
]+[[<>-]-++>[<-]<+-[ +-->< >><>-<<<]>>-+-> >>+>[
<-]<+-[- >+>[- <-]<[ >>[-< <-+->
]-<<[-> ><- >+->- +-<<< <]]<]
>>-<<< < <+>[> <<-]< [>+->
[-+<<- >]<<[ <]]>- +-]>>
>>><>> >><>+ ]-<<< >+-<<
<>+>- ++[<- ]<[-+ >>+[<
<<>-> -+]<< [>>>+ [<-+<
<->>] <<<[< ]]]>- ]>>>>
[-]<< <<<<< -+<>< <<<<<
<[-]> >+>+[ <-]<[ ->>+<
><<<] >+>[< -]<[> >[<<-
>]<<[ <]]>- [+>>> >>>><
++-++ +++++ ++<<< <<<+>
+-[<<> -]<[> >[<<- >]<<[
<]]>-[ ++>[< -]<[- >>-<<
<]>>-> > +>>+> -[<-] <[<<[
->>>+<< ><< ]+->> >>>+> +[<><
-]<><[-> >+<<< ]<<-< ]<<<< +>[<-
]<[>>[<<->]<<[<]]>-] >>+++ +++++[ -> +++++
+<]>[-<<<<+>>>>]>>> >>+>< >-+[<-]<[>> [<<->
]<<[<]]>-[++>[<-] <[->> -<<<]>>-<<< <<<<<
+>+[<-]<[->>+<< <]>>> >>>>>+>[<- ]<[>>
[<<->]<<[<]]> -]<<[ -]<<<<<+> [<-]<
[>>[<<-> ]<<[< ]]>-]<< [.<]
97 changes: 60 additions & 37 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "bfeLexer.h"
#include "bfeParser.h"
#include "toBFListener.hpp"
#include "toCPPListener.hpp"
#include "executeBFE.hpp"

using namespace antlr4;
Expand All @@ -16,54 +17,76 @@ 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");
("a,action", "Action (exec,cpp,bf)", cxxopts::value<std::string>()->default_value("exec"))
("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"))
// try
{
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())
auto results = options.parse(argc, argv);
std::string action = results["action"].as<std::string>();
if (action.compare("exec") && action.compare("bf") && action.compare("cpp"))
{
std::cout << "Could not open" << std::endl;
return 1;
action = std::string( "exec");
}
ANTLRInputStream input(stream);
bfeLexer lexer(&input);
CommonTokenStream tokens(&lexer);
bfeParser parser(&tokens);
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;
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
tree::ParseTree *tree = parser.program();
if (action.compare("bf")==0)
{
toBFListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
}
else if (action.compare( "exec")==0)
{
visitor->visit(tree);
std::cout << std::endl;
executeBGE *visitor = new executeBGE();
try
{
visitor->visit(tree);
std::cout << std::endl;
}
catch (std::string e)
{
std::cout << "\nIllegal:" << e << std::endl;
}
}
catch (std::string e)
else
{
std::cout << "\nIllegal:" << e<<std::endl;
//else cpp
toCPPListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
}
}
}else{
std::cout<<"?:Expected file"<<std::endl;
return 1;
else
{
std::cout << "?:Expected file" << std::endl;
return 1;
}
}


// catch (std::exception e)
// {
// std::cout << "E:" << e.what() << "\n";
// return 1;
// }

// if(argc>2 && )
return 0;
Expand Down
11 changes: 11 additions & 0 deletions src/toBFListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ void toBFListener::enterPtrRight(bfeParser::PtrRightContext *ctx)
{
printStack.back() += (">");
}
void toBFListener::enterOutputStmt(bfeParser::OutputStmtContext *ctx) {
printStack.back() += (".");
}
void toBFListener::enterInputStmt(bfeParser::InputStmtContext *ctx) {
printStack.back() += (",");
}

void toBFListener::enterNumberedStmt(bfeParser::NumberedStmtContext *ctx)
{
Expand All @@ -44,9 +50,14 @@ void toBFListener::exitNumberedStmt(bfeParser::NumberedStmtContext *ctx)
printStack.back() += s;
}
}



void toBFListener::enterLoopStmt(bfeParser::LoopStmtContext *ctx){
printStack.push_back("");
}


void toBFListener::exitLoopStmt(bfeParser::LoopStmtContext *ctx){
std::string s = printStack.back();
printStack.pop_back();
Expand Down
79 changes: 79 additions & 0 deletions src/toCPPListener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "bfeParser.h"
#include "bfeBaseListener.h"
#include "toCPPListener.hpp"
// #include "bfBaseVisitor.h"

using namespace antlr4;

void toCPPListener::enterProgram(bfeParser::ProgramContext *ctx)
{
printStack.push_back("#include<iostream>\n#include<vector>\nusing namespace std;\nint main(){\nvector<char> vec(1000,0);int ptr = 0;\n");
}
void toCPPListener::exitProgram(bfeParser::ProgramContext *ctx)
{
std::cout << printStack.front() << "}\n"<< std::endl;
}
void toCPPListener::enterPtrIncr(bfeParser::PtrIncrContext *ctx)
{
// printStack.back() += "+";
printStack.back()+= "vec[ptr]++;\n";
}
void toCPPListener::enterPtrDecr(bfeParser::PtrDecrContext *ctx)
{
// printStack.back() += ("-");
printStack.back()+= "vec[ptr]--;\n";
}
void toCPPListener::enterPtrLeft(bfeParser::PtrLeftContext *ctx)
{
// printStack.back() += ("<");
printStack.back() += ("ptr--;\n");
}
void toCPPListener::enterPtrRight(bfeParser::PtrRightContext *ctx)
{

printStack.back() += ("ptr++;\n");
}

void toCPPListener::enterNumberedStmt(bfeParser::NumberedStmtContext *ctx)
{
printStack.push_back("");
}
void toCPPListener::exitNumberedStmt(bfeParser::NumberedStmtContext *ctx)
{
std::string s = printStack.back();
printStack.pop_back();
int n = stoi(ctx->NUMBER()->getText());
for (int i = 0; i < n; i++)
{
printStack.back() += s;
}
}
void toCPPListener::enterLoopStmt(bfeParser::LoopStmtContext *ctx){
printStack.push_back("");
}
void toCPPListener::exitLoopStmt(bfeParser::LoopStmtContext *ctx){
std::string s = printStack.back();
printStack.pop_back();
printStack.back()+= "while(vec[ptr]){\n"+s+"}\n";
}

void toCPPListener::enterOutputStmt(bfeParser::OutputStmtContext *ctx){
std::string s = printStack.back();
// printStack.pop_back();
printStack.back()+= "cout<<vec[ptr];\n";
}

void toCPPListener::enterInputStmt(bfeParser::InputStmtContext *ctx){
// std::string s = printStack.back();
// printStack.pop_back();
printStack.back()+= "cin>>vec[ptr];\n";
}

void toCPPListener::enterGroupedStmt(bfeParser::GroupedStmtContext *ctx){
printStack.push_back("");
}
void toCPPListener::exitGroupedStmt(bfeParser::GroupedStmtContext *ctx){
std::string s = printStack.back();
printStack.pop_back();
printStack.back()+=(s);
}

0 comments on commit 75f7125

Please sign in to comment.