Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

debugging - Brain**** implimentation in C

I'm trying to run a hello world program with the brainfuck implementation that I made this week but I'm getting a strange output.

This is the hello world file that I'm trying to run.

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.------- 
-.>>+.>++.

I have saved it to hello.txt, and am parsing it in the main function of the program.

The output is

1 -3 4 4 7 0 -1 7 10 4 -4 1 2

which, when you add 72, and convert to character , gives

I E L L O H G O R L D I J

So it seems like there is something I'm missing, even though its sort of close to the right solution.

But I'll go into the structure of the thing anyhow, the general idea is as followed:

We have two 'tapes', a program to follow which I label 'parsed' which has a pointer 'attached' to it, and an empty tape to write into, which I label 'mem', which also has a pointer 'attached' to it. We also have a list of structures that we can use to jump between bracket locations.

In the main function, there are three parts: first I parse the program and store it in an array. Then I run through this parsing and match brackets, then I start the brainfuck loop and write the output onto the empty array, until the final character has been reached.

In the brainfuck loop, when we find a bracket, we run through the list of pairs to find its match, and then jump to that location.

Maybe it's a bit clunky, but I hope it makes sense.

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

    #define MAX 30000

    //input tape
    char parsed[MAX];
    char * ptr;

    //bracket matching
    struct bracketlinks{
        char * bracket_1;
        char * bracket_2;
    };
    struct bracketlinks * pairslist;
    int bracket_count;

    //output tape
    char mem[MAX] = {0};
    int * mptr;

    int main(){
        mptr = malloc(sizeof(int));

        //parse text file and make list of tokens
        FILE * parsecode;
        parsecode = fopen("helloworldbf.txt", "r");
        int count = 0;
        char buffer;
        while(!feof(parsecode)){
            buffer = fgetc(parsecode);
            if(buffer == 10){break;}
            if(buffer == 32){continue;}
            else{
                parsed[count] = buffer;
                if(buffer == 91 || buffer == 93){
                    bracket_count++;
                }
                count++;
            }
        }
        fclose(parsecode);

        pairslist = malloc(bracket_count * sizeof(char*));

        //creates array of structures which match brackets so we can perform memory jumps
        int reset_count;
        int list_counter = 0;
        for(int i = 0; i < count; i++){
            if(parsed[i] == '['){
                reset_count = 0;
                for(int j = 0; j < count - i + 1; j++){
                    if(parsed[i + j] == '['){reset_count++;}
                    if(parsed[i + j] == ']'){reset_count--;}
                    if(reset_count == 0){
                        struct bracketlinks new;
                        new.bracket_1 = &parsed[i];
                        new.bracket_2 = &parsed[i + j];
                        pairslist[list_counter] = new;
                        list_counter++;
                        break;
                    }
                    else{continue;}
                } 
            }
            else{continue;}
        }

        //runs through the input tape and performs operations on the output tape
        ptr = parsed;
        char final_token = ptr[count];
        while(ptr[0] != final_token){
            if(ptr[0] == '['){
                if(mem[mptr[0]]){++ptr;}
                if(!mem[mptr[0]]){
                    for(int i = 0; i < bracket_count/2; i++){
                        if(pairslist[i].bracket_1 == &ptr[0]){
                            ptr = ++pairslist[i].bracket_2;
                        }
                        if(pairslist[i].bracket_2 == &ptr[0]){
                            ptr = ++pairslist[i].bracket_1;
                        }
                        else{continue;}
                    }
                }
            }
            if(ptr[0] == ']'){
                if(!mem[mptr[0]]){
                    for(int i = 0; i < bracket_count/2; i++){
                        if(pairslist[i].bracket_1 == &ptr[0]){
                            ptr = ++pairslist[i].bracket_2;
                        }
                        if(pairslist[i].bracket_2 == &ptr[0]){
                            ptr = ++pairslist[i].bracket_1;
                        }
                        else{continue;}
                    }
                }
                if(mem[mptr[0]]){++ptr;}
                else{continue;}
            }
            if(ptr[0] == '+'){++ptr;++mem[mptr[0]];}
            if(ptr[0] == '-'){++ptr;--mem[mptr[0]];}
            if(ptr[0] == '>'){++ptr; mptr[0]++;}
            if(ptr[0] == '<'){mptr[0]--;++ptr;}
            if(ptr[0] == '.'){++ptr; printf("%c %d 
", mem[mptr[0]] + 72, mem[mptr[0]]);}
            else{continue;}
        }
        free(pairslist);
        free(mptr);
        return 0;
    }

Any help would be massively appreciated,

Cheers..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK so here's the fix for the main bug: basically just switch the exclamation marks and don't be a numpty.

 if(ptr[0] == ']'){
            if(mem[mptr[0]]){
                for(int i = 0; i < bracket_count/2; i++){
                    if(pairslist[i].bracket_1 == &ptr[0]){
                        ptr = ++pairslist[i].bracket_2;
                    }
                    if(pairslist[i].bracket_2 == &ptr[0]){
                        ptr = ++pairslist[i].bracket_1;
                    }
                    else{continue;}
                }
            }
            if(!mem[mptr[0]]){++ptr;}
            else{continue;}
        }

Still working on the !feof thing but will update with that too


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...