diff --git a/C++/brainfuckemulator/.vscode/settings.json b/C++/brainfuckemulator/.vscode/settings.json new file mode 100644 index 0000000..def3d8b --- /dev/null +++ b/C++/brainfuckemulator/.vscode/settings.json @@ -0,0 +1,37 @@ +{ + "files.associations": { + "fstream": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "*.tcc": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "type_traits": "cpp", + "tuple": "cpp", + "typeinfo": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/C++/brainfuckemulator/a.out b/C++/brainfuckemulator/a.out new file mode 100755 index 0000000..3c99374 Binary files /dev/null and b/C++/brainfuckemulator/a.out differ diff --git a/C++/brainfuckemulator/fml.bf b/C++/brainfuckemulator/fml.bf new file mode 100644 index 0000000..f767b69 --- /dev/null +++ b/C++/brainfuckemulator/fml.bf @@ -0,0 +1,2 @@ +>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>> +++++++++[<++++>-]<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+. diff --git a/C++/brainfuckemulator/main.cpp b/C++/brainfuckemulator/main.cpp new file mode 100644 index 0000000..4a08e79 --- /dev/null +++ b/C++/brainfuckemulator/main.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +// > Increment the pointer. +// < Decrement the pointer. +// + Increment the byte at the pointer. +// - Decrement the byte at the pointer. +// . Output the byte at the pointer. +// , Input a byte and store it in the byte at the pointer. +// [ Jump forward past the matching ] if the byte at the pointer is zero. +// ] Jump backward to the matching [ unless the byte at the pointer is zero. + +class BrainFuck { +public: + char stack[30000]; + int programCounter = 0; + char* ptr; + std::string program; + + BrainFuck(std::string program) { + this->program = program; + this->ptr = (char*)this->program.c_str(); + } + + bool tick() { + if (programCounter >= 30000) return false; + int bal; + switch((char)program[programCounter]) { + case '>': ++ptr; break; + case '<': --ptr; break; + case '+': ++*ptr; break; + case '-': --*ptr; break; + case '.': programPrint(*ptr); break; + case ',': break; // TODO GETS(); + case '[': + bal = 1; + if (*ptr == '\0') { + do { + programCounter++; + if (program[programCounter] == '[') bal++; + else if (program[programCounter] == ']') bal--; + } while (bal != 0); + } + break; + case ']': + bal = 0; + do { + if (program[programCounter] == '[') bal++; + else if (program[programCounter] == ']') bal--; + programCounter--; + } while (bal != 0); + break; + } + programCounter++; + return true; + } + + void programPrint(char toPrint) { + std::cout << "OUTPUT AT POSITION " << programCounter << ": "; + putchar(toPrint); + putchar('\n'); + } +}; + +int main(int argc, char** argv) { + if (argc < 2) {std::cout << "No input file specified" << std::endl; exit(0); } + + std::ifstream input(*(argv + 1)); + std::string programStream; + + for (std::string line; std::getline(input, line); ) + programStream += line; + + programStream.erase(std::remove(programStream.begin(), programStream.end(), '\n'), programStream.end()); + + BrainFuck CPU(programStream); + + while (1) { + if (!CPU.tick()) { + exit(0); + } + } + + std::cout << programStream << std::endl; +}