Brainfuck emulator
This commit is contained in:
37
C++/brainfuckemulator/.vscode/settings.json
vendored
Normal file
37
C++/brainfuckemulator/.vscode/settings.json
vendored
Normal file
@@ -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"
|
||||
}
|
||||
}
|
||||
BIN
C++/brainfuckemulator/a.out
Executable file
BIN
C++/brainfuckemulator/a.out
Executable file
Binary file not shown.
2
C++/brainfuckemulator/fml.bf
Normal file
2
C++/brainfuckemulator/fml.bf
Normal file
@@ -0,0 +1,2 @@
|
||||
>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>
|
||||
++++++++[<++++>-]<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.
|
||||
87
C++/brainfuckemulator/main.cpp
Normal file
87
C++/brainfuckemulator/main.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
// > 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;
|
||||
}
|
||||
Reference in New Issue
Block a user