7 Commits
0.0.1 ... 1.0.0

Author SHA1 Message Date
75f71259a0 introduce transpile to cpp 2020-10-30 20:35:01 +05:30
e6e225b639 Change README 2020-10-04 14:47:24 +05:30
c55a1745a5 Remove memory allocation bug 2020-10-01 23:19:45 +05:30
a1c9e4894f Rearrange README 2020-10-01 22:13:11 +05:30
6f56891851 Add readme updates 2020-10-01 22:05:24 +05:30
036fefcd1d Add translation and cxxopts 2020-10-01 22:03:09 +05:30
b893608a57 Update README 2020-10-01 20:43:00 +05:30
16 changed files with 395 additions and 42 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"
} }

69
.vscode/launch.json vendored
View File

@@ -10,7 +10,7 @@
"name": "Grammar - simple.bfe", "name": "Grammar - simple.bfe",
"input": "samples/simple.bfe", "input": "samples/simple.bfe",
"visualParseTree": true, "visualParseTree": true,
"grammar": "${workspaceFolder}/grammar/bf.g4" "grammar": "${workspaceFolder}/grammar/bfe.g4"
}, },
{ {
"type": "antlr-debug", "type": "antlr-debug",
@@ -18,7 +18,15 @@
"name": "Grammar - group.bfe", "name": "Grammar - group.bfe",
"input": "samples/group.bfe", "input": "samples/group.bfe",
"visualParseTree": true, "visualParseTree": true,
"grammar": "${workspaceFolder}/grammar/bf.g4" "grammar": "${workspaceFolder}/grammar/bfe.g4"
},
{
"type": "antlr-debug",
"request": "launch",
"name": "Grammar - testmem.bfe",
"input": "samples/testmem.bfe",
"visualParseTree": true,
"grammar": "${workspaceFolder}/grammar/bfe.g4"
}, },
{ {
"name": "GDB - simple.bfe", "name": "GDB - simple.bfe",
@@ -39,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", "name": "GDB - group.bfe",
"type": "cppdbg", "type": "cppdbg",
@@ -57,6 +84,44 @@
"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
}
]
},
{
"name": "GDB - testmem.bfe",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main.out",
"args": ["samples/testmem.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

@@ -10,6 +10,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
set(SOURCES set(SOURCES
src/main.cpp src/main.cpp
src/toBFListener.cpp src/toBFListener.cpp
src/toCPPListener.cpp
src/executeBFE.cpp src/executeBFE.cpp
lib/generated/bfeLexer.cpp lib/generated/bfeLexer.cpp
lib/generated/bfeParser.cpp lib/generated/bfeParser.cpp
@@ -27,5 +28,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

@@ -1,25 +1,76 @@
# bfc # (+97>)3<3+>+5>+2<2(.>)3 (*.bfe)
An extension of a language that shall not be named A slight syntactic superset of BF.
*Hello world* Development requirements:
1. cxxopts -> v2.2.1
2. antlr4
*HelloWorld.bfe*
``` ```
-[-7>+<]>-.-[->+5<]>++.+7..+3.[-3>+<]>-5.--[->+4<]>-.-8.+3.-6.-8. -[-7>+<]>-.-[->+5<]>++.+7..+3.[-3>+<]>-5.--[->+4<]>-.-8.+3.-6.-8.
``` ```
## Setup oddities ## 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 -a bf #Translate file to bf
./bfc prog.bfe -a cpp # Translate file to cpp
```
## Syntax
**Basic**
```bf
,. IO
<> Address manipulation
+- Value manipulation
[] Looping construct
```
**Comments**
`//` - Single Line
`/* */` - Multi Line
**Repeats**
```
<symbol><number>
(<symbols>)<number>
```
Eg.
```
+65. //prints A
>
(+65>)3 //stores AAA
(<.)3 //prints AAA
```
## Setup oddities (dev)
1. Copy antlr4's cpp libs to `./libs/antlr4`. (Please take care that the include folder points straight to the files not to `antlr-runtime`) 1. Copy antlr4's cpp libs to `./libs/antlr4`. (Please take care that the include folder points straight to the files not to `antlr-runtime`)
2. Copy `antlr-4.8-complete.jar` to `./thirdparty/antlr`. 2. Copy `antlr-4.8-complete.jar` to `./thirdparty/antlr`.
3. Generate parsers to `./libs/generated` or use VSCode with the ANTLR extension. 3. Generate parsers to `./libs/generated` or use VSCode with the ANTLR extension.
## Todo (Tentative) ## Todo (Tentative)
- [ ] Grammar - [ ] Grammar
- [X] Number shorthand - [X] Number shorthand
- [X] Loop statements - [X] Loop statements
- [ ] Shorthand segments - [ ] Shorthand segments
- [ ] Processing - [X] Processing
- [X] Translate to bf - [X] Translate to bf
- [ ] Execute result - [X] Execute results

View File

@@ -9,11 +9,11 @@ using namespace antlr4;
using namespace antlrcpp; using namespace antlrcpp;
// using namespace std; // using namespace std;
class executeBGE: public bfeBaseVisitor{ class executeBGE: public bfeBaseVisitor{
private: protected:
std::vector<char> memory; std::vector<char> memory;
int pointer=0; int pointer=0;
public: public:
executeBGE() : bfeBaseVisitor(),memory(100) { executeBGE() : bfeBaseVisitor(),memory(1) {
} }
// Any visitProgram(bfeParser::ProgramContext*) override; // Any visitProgram(bfeParser::ProgramContext*) override;
Any visitNumberedStmt(bfeParser::NumberedStmtContext*) override; Any visitNumberedStmt(bfeParser::NumberedStmtContext*) override;

View File

@@ -22,4 +22,6 @@ class toBFListener : public bfeBaseListener {
void exitLoopStmt(bfeParser::LoopStmtContext *ctx) override; void exitLoopStmt(bfeParser::LoopStmtContext *ctx) override;
void enterGroupedStmt(bfeParser::GroupedStmtContext *ctx) override; void enterGroupedStmt(bfeParser::GroupedStmtContext *ctx) override;
void exitGroupedStmt(bfeParser::GroupedStmtContext *ctx) override; void exitGroupedStmt(bfeParser::GroupedStmtContext *ctx) override;
void enterOutputStmt(bfeParser::OutputStmtContext *ctx) override;
void enterInputStmt(bfeParser::InputStmtContext *ctx) override;
}; };

27
include/toCPPListener.hpp Normal file
View File

@@ -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;
};

1
samples/bfc.bfe Normal file
View File

@@ -0,0 +1 @@
(+97>)3<3+>+5>+2<2(.>)3

View File

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

48
samples/test.bfe Normal file
View File

@@ -0,0 +1,48 @@
>,<>>,>,<[->-
>+<><<]+->>[-<<+>>]
<+-><><<<[-+->-<+-<+>]<
[->+<]><>>>>>><>++->>+->>
>>>+[>><>>>[-]<[-<>-+]<<>[-
+-]-++-+-< [-]<><<>[+
---+]<><< <<+-<<<<>
-+<><<<> <+-<<[->
>>>>>>+ >>>>>-+
>+<<<>< < <><<<<<
<<<<]>[ ->> >>><><>
><>>+>> >>>>+ <<<<<><
<<<<+-< <<<]> [->>>>>
>><>+
>>>>-
+><>>
+<<<<
<<+-<
<<<><<<< -+><] >>-++-+>+-+[<-] <[->>
<>++-<<<]>>>> [-<<< +--+<<<<+>><>>> >>>]>
[-<<-+<<<<<+<>> +->>> >>>]>[-<<<<><<< ><<+>
>>>>>>]>>>>>><>>+ [-<<< <+--+<><+-<<<<< <[->>
+>>+<<<<]+->[->>+>> +-+<< <<]>[-<<+>>]>[- <<+>>
]+[[<>-]-++>[<-]<+-[ +-->< >><>-<<<]>>-+-> >>+>[
<-]<+-[- >+>[- <-]<[ >>[-< <-+->
]-<<[-> ><- >+->- +-<<< <]]<]
>>-<<< < <+>[> <<-]< [>+->
[-+<<- >]<<[ <]]>- +-]>>
>>><>> >><>+ ]-<<< >+-<<
<>+>- ++[<- ]<[-+ >>+[<
<<>-> -+]<< [>>>+ [<-+<
<->>] <<<[< ]]]>- ]>>>>
[-]<< <<<<< -+<>< <<<<<
<[-]> >+>+[ <-]<[ ->>+<
><<<] >+>[< -]<[> >[<<-
>]<<[ <]]>- [+>>> >>>><
++-++ +++++ ++<<< <<<+>
+-[<<> -]<[> >[<<- >]<<[
<]]>-[ ++>[< -]<[- >>-<<
<]>>-> > +>>+> -[<-] <[<<[
->>>+<< ><< ]+->> >>>+> +[<><
-]<><[-> >+<<< ]<<-< ]<<<< +>[<-
]<[>>[<<->]<<[<]]>-] >>+++ +++++[ -> +++++
+<]>[-<<<<+>>>>]>>> >>+>< >-+[<-]<[>> [<<->
]<<[<]]>-[++>[<-] <[->> -<<<]>>-<<< <<<<<
+>+[<-]<[->>+<< <]>>> >>>>>+>[<- ]<[>>
[<<->]<<[<]]> -]<<[ -]<<<<<+> [<-]<
[>>[<<-> ]<<[< ]]>-]<< [.<]

1
samples/testmem.bfe Normal file
View File

@@ -0,0 +1 @@
(+65.>)65536

View File

@@ -20,9 +20,6 @@ Any executeBGE::visitNumberedStmt(bfeParser::NumberedStmtContext *ctx){
} }
Any executeBGE::visitPtrIncr(bfeParser::PtrIncrContext *ctx){ Any executeBGE::visitPtrIncr(bfeParser::PtrIncrContext *ctx){
if(memory.size()<pointer){
memory.push_back(0);
}
memory[pointer]++; memory[pointer]++;
return Any(); return Any();
} }
@@ -33,12 +30,23 @@ Any executeBGE::visitPtrDecr(bfeParser::PtrDecrContext *ctx){
} }
Any executeBGE::visitPtrRight(bfeParser::PtrRightContext *ctx){ Any executeBGE::visitPtrRight(bfeParser::PtrRightContext *ctx){
// too much -> throw this program out
if(memory.max_size()==memory.size()){
throw (std::string("Max Size Reached ")+to_string(memory.capacity()));
}
// its about to be incremented -> need to increase
if(memory.size()==pointer+1){
memory.push_back(0);
}
pointer++; pointer++;
return Any(); return Any();
} }
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,45 @@
#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 "toCPPListener.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()
("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"});
// try
{
auto results = options.parse(argc, argv);
std::string action = results["action"].as<std::string>();
if (action.compare("exec") && action.compare("bf") && action.compare("cpp"))
{
action = std::string( "exec");
}
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 +51,43 @@ 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 (action.compare("bf")==0)
// tree::ParseTreeWalker::DEFAULT.walk(&listener, tree); {
executeBGE* visitor = new executeBGE(); toBFListener listener;
try{ tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
visitor->visit(tree);
std::cout<<std::endl;
}catch(std::string e){
std::cout<<"Illegal:"<<e<<std::endl;
} }
else if (action.compare( "exec")==0)
{
executeBGE *visitor = new executeBGE();
try
{
visitor->visit(tree);
std::cout << std::endl;
}
catch (std::string e)
{
std::cout << "\nIllegal:" << e << std::endl;
}
}
else
{
//else cpp
toCPPListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
}
}
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; return 0;
} }

