Advertisement
Radfler

primitive brainfuck interpreter

Jun 22nd, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class BrainfuckInterpreter {
  8.  
  9. public:
  10.  
  11.     explicit BrainfuckInterpreter(const string& instructions)
  12.         : instructions(instructions), memory(30000) { }
  13.  
  14.     void execute() {
  15.  
  16.         auto the_pointer = memory.data();
  17.  
  18.         for(char instruction : instructions) {
  19.            
  20.             switch(instruction) {
  21.            
  22.             case '>':
  23.                 ++the_pointer;
  24.                 break;
  25.  
  26.             case '<':
  27.                 --the_pointer;
  28.                 break;
  29.  
  30.             case '+':
  31.                 ++*the_pointer;
  32.                 break;
  33.  
  34.             case '-':
  35.                 --*the_pointer;
  36.                 break;
  37.  
  38.             case '.':
  39.                 cout << *the_pointer;
  40.                 break;
  41.  
  42.             case ',':
  43.                 cin >> *the_pointer;
  44.                 break;
  45.  
  46.             }
  47.  
  48.         }
  49.  
  50.     }
  51.  
  52. private:
  53.  
  54.     string instructions;
  55.     vector<unsigned char> memory;
  56.  
  57. };
  58.  
  59. int main() {
  60.  
  61.     string instructions;
  62.     cin >> instructions;
  63.  
  64.     BrainfuckInterpreter interpreter(instructions);
  65.     interpreter.execute();
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement