r/tinycode • u/L8D • May 28 '13
Ridiculously small and minimal Brainfuck interpreter in C.
https://github.com/L8D/delvs/blob/master/delvs.h5
u/rooktakesqueen May 28 '13 edited May 28 '13
Running this with the input you've given as an example, ",[.,]", will inevitably cause a stack overflow on the [.
If you change line 31 to
while(*p) lexer(c+1, p);then you'll get around that problem, but you'll still be faced with the problem that the looping isn't quite accurate. You would expect the program
+++++[>,.<-]>...given the input hello? to produce helloooo but in your case it will produce hello? -- after finishing the loop that's begun by the [ at position 5 it continues to position 6 instead of jumping to position 12 as would be expected (so it does the loop body one extra time), and as soon as it hits the ] at position 11 it will return from the outermost call of lexer (so it won't hit any code after the end of the loop).
Edit: But I'll help. Make lexer return a const char * instead of void; then
case '[':
int loopStart = c+1;
while(*p) c = lexer(loopStart, p);
break;
case ']':
return c+1;Or you can call it something shorter and more confusing that loopStart if you want.
1
u/L8D May 28 '13
When I tested with both:
,[.[-],]and
++++++++++[>++++++++++>+<<-]>++++.+.>.they had worked as expected.
Though I'll change it to fit the dialect. Thanks for the find.
3
u/rooktakesqueen May 28 '13 edited May 28 '13
That's confusing because the code as written simply cannot handle encountering
[without causing an infinite loop. Removing extraneous bits:void lexer(const char *c, short int *p) { for (; *c; c++) { switch (*c) { case '[': while(*p) lexer(c, p); break; } } }If
*cand*pare both nonzero, it will recursively calllexer(c,p)over and over until stack overflow.3
u/L8D May 28 '13 edited May 29 '13
OOOOH SHIT!!! I forgot to push my fix for that...Re-clone an compile now. Sorry.
EDIT: This fix fixes what you edited into your original comment.
2
u/rooktakesqueen May 28 '13
One last suggestion for slight minification: make it a while(*c) loop instead of for(;*c;c++), and do switch(*c++) instead of switch(*c).
You save a whopping 1 character that way... but it's also slightly more idiomatic min-C.
1
u/L8D May 28 '13 edited May 28 '13
Thanks, didn't think that'll work well with switch at first.
EDIT: It doesn't, this causes it to skip the first char of code, fixing this will take more space and proccessing than not. Also, the for loop makes the code more humanly readable.
2
u/rooktakesqueen May 29 '13
this causes it to skip the first char of code
No it doesn't...
*++cwould skip the first char of code.*c++would not.1
u/L8D May 29 '13 edited May 29 '13
*c++crashes the program, and++*cskips first char. I couldn't find a practical fix. I'd have to do(*c)++, or, due to my fix for looping,*((*c)++)1
u/rooktakesqueen May 29 '13
Using a double pointer the way you're doing now is kind of smelly. It shouldn't be necessary. You can return a value from
lexerinstead of doing all this double-pointer muckery.
*c++works just fine ifcis a single pointer; the++will always resolve with higher precedence than the*. If it's a double pointer then yeah, you need to do something like*(*c)++and that's just unpleasant.
3
5
u/hamsterpotpies May 28 '13
Wow, I think I might be able to understand Brainfuck after looking at that.
28
u/[deleted] May 28 '13
That's hardly qualifies as "ridiculously small and minimal". This is what a typical first crack at a brainfuck interpreter looks like. The lack of error checking alone will expeditiously bite you in a butt on something like mandelbrot.b. There's at least one bug in there too.
Keep minimizing ;)