40
\$\begingroup\$

I am a beginner with regards to C; I'm used to using high-level languages (Java, Python, JS, etc) and this is my first project in a lower-level language. I wrote a simple Brainfuck interpreter, given that my Python implementation was super slow.

Here's the code:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

unsigned char mem[30000] = {0};
unsigned char* ptr = mem;

void goToLoopEnd(char** ip) {
    int searching = 1;

    while (searching > 0) {
        (*ip)++;

        switch (**ip) {
            case '[':
                searching++;
                break;
            case ']':
                searching--;
                break;
            default:
                break;
        }
    }
}

void goToLoopStart(char** ip) {
    int searching = 1;

    while (searching > 0) {
        (*ip)--;

        switch (**ip) {
            case ']':
                searching++;
                break;
            case '[':
                searching--;
                break;
            default:
                break;
        }
    }
}

void interpret(char* code) {
    char* ip;

    for (ip = code; *ip != '\0'; ip++) {
        switch (*ip) {
            case '>':
                ptr++;
                break;
            case '<':
                ptr--;
                break;
            case '+':
                (*ptr)++;
                break;
            case '-':
                (*ptr)--;
                break;
            case '.':
                putchar(*ptr);
                break;
            case ',':
                *ptr = getchar();
                break;
            case '[':
                if (*ptr == 0) {
                    goToLoopEnd(&ip);
                }
                break;
            case ']':
                if (*ptr != 0) {
                    goToLoopStart(&ip);
                }
                break;
            default:
                break;
        }
    }
}