View File

@@ -29,6 +29,12 @@ void toBFListener::enterPtrRight(bfeParser::PtrRightContext *ctx)
{ {
printStack.back() += (">"); printStack.back() += (">");
} }
void toBFListener::enterOutputStmt(bfeParser::OutputStmtContext *ctx) {
printStack.back() += (".");
}
void toBFListener::enterInputStmt(bfeParser::InputStmtContext *ctx) {
printStack.back() += (",");
}
void toBFListener::enterNumberedStmt(bfeParser::NumberedStmtContext *ctx) void toBFListener::enterNumberedStmt(bfeParser::NumberedStmtContext *ctx)
{ {
@@ -44,9 +50,14 @@ void toBFListener::exitNumberedStmt(bfeParser::NumberedStmtContext *ctx)
printStack.back() += s; printStack.back() += s;
} }
} }
void toBFListener::enterLoopStmt(bfeParser::LoopStmtContext *ctx){ void toBFListener::enterLoopStmt(bfeParser::LoopStmtContext *ctx){
printStack.push_back(""); printStack.push_back("");
} }
void toBFListener::exitLoopStmt(bfeParser::LoopStmtContext *ctx){ void toBFListener::exitLoopStmt(bfeParser::LoopStmtContext *ctx){
std::string s = printStack.back(); std::string s = printStack.back();
printStack.pop_back(); printStack.pop_back();

79
src/toCPPListener.cpp Normal file
View File

@@ -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);
}