5 Commits
0.0.1 ... 0.1.1

Author SHA1 Message Date
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
10 changed files with 173 additions and 39 deletions

View File

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

50
.vscode/launch.json vendored
View File

@@ -10,7 +10,7 @@
"name": "Grammar - simple.bfe",
"input": "samples/simple.bfe",
"visualParseTree": true,
"grammar": "${workspaceFolder}/grammar/bf.g4"
"grammar": "${workspaceFolder}/grammar/bfe.g4"
},
{
"type": "antlr-debug",
@@ -18,7 +18,15 @@
"name": "Grammar - group.bfe",
"input": "samples/group.bfe",
"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",
@@ -57,6 +65,44 @@
"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",
"thread": "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}/include
${PROJECT_SOURCE_DIR}/lib/antlr4/include
${PROJECT_SOURCE_DIR}/lib/cxxopts/include
)

View File

@@ -1,25 +1,68 @@
# bfc
# bfc (*.bfe)
An extension of a language that shall not be named
*Hello world*
Requires:
1. cxxopts -> v2.2.1
2. antlr4
*HelloWorld.bfe*
```
-[-7>+<]>-.-[->+5<]>++.+7..+3.[-3>+<]>-5.--[->+4<]>-.-8.+3.-6.-8.
```
## Setup oddities
## Usage
```sh
./bfc -h #Help
./bfc prog.bfe #Execute
./bfc prog.bfe -t #Translate file to bf
```
## 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`)
2. Copy `antlr-4.8-complete.jar` to `./thirdparty/antlr`.
3. Generate parsers to `./libs/generated` or use VSCode with the ANTLR extension.
## Todo (Tentative)
- [ ] Grammar
- [X] Number shorthand
- [X] Loop statements
- [ ] Shorthand segments
- [ ] Processing
- [X] Processing
- [X] Translate to bf
- [ ] Execute result
- [X] Execute results

View File

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

View File

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

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){
if(memory.size()<pointer){
memory.push_back(0);
}
memory[pointer]++;
return Any();
}
@@ -33,12 +30,23 @@ Any executeBGE::visitPtrDecr(bfeParser::PtrDecrContext *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++;
return Any();
}
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--;
return Any();
}

View File

@@ -1,20 +1,37 @@
#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)
{
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;
bool outFile = false;
stream.open(argv[1]);
stream.open(results["input"].as<std::string>());
if (stream.fail())
{
std::cout << "Could not open" << std::endl;
@@ -26,14 +43,28 @@ int main(int argc, const char *argv[])
bfeParser parser(&tokens);
tree::ParseTree *tree = parser.program();
// toBFListener listener;
// tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
executeBGE* visitor = new executeBGE();
try{
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<<"Illegal:"<<e<<std::endl;
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;
}