Created
March 28, 2017 03:54
-
-
Save Kampfkarren/f90da411b5ec32c703ac46844cafa073 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//written entirely by boyned | |
//doesnt take any inputs bc im using coderpad | |
#include <stdio.h> | |
#include <string.h> | |
#define TAPE_LEN 256 | |
#define ECHO_BEHAVIOR 0 //0 - ASCII (real bf), 1 - actual number | |
void brainfuck(const char* code){ | |
int tape[TAPE_LEN] = {0}; | |
int tapeIndex = 0; | |
unsigned int i = 0; | |
unsigned int loops[TAPE_LEN]; | |
unsigned int loopNumber = 0; | |
while(i < strlen(code)){ | |
switch(code[i]){ | |
case '+': | |
tape[tapeIndex]++; | |
break; | |
case '-': | |
tape[tapeIndex]--; | |
break; | |
case '>': | |
tapeIndex++; | |
break; | |
case '<': | |
tapeIndex--; | |
break; | |
case '[': | |
if(tape[tapeIndex] == 0){ | |
break; | |
} | |
loopNumber++; | |
loops[loopNumber] = i; | |
break; | |
case ']': | |
if(tape[tapeIndex] == 0){ | |
loopNumber--; | |
} | |
if(loopNumber > 0){ | |
i = loops[loopNumber]; | |
} | |
break; | |
case '.': | |
printf(ECHO_BEHAVIOR ? "%d" : "%c", tape[tapeIndex]); | |
break; | |
case ',': | |
tape[tapeIndex] = getchar(); | |
break; | |
} | |
i++; | |
} | |
} | |
int main() { | |
brainfuck("+[>,.-------------]<[<]>>[+++++++++++++.>]"); //ask for an input and then echo it when the user finishes | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment