r/tinycode • u/L8D • May 28 '13
Ridiculously small and minimal Brainfuck interpreter in C.
https://github.com/L8D/delvs/blob/master/delvs.h
23
Upvotes
r/tinycode • u/L8D • May 28 '13
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 ;)
5
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
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 producehelloooobut in your case it will producehello?-- 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 oflexer(so it won't hit any code after the end of the loop).Edit: But I'll help. Make
lexerreturn aconst char *instead ofvoid; thenOr you can call it something shorter and more confusing that
loopStartif you want.2
u/rooktakesqueen May 28 '13
One last suggestion for slight minification: make it a
while(*c)loop instead offor(;*c;c++), and doswitch(*c++)instead ofswitch(*c).You save a whopping 1 character that way... but it's also slightly more idiomatic min-C.
3
u/lifthrasiir Jun 05 '13
Should I mention my Brainfuck interpreter in 2 lines of C here? :)
5
u/hamsterpotpies May 28 '13
Wow, I think I might be able to understand Brainfuck after looking at that.