#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> tape(30000, 0); // tape with 30000 cells int ptr = 0; // pointer to the current cell (memory address) int code_ptr = 0; // pointer to current code instruction string code; getline(cin, code); while (code_ptr < code.size()) { char instruction = code[code_ptr]; // instructions // https://en.wikipedia.org/wiki/[redacted]Language_design switch (instruction) { case '>': // increment cell pointer ++ptr; break; case '<': // decrement cell pointer --ptr; break; case '+': // increment byte at ptr cell ++tape[ptr]; break; case '-': // decrement byte at ptr cell --tape[ptr]; break; case '.': // print byte at ptr cell cout << (char)tape[ptr]; break; case ',': // accept one byte of input, store it's value at ptr cell tape[ptr] = cin.get(); break; case '[': // start a loop if (tape[ptr] == 0) { int loop_count = 1; while (loop_count > 0) { ++code_ptr; if (code[code_ptr] == '[') ++loop_count; else if (code[code_ptr] == ']') --loop_count; } } break; case ']': // end a loop if (code[code_ptr] != 0) { int loop_count = 1; while (loop_count > 0) { --code_ptr; if (code[code_ptr] == '[') --loop_count; else if (code[code_ptr] == ']') ++loop_count; } } break; default: cout << "\nUnrecognized character: " << code[code_ptr]; break; } ++code_ptr; } return 0; }
>++++++++[<+++++++++>-]<.
if (code[code_ptr] != 0) {
if (tape[ptr] != 0) {
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)