Gotohell

From Esolang
Jump to navigation Jump to search

Gotohell is a variation of C that disallows any flow control or function definitions (besides main), and focuses on defining labels and using the goto statement. switch statements are not allowed.

if statements can only be used in the following way:

if(condition) goto label; 

Turing Completeness

Gotohell is a bounded-storage machine as it is a subset of C, which has a limited amount of memory.

Note: if unbounded integers are allowed in an int, then it's Turing-complete, 'cause you can use malloc

Examples

Brainfuck interpreter with auto-extending memory

#include <stdio.h>

int cs[255];
int csp = 0;

int* tape;
int tp = 0, cmsize = 3;

#define PUSH(x) cs[++csp] = x
#define POP cs[csp--] 

const char* program="YOUR PROGRAM HERE";
int main(int argc, char** argv){
    tape=malloc(sizeof int*cmsize);
	gets(program);
	int idx = 0;
	loop:
		if(program[idx] == '>') goto tr;
		if(program[idx] == '<') goto tl;
		if(program[idx] == '+') goto plus;
		if(program[idx] == '-') goto minus;
		if(program[idx] == '[') goto bloop;
		if(program[idx] == ']') goto endbloop;
		if(program[idx] == '.') goto print;
		if(program[idx] == ',') goto input;
		goto exit;
        extend:
            int nmsize=cmsize<<1, *oldtape=tape, cindex=0;
            tape=malloc(nmsize*sizeof int);
            tmp:
                if(cindex>=cmsize)goto endl;
                tape[cindex]=oldtape[cindex];
                cindex++;
                goto tmp;
		tr:
			tp++;
            if(tp>=cmsize)goto extend;
			goto endl;
		tl:
			tp--;
			goto endl;
		plus:
			tape[tp]++;
			goto endl;
		minus:
			tape[tp]--;
			goto endl;
		bloop:
			if(tape[tp] > 0) goto t;
			int loopP = 1;
			s:
				if(loopP == 0) goto endl;
				if(program[++idx] == '[') goto i;
				if(program[++idx] == ']') goto d;
				goto s;
				i: loopP++; goto s;
				d: loopP--; goto s;
			t:
				PUSH(idx);
			goto endl;
		endbloop:
			int lindex = POP;
			if(tape[tp] > 0) goto j;
			goto endl;
			j: idx = lindex; PUSH(lindex);
			goto endl;
		print:
			putchar(tape[tp]);
			goto endl;
		input:
			tape[tp] = getchar();
			goto endl;
		endl:
			idx++;
			goto loop;
	exit:
}

Input is taken through stdin.