10 Commits

Author SHA1 Message Date
9f3af7ac5f Create .gitattributes
mark generated files
2025-07-15 19:42:39 +01:00
4b7b386672 [generated] update 2021-04-13 11:25:48 +05:30
ae57a1bcf2 [grammar] update 2021-04-13 11:25:39 +05:30
88109924ed [add] generated text 2021-04-04 18:24:11 +05:30
f11f844ef5 [update] readme and cxxopts 2021-04-04 18:23:58 +05:30
b38a794465 [switch] to pre-installed libs 2021-04-04 18:22:45 +05:30
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
33 changed files with 4737 additions and 97 deletions

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
lib/generated/* linguist-generated

2
.gitignore vendored
View File

@@ -1,6 +1,6 @@
.antlr/ .antlr/
build/ build/
lib/ # lib/
# Prerequisites # Prerequisites
thirdparty/antlr/*.jar thirdparty/antlr/*.jar
*.d *.d

View File

@@ -1,20 +0,0 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/include",
"${workspaceFolder}/lib/antlr4/include",
"${workspaceFolder}/lib/cxxopts/include",
"${workspaceFolder}/lib/generated"
],
"defines": [],
"compilerPath": "/usr/lib64/ccache/clang",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}

50
.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",
@@ -76,6 +103,25 @@
"ignoreFailures": true "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

@@ -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
@@ -20,13 +21,13 @@ set(SOURCES
) )
add_executable(main.out ${SOURCES}) add_executable(main.out ${SOURCES})
target_link_libraries(main.out ${PROJECT_SOURCE_DIR}/lib/antlr4/lib/libantlr4-runtime.a) target_link_libraries(main.out libantlr4-runtime.so)
# target_link_libraries(main.out ${PROJECT_SOURCE_DIR}/lib/antlr4/lib/libantlr4-runtime.a)
target_include_directories(main.out target_include_directories(main.out
PUBLIC PUBLIC
${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/cxxopts/include ${PROJECT_SOURCE_DIR}/lib/cxxopts/include
/usr/include/antlr4-runtime
) )

View File

@@ -1,10 +1,10 @@
# bfc (*.bfe) # (+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 1. cxxopts -> v2.2.1
2. antlr4 2. antlr4 -> v4
*HelloWorld.bfe* *HelloWorld.bfe*
@@ -12,30 +12,22 @@ Requires:
-[-7>+<]>-.-[->+5<]>++.+7..+3.[-3>+<]>-5.--[->+4<]>-.-8.+3.-6.-8. -[-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 ## Usage
```sh ```sh
./bfc -h #Help ./bfc -h #Help
./bfc prog.bfe #Execute ./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
``` ```
## Setup oddities
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
- [X] Processing
- [X] Translate to bf
- [X] Execute results
## Syntax ## Syntax
**Basic** **Basic**
@@ -64,4 +56,20 @@ Eg.
> >
(+65>)3 //stores AAA (+65>)3 //stores AAA
(<.)3 //prints AAA (<.)3 //prints AAA
``` ```
## Setup oddities (dev)
1. Generate parsers to `./libs/generated` or use VSCode with the ANTLR extension or use a downloaded ANTLR JAR.
## Todo (Tentative)
- [ ] Grammar
- [X] Number shorthand
- [X] Loop statements
- [X] Ignore rest
- [ ] Shorthand segments
- [X] Processing
- [X] Translate to bf
- [X] Execute results

View File

@@ -68,7 +68,6 @@ outputStmt
NEWLINE: '\n' -> skip; NEWLINE: '\n' -> skip;
COMMENT: '//' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN); COMMENT: '//' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN);
ML_COMMENT: '/*' .*? '*/' ->channel(HIDDEN); ML_COMMENT: '/*' .*? '*/' ->channel(HIDDEN);
WS: [ \r\n] -> skip;
DEF: '#'; DEF: '#';
LOOPSTART: '['; LOOPSTART: '[';
LOOPEND:']'; LOOPEND:']';
@@ -81,3 +80,6 @@ DEC: '-';
INC: '+'; INC: '+';
LEFT: '<'; LEFT: '<';
RIGHT: '>'; RIGHT: '>';
EVERYTHING_ELSE: . ->channel(HIDDEN);
WS: [ \r\n] -> skip;

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

File diff suppressed because it is too large Load Diff

