Skip to content

Instantly share code, notes, and snippets.

@ox
Created February 12, 2010 15:52
Show Gist options
  • Save ox/302672 to your computer and use it in GitHub Desktop.
Save ox/302672 to your computer and use it in GitHub Desktop.
FULL Brainfuck implimentation in response to Marc-André's Gist: http://gist.github.com/302316;
// Brainfuck, full implimentation, idea by Marc-André Cournoyer. this is turing complete.
// the bracket code was made by a friend, zli5t. my implementations were way too complex. or wrong.
// compile: gcc 1L.c
// example:
// ./a.out ++.
// expanded:
// incriment reg[0] by 1
// incriment reg[0] by 1
// print out reg[0]
// outputs: 2
#include "stdio.h"
main (int argc, char *argv[]) {
char *arg = argv[1];
char reg[300000]={};
int i = 0;
int j;
while(*arg) {
switch(*arg) {
case '+': reg[i]++;break;
case '-': reg[i]--;break;
case '<': if(i!=0)i--;break;
case '>': if(i!=29999)i++;break;
case '.': printf("%i", reg[i] );break;
case ',': reg[i]=getchar();break;
case '[': if(reg[i]!=0)break;j=1;while(j!=0){arg++;if(*arg=='[')j++;if(*arg==']')j--;}break;
case ']': if(reg[i]==0)break;j=1;while(j!=0){arg--;if(*arg==']')j++;if(*arg=='[')j--;}break;
} arg++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment