Translation into C

bAdkOde can easily be translated into C. Assuming that we have three (long int) variables called a, b, and top that refer to register a, register b, and top of the stack, and also assuming that we have two (long int) arrays mem and stack, we can translate bAdkOde into C via the following rules:

1. Any references to the registers a and b directly translate to the variables 
   a and b.
2. [b or [a translates to mem[b] or mem[a].
3. ) and ( both use stack[top]. However, in ), stack[top] is on the 
   right-hand side of the assignment expression whereas in ( it is on the 
   left-hand side.
4. >, +, and - map to =, +, and - respectively.
5. ', ", and ? map to printf (or printf("%d", value)), putchar (from stdio.h) 
   and getchar (also from stdio.h).
6. { maps to while, and =, !, +, and - map to == 0, != 0, >= 0, and < 0. } 
   maps to the closing } brace for the while.

Note: My current implementation (which I wrote around four or five years ago) really sucks! I think the interpreter could be written more elegantly. Also, I think I should probably figure out another way to implement the memory and stack in the C translation. Right now I create a fixed-size array. I think the ideal implementation would be a linked list that grows and shrinks as required.

Translation of fibonacci.bad into fibonacci.c


int main()
long int a = 0;
long int b = 0;

long int mem[131072];
long int stack[131072];

long int top = 0;
long int i = 0;

/* zero out memory and stack */
for(i = 0; i < 131072; i++) { mem[i] = 0; stack[i] = 0; } stack[top] = 0; top++; stack[top] = 1; top++; a = 10; mem[a] = a; while(mem[a] != 0) { top--; a = stack[top]; top--; b = stack[top]; stack[top] = b; top++; printf("%d", b); putchar(32); b += a; top--; a = stack[top]; stack[top] = b; top++; stack[top] = a; top++; a = 10; mem[a]--; } putchar(8); putchar(10); return 0; } [/sourcecode] Turing completeness and self-modifying code

I have no idea if bAdkOde is Turing Complete. I suspect it might be, but I have no proof. I recall reading somewhere that if you are able to write a quine (program that prints itself) in a language, then that language is Turing Complete. I figure that if I'm able to write a quine then I should be good! Regarding self-modifying code, I expect that it should be trivial to modify the interpreter so that program code exists in the same memory-space as data. However, this would make the translation of the program into C a little trickier.

Oh, and here is a link to the bAdkOde interpreter that I wrote in Perl (I'm not proud of this code at all! Could be written better :p).

Closing remarks

bAdkOde isn't meant to be a practical language. It's an esoteric language and its creation was an academic exercise for me. If you're trying to write actual software in bAdkOde, then God help you! 🙂