29
lib/generated/bfe.tokens generated Normal file
View File

@@ -0,0 +1,29 @@
NEWLINE=1
COMMENT=2
ML_COMMENT=3
DEF=4
LOOPSTART=5
LOOPEND=6
GRPSTART=7
GRPEND=8
NUMBER=9
INPUT=10
OUTPUT=11
DEC=12
INC=13
LEFT=14
RIGHT=15
EVERYTHING_ELSE=16
WS=17
'\n'=1
'#'=4
'['=5
']'=6
'('=7
')'=8
','=10
'.'=11
'-'=12
'+'=13
'<'=14
'>'=15

7
lib/generated/bfeBaseListener.cpp generated Normal file
View File

@@ -0,0 +1,7 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#include "bfeBaseListener.h"

68
lib/generated/bfeBaseListener.h generated Normal file
View File

@@ -0,0 +1,68 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#pragma once
#include "antlr4-runtime.h"
#include "bfeListener.h"
/**
* This class provides an empty implementation of bfeListener,
* which can be extended to create a listener which only needs to handle a subset
* of the available methods.
*/
class bfeBaseListener : public bfeListener {
public:
virtual void enterProgram(bfeParser::ProgramContext * /*ctx*/) override { }
virtual void exitProgram(bfeParser::ProgramContext * /*ctx*/) override { }
virtual void enterStatements(bfeParser::StatementsContext * /*ctx*/) override { }
virtual void exitStatements(bfeParser::StatementsContext * /*ctx*/) override { }
virtual void enterEligibleStmt(bfeParser::EligibleStmtContext * /*ctx*/) override { }
virtual void exitEligibleStmt(bfeParser::EligibleStmtContext * /*ctx*/) override { }
virtual void enterNumberedStmt(bfeParser::NumberedStmtContext * /*ctx*/) override { }
virtual void exitNumberedStmt(bfeParser::NumberedStmtContext * /*ctx*/) override { }
virtual void enterStmt(bfeParser::StmtContext * /*ctx*/) override { }
virtual void exitStmt(bfeParser::StmtContext * /*ctx*/) override { }
virtual void enterGroupedStmt(bfeParser::GroupedStmtContext * /*ctx*/) override { }
virtual void exitGroupedStmt(bfeParser::GroupedStmtContext * /*ctx*/) override { }
virtual void enterLoopStmt(bfeParser::LoopStmtContext * /*ctx*/) override { }
virtual void exitLoopStmt(bfeParser::LoopStmtContext * /*ctx*/) override { }
virtual void enterBasicStmt(bfeParser::BasicStmtContext * /*ctx*/) override { }
virtual void exitBasicStmt(bfeParser::BasicStmtContext * /*ctx*/) override { }
virtual void enterPtrIncr(bfeParser::PtrIncrContext * /*ctx*/) override { }
virtual void exitPtrIncr(bfeParser::PtrIncrContext * /*ctx*/) override { }
virtual void enterPtrDecr(bfeParser::PtrDecrContext * /*ctx*/) override { }
virtual void exitPtrDecr(bfeParser::PtrDecrContext * /*ctx*/) override { }
virtual void enterPtrLeft(bfeParser::PtrLeftContext * /*ctx*/) override { }
virtual void exitPtrLeft(bfeParser::PtrLeftContext * /*ctx*/) override { }
virtual void enterPtrRight(bfeParser::PtrRightContext * /*ctx*/) override { }
virtual void exitPtrRight(bfeParser::PtrRightContext * /*ctx*/) override { }
virtual void enterInputStmt(bfeParser::InputStmtContext * /*ctx*/) override { }
virtual void exitInputStmt(bfeParser::InputStmtContext * /*ctx*/) override { }
virtual void enterOutputStmt(bfeParser::OutputStmtContext * /*ctx*/) override { }
virtual void exitOutputStmt(bfeParser::OutputStmtContext * /*ctx*/) override { }
virtual void enterEveryRule(antlr4::ParserRuleContext * /*ctx*/) override { }
virtual void exitEveryRule(antlr4::ParserRuleContext * /*ctx*/) override { }
virtual void visitTerminal(antlr4::tree::TerminalNode * /*node*/) override { }
virtual void visitErrorNode(antlr4::tree::ErrorNode * /*node*/) override { }
};

7
lib/generated/bfeBaseVisitor.cpp generated Normal file
View File

@@ -0,0 +1,7 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#include "bfeBaseVisitor.h"

76
lib/generated/bfeBaseVisitor.h generated Normal file
View File

@@ -0,0 +1,76 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#pragma once
#include "antlr4-runtime.h"
#include "bfeVisitor.h"
/**
* This class provides an empty implementation of bfeVisitor, which can be
* extended to create a visitor which only needs to handle a subset of the available methods.
*/
class bfeBaseVisitor : public bfeVisitor {
public:
virtual antlrcpp::Any visitProgram(bfeParser::ProgramContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitStatements(bfeParser::StatementsContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitEligibleStmt(bfeParser::EligibleStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitNumberedStmt(bfeParser::NumberedStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitStmt(bfeParser::StmtContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitGroupedStmt(bfeParser::GroupedStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitLoopStmt(bfeParser::LoopStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitBasicStmt(bfeParser::BasicStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitPtrIncr(bfeParser::PtrIncrContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitPtrDecr(bfeParser::PtrDecrContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitPtrLeft(bfeParser::PtrLeftContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitPtrRight(bfeParser::PtrRightContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitInputStmt(bfeParser::InputStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual antlrcpp::Any visitOutputStmt(bfeParser::OutputStmtContext *ctx) override {
return visitChildren(ctx);
}
};

192
lib/generated/bfeLexer.cpp generated Normal file
View File

@@ -0,0 +1,192 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#include "bfeLexer.h"
using namespace antlr4;
bfeLexer::bfeLexer(CharStream *input) : Lexer(input) {
_interpreter = new atn::LexerATNSimulator(this, _atn, _decisionToDFA, _sharedContextCache);
}
bfeLexer::~bfeLexer() {
delete _interpreter;
}
std::string bfeLexer::getGrammarFileName() const {
return "bfe.g4";
}
const std::vector<std::string>& bfeLexer::getRuleNames() const {
return _ruleNames;
}
const std::vector<std::string>& bfeLexer::getChannelNames() const {
return _channelNames;
}
const std::vector<std::string>& bfeLexer::getModeNames() const {
return _modeNames;
}
const std::vector<std::string>& bfeLexer::getTokenNames() const {
return _tokenNames;
}
dfa::Vocabulary& bfeLexer::getVocabulary() const {
return _vocabulary;
}
const std::vector<uint16_t> bfeLexer::getSerializedATN() const {
return _serializedATN;
}
const atn::ATN& bfeLexer::getATN() const {
return _atn;
}
// Static vars and initialization.
std::vector<dfa::DFA> bfeLexer::_decisionToDFA;
atn::PredictionContextCache bfeLexer::_sharedContextCache;
// We own the ATN which in turn owns the ATN states.
atn::ATN bfeLexer::_atn;
std::vector<uint16_t> bfeLexer::_serializedATN;
std::vector<std::string> bfeLexer::_ruleNames = {
u8"NEWLINE", u8"COMMENT", u8"ML_COMMENT", u8"DEF", u8"LOOPSTART", u8"LOOPEND",
u8"GRPSTART", u8"GRPEND", u8"NUMBER", u8"INPUT", u8"OUTPUT", u8"DEC",
u8"INC", u8"LEFT", u8"RIGHT", u8"EVERYTHING_ELSE", u8"WS"
};
std::vector<std::string> bfeLexer::_channelNames = {
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
};
std::vector<std::string> bfeLexer::_modeNames = {
u8"DEFAULT_MODE"
};
std::vector<std::string> bfeLexer::_literalNames = {
"", u8"'\n'", "", "", u8"'#'", u8"'['", u8"']'", u8"'('", u8"')'", "",
u8"','", u8"'.'", u8"'-'", u8"'+'", u8"'<'", u8"'>'"
};
std::vector<std::string> bfeLexer::_symbolicNames = {
"", u8"NEWLINE", u8"COMMENT", u8"ML_COMMENT", u8"DEF", u8"LOOPSTART",
u8"LOOPEND", u8"GRPSTART", u8"GRPEND", u8"NUMBER", u8"INPUT", u8"OUTPUT",
u8"DEC", u8"INC", u8"LEFT", u8"RIGHT", u8"EVERYTHING_ELSE", u8"WS"
};
dfa::Vocabulary bfeLexer::_vocabulary(_literalNames, _symbolicNames);
std::vector<std::string> bfeLexer::_tokenNames;
bfeLexer::Initializer::Initializer() {
// This code could be in a static initializer lambda, but VS doesn't allow access to private class members from there.
for (size_t i = 0; i < _symbolicNames.size(); ++i) {
std::string name = _vocabulary.getLiteralName(i);
if (name.empty()) {
name = _vocabulary.getSymbolicName(i);
}
if (name.empty()) {
_tokenNames.push_back("<INVALID>");
} else {
_tokenNames.push_back(name);
}
}
_serializedATN = {
0x3, 0x608b, 0xa72a, 0x8133, 0xb9ed, 0x417c, 0x3be7, 0x7786, 0x5964,
0x2, 0x13, 0x6b, 0x8, 0x1, 0x4, 0x2, 0x9, 0x2, 0x4, 0x3, 0x9, 0x3, 0x4,
0x4, 0x9, 0x4, 0x4, 0x5, 0x9, 0x5, 0x4, 0x6, 0x9, 0x6, 0x4, 0x7, 0x9,
0x7, 0x4, 0x8, 0x9, 0x8, 0x4, 0x9, 0x9, 0x9, 0x4, 0xa, 0x9, 0xa, 0x4,
0xb, 0x9, 0xb, 0x4, 0xc, 0x9, 0xc, 0x4, 0xd, 0x9, 0xd, 0x4, 0xe, 0x9,
0xe, 0x4, 0xf, 0x9, 0xf, 0x4, 0x10, 0x9, 0x10, 0x4, 0x11, 0x9, 0x11,
0x4, 0x12, 0x9, 0x12, 0x3, 0x2, 0x3, 0x2, 0x3, 0x2, 0x3, 0x2, 0x3, 0x3,
0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x7, 0x3, 0x2e, 0xa, 0x3, 0xc, 0x3, 0xe,
0x3, 0x31, 0xb, 0x3, 0x3, 0x3, 0x5, 0x3, 0x34, 0xa, 0x3, 0x3, 0x3, 0x5,
0x3, 0x37, 0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x4, 0x3, 0x4,
0x3, 0x4, 0x7, 0x4, 0x3f, 0xa, 0x4, 0xc, 0x4, 0xe, 0x4, 0x42, 0xb, 0x4,
0x3, 0x4, 0x3, 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
0x2, 0x2, 0x3, 0x2,
};
atn::ATNDeserializer deserializer;
_atn = deserializer.deserialize(_serializedATN);
size_t count = _atn.getNumberOfDecisions();
_decisionToDFA.reserve(count);
for (size_t i = 0; i < count; i++) {
_decisionToDFA.emplace_back(_atn.getDecisionState(i), i);
}
}
bfeLexer::Initializer bfeLexer::_init;

58
lib/generated/bfeLexer.h generated Normal file
View File

@@ -0,0 +1,58 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#pragma once
#include "antlr4-runtime.h"
class bfeLexer : public antlr4::Lexer {
public:
enum {
NEWLINE = 1, COMMENT = 2, ML_COMMENT = 3, DEF = 4, LOOPSTART = 5, LOOPEND = 6,
GRPSTART = 7, GRPEND = 8, NUMBER = 9, INPUT = 10, OUTPUT = 11, DEC = 12,
INC = 13, LEFT = 14, RIGHT = 15, EVERYTHING_ELSE = 16, WS = 17
};
bfeLexer(antlr4::CharStream *input);
~bfeLexer();
virtual std::string getGrammarFileName() const override;
virtual const std::vector<std::string>& getRuleNames() const override;
virtual const std::vector<std::string>& getChannelNames() const override;
virtual const std::vector<std::string>& getModeNames() const override;
virtual const std::vector<std::string>& getTokenNames() const override; // deprecated, use vocabulary instead
virtual antlr4::dfa::Vocabulary& getVocabulary() const override;
virtual const std::vector<uint16_t> getSerializedATN() const override;
virtual const antlr4::atn::ATN& getATN() const override;
private:
static std::vector<antlr4::dfa::DFA> _decisionToDFA;
static antlr4::atn::PredictionContextCache _sharedContextCache;
static std::vector<std::string> _ruleNames;
static std::vector<std::string> _tokenNames;
static std::vector<std::string> _channelNames;
static std::vector<std::string> _modeNames;
static std::vector<std::string> _literalNames;
static std::vector<std::string> _symbolicNames;
static antlr4::dfa::Vocabulary _vocabulary;
static antlr4::atn::ATN _atn;
static std::vector<uint16_t> _serializedATN;
// Individual action functions triggered by action() above.
// Individual semantic predicate functions triggered by sempred() above.
struct Initializer {
Initializer();
};
static Initializer _init;
};

29
lib/generated/bfeLexer.tokens generated Normal file
View File

@@ -0,0 +1,29 @@
NEWLINE=1
COMMENT=2
ML_COMMENT=3
DEF=4
LOOPSTART=5
LOOPEND=6
GRPSTART=7
GRPEND=8
NUMBER=9
INPUT=10
OUTPUT=11
DEC=12
INC=13
LEFT=14
RIGHT=15
EVERYTHING_ELSE=16
WS=17
'\n'=1
'#'=4
'['=5
']'=6
'('=7
')'=8
','=10
'.'=11
'-'=12
'+'=13
'<'=14
'>'=15

7
lib/generated/bfeListener.cpp generated Normal file
View File

@@ -0,0 +1,7 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#include "bfeListener.h"

61
lib/generated/bfeListener.h generated Normal file
View File

@@ -0,0 +1,61 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#pragma once
#include "antlr4-runtime.h"
#include "bfeParser.h"
/**
* This interface defines an abstract listener for a parse tree produced by bfeParser.
*/
class bfeListener : public antlr4::tree::ParseTreeListener {
public:
virtual void enterProgram(bfeParser::ProgramContext *ctx) = 0;
virtual void exitProgram(bfeParser::ProgramContext *ctx) = 0;
virtual void enterStatements(bfeParser::StatementsContext *ctx) = 0;
virtual void exitStatements(bfeParser::StatementsContext *ctx) = 0;
virtual void enterEligibleStmt(bfeParser::EligibleStmtContext *ctx) = 0;
virtual void exitEligibleStmt(bfeParser::EligibleStmtContext *ctx) = 0;
virtual void enterNumberedStmt(bfeParser::NumberedStmtContext *ctx) = 0;
virtual void exitNumberedStmt(bfeParser::NumberedStmtContext *ctx) = 0;
virtual void enterStmt(bfeParser::StmtContext *ctx) = 0;
virtual void exitStmt(bfeParser::StmtContext *ctx) = 0;
virtual void enterGroupedStmt(bfeParser::GroupedStmtContext *ctx) = 0;
virtual void exitGroupedStmt(bfeParser::GroupedStmtContext *ctx) = 0;
virtual void enterLoopStmt(bfeParser::LoopStmtContext *ctx) = 0;
virtual void exitLoopStmt(bfeParser::LoopStmtContext *ctx) = 0;
virtual void enterBasicStmt(bfeParser::BasicStmtContext *ctx) = 0;
virtual void exitBasicStmt(bfeParser::BasicStmtContext *ctx) = 0;
virtual void enterPtrIncr(bfeParser::PtrIncrContext *ctx) = 0;
virtual void exitPtrIncr(bfeParser::PtrIncrContext *ctx) = 0;
virtual void enterPtrDecr(bfeParser::PtrDecrContext *ctx) = 0;
virtual void exitPtrDecr(bfeParser::PtrDecrContext *ctx) = 0;
virtual void enterPtrLeft(bfeParser::PtrLeftContext *ctx) = 0;
virtual void exitPtrLeft(bfeParser::PtrLeftContext *ctx) = 0;
virtual void enterPtrRight(bfeParser::PtrRightContext *ctx) = 0;
virtual void exitPtrRight(bfeParser::PtrRightContext *ctx) = 0;
virtual void enterInputStmt(bfeParser::InputStmtContext *ctx) = 0;
virtual void exitInputStmt(bfeParser::InputStmtContext *ctx) = 0;
virtual void enterOutputStmt(bfeParser::OutputStmtContext *ctx) = 0;
virtual void exitOutputStmt(bfeParser::OutputStmtContext *ctx) = 0;
};

1131
lib/generated/bfeParser.cpp generated Normal file

File diff suppressed because it is too large Load Diff

296
lib/generated/bfeParser.h generated Normal file
View File

@@ -0,0 +1,296 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#pragma once
#include "antlr4-runtime.h"
class bfeParser : public antlr4::Parser {
public:
enum {
NEWLINE = 1, COMMENT = 2, ML_COMMENT = 3, DEF = 4, LOOPSTART = 5, LOOPEND = 6,
GRPSTART = 7, GRPEND = 8, NUMBER = 9, INPUT = 10, OUTPUT = 11, DEC = 12,
INC = 13, LEFT = 14, RIGHT = 15, EVERYTHING_ELSE = 16, WS = 17
};
enum {
RuleProgram = 0, RuleStatements = 1, RuleEligibleStmt = 2, RuleNumberedStmt = 3,
RuleStmt = 4, RuleGroupedStmt = 5, RuleLoopStmt = 6, RuleBasicStmt = 7,
RulePtrIncr = 8, RulePtrDecr = 9, RulePtrLeft = 10, RulePtrRight = 11,
RuleInputStmt = 12, RuleOutputStmt = 13
};
bfeParser(antlr4::TokenStream *input);
~bfeParser();
virtual std::string getGrammarFileName() const override;
virtual const antlr4::atn::ATN& getATN() const override { return _atn; };
virtual const std::vector<std::string>& getTokenNames() const override { return _tokenNames; }; // deprecated: use vocabulary instead.
virtual const std::vector<std::string>& getRuleNames() const override;
virtual antlr4::dfa::Vocabulary& getVocabulary() const override;
class ProgramContext;
class StatementsContext;
class EligibleStmtContext;
class NumberedStmtContext;
class StmtContext;
class GroupedStmtContext;
class LoopStmtContext;
class BasicStmtContext;
class PtrIncrContext;
class PtrDecrContext;
class PtrLeftContext;
class PtrRightContext;
class InputStmtContext;
class OutputStmtContext;
class ProgramContext : public antlr4::ParserRuleContext {
public:
ProgramContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *EOF();
StatementsContext *statements();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
ProgramContext* program();
class StatementsContext : public antlr4::ParserRuleContext {
public:
StatementsContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<EligibleStmtContext *> eligibleStmt();
EligibleStmtContext* eligibleStmt(size_t i);
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
StatementsContext* statements();
class EligibleStmtContext : public antlr4::ParserRuleContext {
public:
EligibleStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
StmtContext *stmt();
NumberedStmtContext *numberedStmt();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
EligibleStmtContext* eligibleStmt();
class NumberedStmtContext : public antlr4::ParserRuleContext {
public:
NumberedStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
StmtContext *stmt();
antlr4::tree::TerminalNode *NUMBER();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
NumberedStmtContext* numberedStmt();
class StmtContext : public antlr4::ParserRuleContext {
public:
StmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
BasicStmtContext *basicStmt();
GroupedStmtContext *groupedStmt();
LoopStmtContext *loopStmt();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
StmtContext* stmt();
class GroupedStmtContext : public antlr4::ParserRuleContext {
public:
GroupedStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *GRPSTART();
StatementsContext *statements();
antlr4::tree::TerminalNode *GRPEND();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
GroupedStmtContext* groupedStmt();
class LoopStmtContext : public antlr4::ParserRuleContext {
public:
LoopStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *LOOPSTART();
StatementsContext *statements();
antlr4::tree::TerminalNode *LOOPEND();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
LoopStmtContext* loopStmt();
class BasicStmtContext : public antlr4::ParserRuleContext {
public:
BasicStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
PtrIncrContext *ptrIncr();
PtrDecrContext *ptrDecr();
PtrLeftContext *ptrLeft();
PtrRightContext *ptrRight();
InputStmtContext *inputStmt();
OutputStmtContext *outputStmt();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
BasicStmtContext* basicStmt();
class PtrIncrContext : public antlr4::ParserRuleContext {
public:
PtrIncrContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *INC();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
PtrIncrContext* ptrIncr();
class PtrDecrContext : public antlr4::ParserRuleContext {
public:
PtrDecrContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *DEC();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
PtrDecrContext* ptrDecr();
class PtrLeftContext : public antlr4::ParserRuleContext {
public:
PtrLeftContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *LEFT();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
PtrLeftContext* ptrLeft();
class PtrRightContext : public antlr4::ParserRuleContext {
public:
PtrRightContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *RIGHT();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
PtrRightContext* ptrRight();
class InputStmtContext : public antlr4::ParserRuleContext {
public:
InputStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *INPUT();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
InputStmtContext* inputStmt();
class OutputStmtContext : public antlr4::ParserRuleContext {
public:
OutputStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *OUTPUT();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
OutputStmtContext* outputStmt();
private:
static std::vector<antlr4::dfa::DFA> _decisionToDFA;
static antlr4::atn::PredictionContextCache _sharedContextCache;
static std::vector<std::string> _ruleNames;
static std::vector<std::string> _tokenNames;
static std::vector<std::string> _literalNames;
static std::vector<std::string> _symbolicNames;
static antlr4::dfa::Vocabulary _vocabulary;
static antlr4::atn::ATN _atn;
static std::vector<uint16_t> _serializedATN;
struct Initializer {
Initializer();
};
static Initializer _init;
};

7
lib/generated/bfeVisitor.cpp generated Normal file
View File

@@ -0,0 +1,7 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#include "bfeVisitor.h"

52
lib/generated/bfeVisitor.h generated Normal file
View File

@@ -0,0 +1,52 @@
// Generated from /home/atreya/Documents/Projects/Miscellaneous/bfc/grammar/bfe.g4 by ANTLR 4.8
#pragma once
#include "antlr4-runtime.h"
#include "bfeParser.h"
/**
* This class defines an abstract visitor for a parse tree
* produced by bfeParser.
*/
class bfeVisitor : public antlr4::tree::AbstractParseTreeVisitor {
public:
/**
* Visit parse trees produced by bfeParser.
*/
virtual antlrcpp::Any visitProgram(bfeParser::ProgramContext *context) = 0;
virtual antlrcpp::Any visitStatements(bfeParser::StatementsContext *context) = 0;
virtual antlrcpp::Any visitEligibleStmt(bfeParser::EligibleStmtContext *context) = 0;
virtual antlrcpp::Any visitNumberedStmt(bfeParser::NumberedStmtContext *context) = 0;
virtual antlrcpp::Any visitStmt(bfeParser::StmtContext *context) = 0;
virtual antlrcpp::Any visitGroupedStmt(bfeParser::GroupedStmtContext *context) = 0;
virtual antlrcpp::Any visitLoopStmt(bfeParser::LoopStmtContext *context) = 0;
virtual antlrcpp::Any visitBasicStmt(bfeParser::BasicStmtContext *context) = 0;
virtual antlrcpp::Any visitPtrIncr(bfeParser::PtrIncrContext *context) = 0;
virtual antlrcpp::Any visitPtrDecr(bfeParser::PtrDecrContext *context) = 0;
virtual antlrcpp::Any visitPtrLeft(bfeParser::PtrLeftContext *context) = 0;
virtual antlrcpp::Any visitPtrRight(bfeParser::PtrRightContext *context) = 0;
virtual antlrcpp::Any visitInputStmt(bfeParser::InputStmtContext *context) = 0;
virtual antlrcpp::Any visitOutputStmt(bfeParser::OutputStmtContext *context) = 0;
};

1
samples/bfc.bfe Normal file
View File

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

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,13 +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

@@ -7,6 +7,7 @@
#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;
@@ -16,54 +17,76 @@ int main(int argc, char **argv)
cxxopts::Options options(argv[0], "An interpreter/translator for bfe"); cxxopts::Options options(argv[0], "An interpreter/translator for bfe");
options.add_options() options.add_options()
("t,translate", "Translate to bf", cxxopts::value<bool>()->default_value("false")) ("a,action", "Action (exec,cpp,bf)", cxxopts::value<std::string>()->default_value("exec"))
("i,input", "Input(First argument)", cxxopts::value<std::string>()) ("i,input", "Input (First argument)", cxxopts::value<std::string>())
("h,help", "Print usage"); ("h,help", "Print usage");
options.parse_positional({"input"}); options.parse_positional({"input"});
auto results = options.parse(argc, argv); // try
if (results.count("help"))
{ {
std::cout << options.help() << std::endl; auto results = options.parse(argc, argv);
return 0; std::string action = results["action"].as<std::string>();
} if (action.compare("exec") && action.compare("bf") && action.compare("cpp"))
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; 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;
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 (action.compare("bf")==0)
{
toBFListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
}
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; 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;
} }
// catch (std::exception e)
// {
// std::cout << "E:" << e.what() << "\n";
// return 1;
// }
// if(argc>2 && ) // 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);
}

View File