1

I sat down and tried to implement the BrainFuck. The syntax seems pretty simple. I am having trouble getting the silly thing to work. I've been at this a while; and I admit I need some sleep. Maybe that is all the problem is. The interpreter isn't outputting anything. I'm pretty sure the problem is a simple one; and I know I need to modularise some of the function calls after I get a better grasp of what direction I want this program to go. Why am I getting no output?

main.c

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <memory.h>

#include "list.h"

node file;
node flow;
node memm;

void init() {
    file.val = 1;
    file.next = 0;
    flow.val = 1;
    flow.next = 0;
    memm.val = 1;
    memm.next = 0;
}

int run = 1;
void quit(int val) {
    run = 0;
    while (file.next) pop(&file);
    while (flow.next) pop(&flow);
    while (memm.next) pop(&memm);
}

void doop() {
    switch (file.val++) {
        case '>':
            memm.val++;
            break;
        case '<':
            memm.val--;
            break;
        case '+':
            get(&memm, memm.val)->val++;
            break;
        case '-':
            get(&memm, memm.val)->val--;
            break;
        case '.':
            printf("c", get(&memm, memm.val)->val);
            fflush(stdout);
            break;
        case '[':
            if (!get(&memm, memm.val)->val)
                while (get(&file, file.val)->val != ']')
                    file.val++;
            else push(&flow, file.val);
        case ']':
            if (get(&memm, memm.val)->val)
                file.val = pop(&flow);
    }
}

int main(int argc, char** argv) {
    int flen, c, i, f_len;
    FILE *fh;
    char fh_name[] = "test";

    signal(SIGINT, quit);
    init();

    fh = fopen(fh_name, "r");
    while (run && (c = fgetc(fh)) != EOF)
        push(&file, c);
    fclose(fh);

    f_len = length(&file);
    while (file.val > 0 && file.val < f_len)
        doop();
    return (EXIT_SUCCESS);
}

list.h

struct node {
    int val;
    struct node *next;
};
typedef struct node node;
int length(node *n);
void push(node *n, int i);
int pop(node *n);
node *get(node *n, int i);

list.c

#include <stdlib.h>
#include "list.h"

int length(node *m) {
    int len = 0;
    while (m->next) {
        len++;
        m = m->next;
    }
    return len;
}

void push(node *n, int i) {
    node *m = n;
    while (m->next)
        m = m->next;
    m->next = malloc(sizeof(struct node));
    m->next->val = i;
    m->next->next = 0;
}

int pop(node *n) {
    node *m = n;
    int i = length(n) - 1;
    while (i) {
        i--;
        m = m->next;
    }
    i = m->next->val;
    free(m->next);
    m->next = 0;
    return i;
}

node *get(node *n, int i) {
    node *m = n;
    while (i) {
        i--;
        if (!m->next)
            push(n, 0);
        m = m->next;
    }
    return m;
}

test is the BrainFuck "hello world"

Hello World program
>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]
<.#>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++[
<++++>-]<+.[-]++++++++++.
5
  • 1
    @EugeneSh. yes, i'm still at it right now. gdb and I are good friends
    – motoku
    Mar 6, 2015 at 21:01
  • @MotokoKusanagi check that fopen() succeeded before attempting to read from the file, and also The interpreter isn't outputting anything, there is no interpreter involved. Mar 6, 2015 at 21:06
  • @iharob the file is being read; I can read the file in reverse out of memory by popping all the file values. As for there being "no interpreter involved", I probably am using the incorrect term.
    – motoku
    Mar 6, 2015 at 21:09
  • @MotokoKusanagi that is not a good reason to write unsafe code, you can get used to it. Mar 6, 2015 at 21:11
  • I got it working. There were a miltitude of thing wrong with it: Missing breaks, multiple logical errors.
    – motoku
    Mar 6, 2015 at 22:23

2 Answers 2

3

The line

switch (file.val++) {

just can't be right. Currently it's just incrementing the first "val" of the file chain like the "mem.val++" below it.

I expect you'll need to get rid of the ++ on that line then do something about incrementing a pointer to an instruction rather than the instruction itself.


Your ']' instruction is wrong; you'll need to do the pop even if you've not going to go back.


Your '[' instruction is partly wrong. If the value starts at zero it will currently skip to the first ']' it finds not the matching ']'.

3

It's because you need to sleep and also because your code is messy, I found this

printf("c", get(&memm, memm.val)->val);

it will print a c and that's all, it should be

printf("%c", get(&memm, memm.val)->val);
/*      ^ it's the format specifier for the argument */

How did I find this so quickly?

  • Very simple, I enabled compiler warnings.

BTW: get(&memm, memm.val)->val is really bad style, but really bad.

0

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.