Advertisement
MightyD33r

LoadedFuck

Apr 8th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #pragma once
  2.  
  3. using namespace std;
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <conio.h>
  8.  
  9. unsigned char arr[65536] = { 0 }; //main array
  10. unsigned short ptr = 0; //basic array pointer
  11.  
  12. unsigned char cbfr = 0; //copy buffer
  13.  
  14. unsigned char stk[256] = { 0 }; //stack
  15. unsigned char sp = 0; //stack pointer
  16.  
  17. int skip(int ip, string command)
  18. //Input: Integer representing current instruction pointer, string representing a list of commands
  19. //Output: If arr[p] is zero, trace command to matching closing bracket and return i as the new instruction pointer, otherwise return the input instruction pointer
  20. {
  21.     if (arr[ptr] == 0)
  22.     {
  23.         unsigned int nstLV = 1; //nest level
  24.         while (nstLV > 0)
  25.         {
  26.             ip++;
  27.             //if the instruction pointer passes an opening bracket, it entered a nest level
  28.             if (command[ip] == '[')
  29.                 nstLV++;
  30.             //if the instruction pointer passes an opening bracket, it left a nest level
  31.             if (command[ip] == ']')
  32.                 nstLV--;
  33.         }
  34.     }
  35.     return ip;
  36. }
  37.  
  38. int repeat(int ip, string command)
  39. //Input: Integer representing current instruction pointer, string representing a list of commands
  40. //Output: If arr[p] is zero, trace command to matching closing bracket and return i as the new instruction pointer, otherwise return the input instruction pointer
  41. {
  42.     if (arr[ptr] != 0)
  43.     {
  44.         unsigned int nstLV = 1; //nest level
  45.         while (nstLV > 0)
  46.         {
  47.             ip--; //first of all bring the instruction pointer back
  48.  
  49.             //if the instruction pointer passes an opening bracket, it left a nest level
  50.             if (command[ip] == '[')
  51.                 nstLV--;
  52.             //if the instruction pointer passes an opening bracket, it entered a nest level
  53.             if (command[ip] == ']')
  54.                 nstLV++;
  55.         }
  56.     }
  57.     return ip;
  58. }
  59.  
  60. void interpret(string command)
  61. {
  62.     for (int i = 0; i < command.length(); i++)
  63.     //going over the command
  64.     {
  65.         switch(command[i])
  66.         //yeah there's a lot of commands
  67.         {
  68.             case '+': //increment value
  69.                 arr[ptr]++;
  70.                 break;
  71.             case '-': //decrement value
  72.                 arr[ptr]--;
  73.                 break;
  74.             case '>': //increment pointer
  75.                 if (ptr < 65536) ptr++;
  76.                 break;
  77.             case '<': //decrement pointer
  78.                 if (ptr > 0) ptr--;
  79.                 break;
  80.             case '[': //start loop or jump forward to matching ']'
  81.                 i = skip(i, command);
  82.                 break;
  83.             case ']': //end loop or jump back to matching '['
  84.                 i = repeat(i, command);
  85.                 break;
  86.             case '.': //print value as character
  87.                 cout << arr[ptr];
  88.                 break;
  89.             case ',': //enter a character without buffer
  90.                 arr[ptr] = _getch();
  91.                 break;
  92.             case '"': //print value as ascii code
  93.                 cout << (int) arr[ptr];
  94.                 break;
  95.             case ';': //print value as ascii code
  96.                 cin >> arr[ptr];
  97.                 break;
  98.             case '(': //push value to stack
  99.                 stk[sp++] = arr[ptr];
  100.                 break;
  101.             case ')': //pop value from stack
  102.                 arr[ptr] = stk[--sp];
  103.                 break;
  104.             case '@': //peek top of stack
  105.                 arr[ptr] = stk[sp - 1];
  106.                 break;
  107.             case '$': //pop value from stack without writing to cell
  108.                 sp--;
  109.                 break;
  110.             case '{': //shift value left
  111.                 arr[ptr] <<= 1;
  112.                 break;
  113.             case '}': //shift value right
  114.                 arr[ptr] >>= 1;
  115.                 break;
  116.             case '/': //store value
  117.                 cbfr = arr[ptr];
  118.                 break;
  119.             case '\\': //paste value
  120.                 arr[ptr] = cbfr;
  121.                 break;
  122.             case '=': //add top of stack to value
  123.                 arr[ptr] += stk[sp - 1];
  124.                 break;
  125.             case '_': //subtract value by top of stack
  126.                 arr[ptr] -= stk[sp - 1];
  127.                 break;
  128.             case '*': //multiply value by top of stack
  129.                 arr[ptr] *= stk[sp - 1];
  130.                 break;
  131.             case ':': //divide value by top of stack
  132.                 arr[ptr] /= stk[sp - 1];
  133.                 break;
  134.             case '%': //modulo value with top of stack
  135.                 arr[ptr] %= stk[sp - 1];
  136.                 break;
  137.             case '&': //logical and with value and top of stack
  138.                 arr[ptr] &= stk[sp - 1];
  139.                 break;
  140.             case '|': //logical and with value and top of stack
  141.                 arr[ptr] |= stk[sp - 1];
  142.                 break;
  143.             case '^': //logical and with value and top of stack
  144.                 arr[ptr] ^= stk[sp - 1];
  145.                 break;
  146.             default: //just print a letter or a number
  147.                 if ((command[i] > 'A' && command[i] < 'Z') || (command[i] > '0' && command[i] < '9')) cout << command[i];
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement