Last active
October 17, 2015 13:18
-
-
Save kristopherjohnson/55092ba62e82c2166125 to your computer and use it in GitHub Desktop.
Simplified Brainfuck interpreter in C++11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2015 Kristopher Johnson | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#include <fstream> | |
#include <iostream> | |
#include <iterator> | |
#include <stdexcept> | |
#include <string> | |
#include <vector> | |
using std::cerr; | |
using std::cin; | |
using std::cout; | |
using std::endl; | |
using std::exception; | |
using std::ifstream; | |
using std::istreambuf_iterator; | |
using std::ios; | |
using std::runtime_error; | |
using std::string; | |
using std::vector; | |
class BrainfuckMachine | |
{ | |
public: | |
explicit BrainfuckMachine(const string& sourceFilePath) | |
{ | |
loadProgramFromFilePath(sourceFilePath); | |
} | |
void executeProgram() | |
{ | |
tapePosition = 0; | |
tape.clear(); | |
ProgramCounter pc = 0; | |
while (pc < program.size()) { | |
auto instruction = program[pc]; | |
++pc; | |
switch (instruction) { | |
case '>': ++tapePosition; | |
break; | |
case '<': --tapePosition; | |
break; | |
case '+': ++markAtTapePosition(); | |
break; | |
case '-': --markAtTapePosition(); | |
break; | |
case '.': cout.put(markAtTapePosition()); | |
break; | |
case ',': readInputCharacter(); | |
break; | |
case '[': if (markAtTapePosition() == 0) pc = matchingCloseBrace(pc); | |
break; | |
case ']': if (markAtTapePosition() != 0) pc = matchingOpenBrace(pc); | |
break; | |
default: // ignore other characters | |
break; | |
} | |
} | |
} | |
private: | |
// The "tape" is a vector of 8-bit marks. | |
// The vector is automatically resized as the tape position increases. | |
// We use a signed type for the index so that we can catch underflow. | |
using TapeMark = char; | |
using Tape = vector<TapeMark>; | |
using TapePosition = int; | |
// The "program" is a vector of 8-bit instructions. | |
// The instructions are '<', '>', '+', '-', '.', ',', '[', and ']' | |
using Instruction = char; | |
using Program = vector<Instruction>; | |
using ProgramCounter = Program::size_type; | |
Tape tape; | |
TapePosition tapePosition = 0; | |
Program program; | |
TapeMark& markAtTapePosition() | |
{ | |
if (tapePosition < 0) | |
throw runtime_error("tape position < 0"); | |
auto pos = static_cast<Tape::size_type>(tapePosition); | |
// Tape extends as needed | |
if (tape.size() <= pos) | |
tape.resize(pos + 128); | |
return tape[pos]; | |
} | |
void loadProgramFromFilePath(const string& sourceFilePath) | |
{ | |
auto sourceFile = ifstream(sourceFilePath, ios::in | ios::binary); | |
if (sourceFile.fail()) | |
throw runtime_error(string("unable to open file at path \"") | |
+ sourceFilePath | |
+ "\""); | |
program.assign(istreambuf_iterator<char>(sourceFile), | |
istreambuf_iterator<char>()); | |
} | |
void readInputCharacter() | |
{ | |
auto input = cin.get(); | |
if (input != EOF) | |
markAtTapePosition() = static_cast<TapeMark>(input); | |
} | |
ProgramCounter matchingCloseBrace(ProgramCounter pc) | |
{ | |
auto nest = 1; | |
while (pc < program.size() && nest > 0) { | |
auto instruction = program[pc]; | |
if (instruction == ']') | |
--nest; | |
else if (instruction == '[') | |
++nest; | |
++pc; | |
} | |
if (nest > 0) | |
throw runtime_error("unmatched ["); | |
return pc; | |
} | |
ProgramCounter matchingOpenBrace(ProgramCounter pc) | |
{ | |
auto nest = 1; | |
--pc; | |
while (pc > 0 && nest > 0) { | |
--pc; | |
auto instruction = program[pc]; | |
if (instruction == '[') | |
--nest; | |
else if (instruction == ']') | |
++nest; | |
} | |
if (nest > 0) | |
throw runtime_error("unmatched ]"); | |
++pc; | |
return pc; | |
} | |
}; | |
int main(int argc, const char * argv[]) | |
{ | |
if (argc < 2) { | |
cerr << "usage: " << argv[0] << " FILENAME ..." << endl; | |
} | |
else for (auto i = 1; i < argc; ++i) { | |
auto sourceFilePath = argv[i]; | |
try { | |
auto machine = BrainfuckMachine(sourceFilePath); | |
machine.executeProgram(); | |
} | |
catch (const exception& ex) { | |
cerr << sourceFilePath << ": error: " << ex.what() << endl; | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CXXFLAGS=--std=c++11 -O2 -Wall | |
all: KJSimpleBrainfuck | |
KJSimpleBrainfuck: KJSimpleBrainfuck.cpp | |
# Should print "OK" | |
test: all | |
./KJSimpleBrainfuck OK.bf | |
clean: | |
- /bin/rm KJSimpleBrainfuck | |
.PHONY: all test clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This simple Brainfuck program writes "OK" and a newline | |
++++[>++++<-]>[<+++++>-]< Put 80 onto tape at position 0 | |
-.----. Write 79 ('O') then 75 ('K') | |
>+++++ +++++. Write newline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simplified version of the KJBrainfuck.cpp interpreter available here: https://gist.github.com/kristopherjohnson/e5fc3d19c251dc561f62