int main() {
    // Set to interpret a mandelbrot set fractal viewer in brainfuck written by Erik Bosman.
    interpret("+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+[>>>>>>[>>>>>>>[-]>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>[-]+<<<<<<++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>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>>>>>]>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<[<<<<<<<<<]>>>>>>>>>[>>>>>>[-<<<<<<->>>>>>]+<<<<<<[->>>>>>->>[-<<<<<<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<<<<<<]]>>>]");

    return 0;
}
```
\$\endgroup\$
4
  • 2
    \$\begingroup\$ I'm looking forward to your posts when you have become an experienced C programmer! :-) \$\endgroup\$ Commented Nov 26, 2019 at 14:36
  • \$\begingroup\$ For loops: wouldn't it better to put loop start position in stack on each [ encountered, and on ] check top address of the stack (and on 0 in cell pop the stack)? IMO that will reduce number of traversings in goToLoop functions. \$\endgroup\$
    – user28434
    Commented Nov 27, 2019 at 9:13
  • 1
    \$\begingroup\$ This is pretty good! One recommendation: Wrap your memory and instruction pointer into a "BrainFuckVM" struct. That will tidy that code up, and allow you to use some stronger types signatures on your functions. Plus from there you can get fancy and add snapshotting to save/restore vm state, you can add an "undo" to go back in time in the processing, etc.! \$\endgroup\$
    – Alexander
    Commented Nov 27, 2019 at 15:17
  • \$\begingroup\$ You lucky... I asked about mine a while back and I had to have a bounty put on it to get anything. \$\endgroup\$
    – S.S. Anne
    Commented Nov 28, 2019 at 15:54

4 Answers 4

48
\$\begingroup\$

This is a pretty reasonable start on a simple interpreter. Edward's suggestions are all good; a few additional suggestions:


interpret("+++++++++++++[->....

Please break up that long line. C allows you to break up literal strings

"like "
"this."

void goToLoopEnd(char** ip) {
...
void goToLoopStart(char** ip) {
...

If you wrote these instead as

char * findLoopEnd(char* ip) {
...
char * findLoopStart(char* ip) { 
...

then you have functions that are just as useful, but do not modify the input and are therefore testable with an independent unit test.

This also eliminates a double pointer; double pointers are hard to reason about, so if there is a cheap and easy way to get rid of them, go for it.

This also enables you to improve the contract of these methods; for example, you could return null if there is no matching bracket. Which then brings up...


Aside from that, my biggest problem with your interpreter is that it has undefined behaviours all over the place if the program is not valid. What happens if there is an unmatched [? What happens if the pointer goes before available memory? Bad stuff, that's what. A good interpreter is safe no matter what its input, and gives good diagnostics to the user when the input is bad. Always structure an interpreter so that it detects bad inputs, because it will get bad inputs.

In particular, note that Brainfuck does not specify what happens when the tape pointer becomes negative; does the tape "wrap around?", is it an error, is there supposed to be an infinite tape in both directions? The conservative thing to do is to make it an error, but you haven't even done that; you just do something undefined.


Once you have the undefined behaviours under control, consider the cost of doing a linear search for matching brackets all the time. It is almost always more efficient to scan the source code once, make a note of the locations of all matching pairs of brackets, and then when you hit a bracket you just do a lookup of the destination rather than a linear search for it. Yes, a linear search is fast if the program is small, but the program might not be small.

This step works to support the previous issue; if you record matching brackets before you run the program, you guarantee that every bracket is matched!

\$\endgroup\$
12
  • 3
    \$\begingroup\$ Strangely, there are no standard diagnostics requirements for Brainfuck interpreters. Your note about checking for bad input is an important one! \$\endgroup\$
    – Edward
    Commented Nov 25, 2019 at 19:49
  • 1
    \$\begingroup\$ I agree with your logic about undefined behavior but I find it ironic we are complaining about it on a question written in C. :) \$\endgroup\$ Commented Nov 25, 2019 at 22:07
  • 14
    \$\begingroup\$ @CaptainMan: That is somewhat ironic, yes. The given implementation turns undefined behaviour in the BF language into undefined behaviour in the C language! The better approach is to define the behaviour in the BF language, and then implement that so that there is no UB in the C implementation. \$\endgroup\$ Commented Nov 25, 2019 at 22:18
  • 3
    \$\begingroup\$ +1, was about to write an answer about using an O(1) map / dictionary data structure to find branch targets (possibly as simple as an array of char* or unsigned indexed by offset within the BF program), but you already mentioned that. Also that depth or nest_depth might be a clearer var name than searching. \$\endgroup\$ Commented Nov 26, 2019 at 2:50
  • 7
    \$\begingroup\$ @Ghost: Whether any choice is "over engineering" depends entirely on the requirements of the client who is you. You moved to C from Python, you say, because the performance was not good enough; you're the only person who knows if the performance is still not good enough. I imagine that you also chose C because it's a language you wish to learn, and implementing simple data structures like hash maps is a great way to learn a new language. \$\endgroup\$ Commented Nov 26, 2019 at 15:30
28
\$\begingroup\$

Here are some things that may help you improve your program. In all, it seems to be nice, straightforward code that does what it needs to do. Good start!

Use only required #includes

The code has #include <stdbool.h> but doesn't use booleans. It also appears that nothing from <stdlib.h> is used either. Only include files that are actually needed.

Avoid the use of global variables

I see that ptr and indirectly, mem are used only within interpret() but they are declared as global variables. It's generally better to explicitly pass variables your function will need or declare them within the appropriately smallest possible scope rather than using the vague implicit linkage of a global variable. In this case, both mem and ptr could be local variables within interpret(). Use memset if you need to zero the contents.

Use const where practical

The interpret() function does not modify the contents of the code pointer it is passed, so it should be declared const:

void interpret(const char* code) { /*...*/ }

This also means that ip can be const and then also that the arguments to the other functions can be, too.

Declare local routines static

If you declare goToLoopEnd and goToLoopStart as static, the compiler will know that they can never be called outside this one file. With that information, much more optimization might be done, such as inlining the code.

Let the compiler generate code

When a C program reaches the end of main the compiler will automatically generate code to return 0, so it is not necessary to write return 0; explicitly at the end of main.

\$\endgroup\$
5
  • 39
    \$\begingroup\$ I agree with all of these suggestions except for the last one. I prefer the return value to be explicit. main is the only function where omitting the return value is allowed (except for void functions), so including it there as well makes the code more consistent. I do not see explicit guidance on this in the C++ core guidelines though, so it seems to be a matter of preference. \$\endgroup\$ Commented Nov 25, 2019 at 19:26
  • 1
    \$\begingroup\$ mem being inside interpret might be a stretch. Probably would be easier to dynamically-allocate ptr. \$\endgroup\$
    – S.S. Anne
    Commented Nov 25, 2019 at 19:42
  • 2
    \$\begingroup\$ @MitchLindgren it is solely a matter of preference, but it is good to know about that aspect of the standard either way. \$\endgroup\$
    – Edward
    Commented Nov 25, 2019 at 20:13
  • 3
    \$\begingroup\$ @JL2210: Most efficient but still good style: static unsigned char mem[30000] = {0}; inside interpret. If you know how much zeroed memory you want, you might as well statically allocate it in the BSS instead of dynamically with calloc. Although for practical purposes, using a big calloc might put it farther from anything else and give more protection against out-of-bounds writes (simple segfault instead of maybe corrupting .data or other BSS objects). In C terms a malformed BF program can cause UB in this interpreter either way; in mainstream implementations the details matter. \$\endgroup\$ Commented Nov 26, 2019 at 2:41
  • 2
    \$\begingroup\$ If you don't include stdbool.h, replace unsigned char mem[30000] = {0}; by static unsigned char mem[30000];, prototype by int main(void), and don't follow that last suggestion, then your code is now compatible with C89, and many more compliers are open to you. \$\endgroup\$
    – Neil
    Commented Nov 26, 2019 at 20:50
2
\$\begingroup\$

Expanding on Edward's point with "const": It's good practice to properly qualify all constant data, not so much for optimization (see http://www.gotw.ca/gotw/081.htm) as for displaying the author's intent, and for preventing errors. const is infectious, so you'll have to change all function signatures and pointer declarations operating on the constant brainfuck code.

That you can initialize a char * with a literal at all is pure legacy: K&R C did not have const (which reflected the fact that one could (I think) write anywhere in memory on then-typical machines like the PDP11). So there was already a lot of code out there which would break, and even after 1989 a lot of people didn't use const.

Back to your program: I do not know brainfuck; a brainfuck program could well be self-modifying like a Turing machine. That it doesn't do that can be expressed in C, preventing accidental write attempts as well.

Incidentally this change makes your program valid C++, another thing I find good practice, even if Lightness Races with Monica will probably disagree: So cast your mallocs if you have any (even if unwind disagrees), cast assignments to enums if you have any, and const your consts.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ BF programs are, thank goodness, not self-modifying. A BF program acts like a Turing Machine with two tapes, a finite, read-only tape for the program, and a writable, infinite tape for the data. Since BF is Turing Complete, you can of course write a simulation of a TM in BF; that's left as an exercise. :) \$\endgroup\$ Commented Nov 27, 2019 at 0:05
  • \$\begingroup\$ Making code be also valid C++ is generally good, but not when it means casting malloc IMO. Re: writeable strings: some PDP-11 models do have memory protection (wikipedia). Early Unix was developed on PDP-11; perhaps it didn't enforce read-only pages, or toolchains put string literals in the .data section. Note that char *p = "hello" is not problematic if you never actually write through the pointer. But yes, that will segfault on modern Unix systems, unless you compiled with a compiler like GCC3 or older with gcc -fwriteable-strings. \$\endgroup\$ Commented Nov 28, 2019 at 6:08
  • \$\begingroup\$ @PeterCordes Ah, interesting! (Both the PDP11 and gcc info). \$\endgroup\$ Commented Nov 28, 2019 at 8:17
  • 1
    \$\begingroup\$ @EricLippert you haven't than seen "fukyorbrain" yet, two BF programs that have "multiple" threads and modify each other :) \$\endgroup\$
    – ljrk
    Commented Dec 14, 2019 at 17:34
1
\$\begingroup\$

Adding to the other answers: These function declarations

int foo();
int bar() { }

do not declare a prototype for foo respectively bar. In consequence, this is a legal implementation of foo:

int foo(int a) { }

And this is a likely illegal call to bar, however the compiler can't warn:

bar(42);

To provide a prototype, add void inside the parenthesis to make it a parameter-list (instead of the C89 identifier list) and all of the above would yield compile time errors:

int foo(void);
int bar(void) {}

This is due to Cs history of K&R style function declarations and definitions:

int foo();

int foo(a)
int a;
{
   /* do something */
}

Compatibility is great, but I don't think you should go lower than C99 if not explicitly needed, especially if it lessens the likelihood of severe bugs ;)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.