Skip to content

Instantly share code, notes, and snippets.

@Kampfkarren
Last active March 8, 2017 05:10
Show Gist options
  • Save Kampfkarren/d95a698d359fcb7d6e9781c8e1004485 to your computer and use it in GitHub Desktop.
Save Kampfkarren/d95a698d359fcb7d6e9781c8e1004485 to your computer and use it in GitHub Desktop.
a brainfuck interpreter i wrote with a friend with comments everywhere
/*
> - moves to the next index
< - moves to the previous index
+ - increases the current index's value by one
- - decreases the current index's value by one
. - prints the ascii character of the current index
, - takes an input of a single character and sets the current value to that character (doesn't print it tho)
[ - if the current value isn't 0, start loop. otherwise, go to ]
] - end of loop
*/
//then learn from me :)
#include <stdio.h> // what
#include <string.h>
//get java off the brain
//if u have questions about anything ask me :)
// k
#define MAX_INDEX 255
#define PRINT_TYPE 0
// that reminds me
// i hacked regexpal by pressing insert
// :O
// isnt long long int an actual data type // oh. i read somewhere it was
// no, just long
// im gonna use long cause ynot
long tape[MAX_INDEX];
int currentIndex = 0;
void interpret(const char* code){
int i = 0;
int loopNumber = 0;
int loops[MAX_INDEX]; //you dont set them unless you want default values, ints default value is always 0
//no you're thinking java
//a retard is someone who doesnt think in anything but java
//:thinking:
// oh that's right. i'm a retard
// even in java you don't set an array to 0
//int arrayName[arrayLength];
//you're getitng there :)
while(i < strlen(code)){
char c = code[i];
switch(c){
case '+':
tape[currentIndex]++;
break;
case '-':
tape[currentIndex]--;
break;
case '>':
if(currentIndex + 1 < MAX_INDEX){ currentIndex++; }
break;
case '<':
if(currentIndex - 1 > -MAX_INDEX + 1){ currentIndex--; }
break; //:)
case '.':
//printf("%c", (char)tape[currentIndex]);
switch(PRINT_TYPE){
case 0:
printf("%c", (char)tape[currentIndex]);
break;
case 1:
printf("%ld", tape[currentIndex]);
break;
}
break;
case '[':
// now we have to figure this out, its the hardest part
//i think thats not how to do it
// use a stack?
//maybe?
//we should probably store the i that starts off the loop
//and we also want to make sure we can nest loops, nesting loops is hard
// gah
//we can make it not nest loops if you'd like
//it would help
//alright
//lets make it easier on us and print out the int instead of the char
//i also just realised that this loop isnt implemented right cause we forgot to check the value if its 0 at the beginning
//currentIndex really shouldnt be referenced
//hmmhmmhmhmmmm.......:thinking:
//....................................................
//.........................
//i give up
//alright let me show you
// oh my GOD i'm an idiot
// i completely forgot about the existence of tape
//i havent even started it yet
if(tape[currentIndex] == 0){
break;
}
loopNumber++;
loops[loopNumber] = i; //now i have
break;
case ']':
// actual code goes here
if(tape[currentIndex] == 0){
loopNumber--;
}
//oh i think my bf code was wrong actually
// ?
if(loopNumber > 0){
i = loops[loopNumber];
}
//i think thats it?
//is that it
//ok lets test it
break;
case ',':
tape[currentIndex] = getchar();
break;
}
i++;
}
}
// do you know what language this reminds me of
//we did it reddit
//the int return is the code for linux and shit
//a proper return is 0, but anything else is interpreted as an error
int main(void){
//interpret("+>+>+> [++++++++++++++++++++++++++++++++++.>]"); //print '!'
//i think i fucked up my code
//the brainfck anyway
//i made it print chars again
//interpret("++++++++++++++++++++++++++++++++++++++.");
//ya think
//idk that might be intentional :thinking:
//we need a good example
//interpret("+[>,.----- ----- ---]<[<]>>[+++++ +++++ +++.>]");
interpret("+[.[.>]]"); //this should print 1 twice i think? let me try in a real brainfuck interpreter
//i think i did it?
//let me read my brainfuck and see if thats what i intended
// random zeros
// oh
//the 0 is the index
//i set it to print numbers again instead of characters
//let me check in an actual brainfuck interpreter again
//one that shows the stack (tape)
//ill upload this to gist :)
//yep i was right
//yep
//thats supposed to work, yeah
//i fucked up the nested loops
//il make '.' not print anything for now, and just make it print loopNumber
//ok lets figure it out
//thats supposed to print 1 twice
// good job boyned
//no its just wrong //christ
//hold on let me figure this out
//an implementation that would work
//i need to write an example that works in a real brainfuck interpreter
// sure
// all these comments for a few lines of code
return 0;
}
// you can learn from it though :)
// as long as you learn from it it was worthwhile
// wow i was really helpful with this wasn't i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment