Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- using namespace std;
- #include <iostream>
- #include <string>
- #include <conio.h>
- unsigned char arr[65536] = { 0 }; //main array
- unsigned short ptr = 0; //basic array pointer
- unsigned char cbfr = 0; //copy buffer
- unsigned char stk[256] = { 0 }; //stack
- unsigned char sp = 0; //stack pointer
- int skip(int ip, string command)
- //Input: Integer representing current instruction pointer, string representing a list of commands
- //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
- {
- if (arr[ptr] == 0)
- {
- unsigned int nstLV = 1; //nest level
- while (nstLV > 0)
- {
- ip++;
- //if the instruction pointer passes an opening bracket, it entered a nest level
- if (command[ip] == '[')
- nstLV++;
- //if the instruction pointer passes an opening bracket, it left a nest level
- if (command[ip] == ']')
- nstLV--;
- }
- }
- return ip;
- }
- int repeat(int ip, string command)
- //Input: Integer representing current instruction pointer, string representing a list of commands
- //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
- {
- if (arr[ptr] != 0)
- {
- unsigned int nstLV = 1; //nest level
- while (nstLV > 0)
- {
- ip--; //first of all bring the instruction pointer back
- //if the instruction pointer passes an opening bracket, it left a nest level
- if (command[ip] == '[')
- nstLV--;
- //if the instruction pointer passes an opening bracket, it entered a nest level
- if (command[ip] == ']')
- nstLV++;
- }
- }
- return ip;
- }
- void interpret(string command)
- {
- for (int i = 0; i < command.length(); i++)
- //going over the command
- {
- switch(command[i])
- //yeah there's a lot of commands
- {
- case '+': //increment value
- arr[ptr]++;
- break;
- case '-': //decrement value
- arr[ptr]--;
- break;
- case '>': //increment pointer
- if (ptr < 65536) ptr++;
- break;
- case '<': //decrement pointer
- if (ptr > 0) ptr--;
- break;
- case '[': //start loop or jump forward to matching ']'
- i = skip(i, command);
- break;
- case ']': //end loop or jump back to matching '['
- i = repeat(i, command);
- break;
- case '.': //print value as character
- cout << arr[ptr];
- break;
- case ',': //enter a character without buffer
- arr[ptr] = _getch();
- break;
- case '"': //print value as ascii code
- cout << (int) arr[ptr];
- break;
- case ';': //print value as ascii code
- cin >> arr[ptr];
- break;
- case '(': //push value to stack
- stk[sp++] = arr[ptr];
- break;
- case ')': //pop value from stack
- arr[ptr] = stk[--sp];
- break;
- case '@': //peek top of stack
- arr[ptr] = stk[sp - 1];
- break;
- case '$': //pop value from stack without writing to cell
- sp--;
- break;
- case '{': //shift value left
- arr[ptr] <<= 1;
- break;
- case '}': //shift value right
- arr[ptr] >>= 1;
- break;
- case '/': //store value
- cbfr = arr[ptr];
- break;
- case '\\': //paste value
- arr[ptr] = cbfr;
- break;
- case '=': //add top of stack to value
- arr[ptr] += stk[sp - 1];
- break;
- case '_': //subtract value by top of stack
- arr[ptr] -= stk[sp - 1];
- break;
- case '*': //multiply value by top of stack
- arr[ptr] *= stk[sp - 1];
- break;
- case ':': //divide value by top of stack
- arr[ptr] /= stk[sp - 1];
- break;
- case '%': //modulo value with top of stack
- arr[ptr] %= stk[sp - 1];
- break;
- case '&': //logical and with value and top of stack
- arr[ptr] &= stk[sp - 1];
- break;
- case '|': //logical and with value and top of stack
- arr[ptr] |= stk[sp - 1];
- break;
- case '^': //logical and with value and top of stack
- arr[ptr] ^= stk[sp - 1];
- break;
- default: //just print a letter or a number
- if ((command[i] > 'A' && command[i] < 'Z') || (command[i] > '0' && command[i] < '9')) cout << command[i];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement