1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <iostream>
#include <bitset>
#include <stack>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#define IS_NUM(x) (x>=48 && x<=57)
using namespace std;
bool ComeAcross(char c, stack<int>& s, vector<string>& grid, bool& StringMode, bool& Trampoline, bitset<2>& Direction)
{
if(!StringMode)
{
int a=s.top();
// Program end
if (c == '@') return false;
// String mode
else if (c == '\"') StringMode = true;
// Input
else if (c == '&'); // INPUT A NUMBER
else if (c == '~'); // INPUT A CHARACTER (ASCII value)
// Direction changers
else if (c == '>') Direction.reset();
else if (c == '^') Direction.set(0),Direction.reset(1);
else if (c == '<') Direction.set(1),Direction.reset(0);
else if (c == 'v') Direction.set();
else if (c == '?')
{ Direction.reset();
if(rand()%100 < 49) Direction.set(0);
if(rand()%100 < 49) Direction.set(1);}
else if (c == '#') Trampoline = true;
// Value pushing
else if (IS_NUM(c)) s.push(c - 48);
else if (s.size()>0)
{
// Basic stack editing
if (c == ':') {s.push(a);}
else if (c == '$') {s.pop();}
// Printing
else if (c == '.') {cout << int(a);s.pop();}
else if (c == ',') {cout << char(a);s.pop();}
// If's
else if (c == '|') {s.pop();Direction.set(0,1);Direction.set(1,a == 0);}
else if (c == '_') {s.pop();Direction.set(0,0);Direction.set(1,a == 0);}
else if (s.size()>1)
{ // Basic arithmetic
if (c == '+') {s.pop();a+=s.top();s.pop();s.push(a);}
else if (c == '-') {s.pop();a-=s.top();s.pop();s.push(a);}
else if (c == '*') {s.pop();a*=s.top();s.pop();s.push(a);}
else if (c == '/') {s.pop();a/=s.top();s.pop();s.push(a);}
else if (c == '%') {s.pop();a%=s.top();s.pop();s.push(a);}
else if (c == '`') {s.pop();a=s.top()>a;s.pop();s.push(a);}
// values
else if (c == 'g') {s.pop();a=grid[a][s.top()];s.pop();s.push(a);}
if (s.size()>2 && c == 'p') {}
}}
}
else
{
if (c == '\"') StringMode = false;
else s.push(c);
}
return true;
}
int main()
{
srand(time(0));
stack<int> BefungeStack;
vector<string> BefungeGrid;
bitset<2> BefungeDirection(0);
BefungeGrid.push_back(string("12+.@"));
int x = 0, y = 0;
bool StringMode = 0;
bool Trampoline = 0;
while(ComeAcross(BefungeGrid[y][x], BefungeStack, BefungeGrid, StringMode, Trampoline, BefungeDirection))
{
if (BefungeDirection[0]==false) x-=(2*BefungeDirection[1]-1)*(1+Trampoline);
else y+=(2*BefungeDirection[1]-1)*(1+Trampoline);
if(y<0) y = BefungeStack.size()-1;
}
}
|