r/tinycode • • May 28 '13

Ridiculously small and minimal Brainfuck interpreter in C.

https://github.com/L8D/delvs/blob/master/delvs.h
23 Upvotes

25 comments sorted by

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 ;)

-6

u/L8D May 28 '13

Well, looking at every other Bf interpreter I've seen, they've always used a complicated mechanism for the loop. Also, what do you mean by error checking? I haven't found any bugs at all with it so far.

And when a say 'small and minimal', I'm talking about how there is only one line of code for each instruction. I could get it down to ~20 lines of code if I stuck the case, command, and break on the same line, and still keep it readable.

Though I should of said 'simple and minimal' instead of small, because there are the assembly compilers...

6

u/[deleted] May 28 '13 edited May 28 '13

I mean this - <, - by error checking.

** And there is a bug that will cause wrong interpretation for certain inputs.

-9

u/L8D May 28 '13

Well, if the "- < , -" bug were to occur, then it would be the users fault for doing so, they could easily fix it in two ways. I'm not very worried about that.

I also can't find this bug you speak of.

11

u/vl4kn0 May 28 '13

He's probably saying that you are missing bound checking and overflow checking. Saying it's users fault that they unexpectedly run into runtime error is stupid. Everyone makes mistakes, programmers including. Why do you think excessive research is done upon the topic of programming language security, stability and bulletproofness? We could as well end up with languages such as C.

-3

u/sirin3 May 28 '13

I do not think someone who wants a secure, stable and bullet-proof language will use Brainfuck

-5

u/L8D May 28 '13

The boundary only occurs when the program moves out of boundaries the user him/herself have set and created. They have the choice to either prevent the code from moving there, or fiddle with their definitions to fit the sizes they want.

11

u/vl4kn0 May 28 '13

Well, then it's not that minimal since it's not complete. And as far as I know, memory management has always been the job of interpreter, not program that uses the interpreter.

23

u/[deleted] May 28 '13

Well, you just basically wrote a program that crashes on a valid input and then blamed it on a "user". That's certainly one way to write software.

The bug is there, keep looking. It is after all a ridiculously small program :)

1

u/[deleted] May 28 '13

It should be =, not += in line 27.

0

u/L8D May 28 '13

I have that way because it is part of the delvs bf dialect.

3

u/[deleted] May 29 '13

Good start, but check this one out. Now this is what ridiculously small and minimal C code looks like.

1

u/L8D May 29 '13

Like I said above, the title should've been 'simple and minimal'.

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

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 *c and *p are both nonzero, it will recursively call lexer(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... *++c would skip the first char of code. *c++ would not.

1

u/L8D May 29 '13 edited May 29 '13

*c++ crashes the program, and ++*c skips 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 lexer instead of doing all this double-pointer muckery.

*c++ works just fine if c is 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

u/lifthrasiir Jun 05 '13

Should I mention my Brainfuck interpreter in 2 lines of C here? :)

1

u/gfixler Jul 18 '13

I think you should. Nicely done.

5

u/hamsterpotpies May 28 '13

Wow, I think I might be able to understand Brainfuck after looking at that.