elikaski / BF-it

A C-like language to Brainfuck compiler, written in Python
MIT License
121 stars 11 forks source link

Add Labels and Goto #58

Open NeeEoo opened 3 years ago

NeeEoo commented 3 years ago

This is a thing that would probably not be implemented for a long time, due to it's completely.

Here is an example:

void main() {
    int x = 5;
    int y = 7;

    if(x>y) {
        goto invalid;
    }

    for(int a = 0; a < 10; a++) {
        if(a*y+x > 128) goto invalid;
        if(a*y+x < 3) goto invalid;
    }

invalid:
    print("Invalid Value");
    while(1){};
}
elikaski commented 3 years ago

This seems very difficult to implement. Do you have any idea how to do it? Because it's hard for me to think of one Note: while implementing this, should also implement "return" statement, to actually affect the flow.

NeeEoo commented 3 years ago

I made this issue as a long term thing. I can only think of one thing, and that is to convert a lot of code into if(!__COMPILER__should_goto_invalid) {}

I actually forgot to add goto valid; and valid: in the code, without them it will always print Invalid Value Here is a way the converted code could be:

void main() {
    int x = 5;
    int y = 7;

    bool __COMPILER__should_goto_invalid = false;
    bool __COMPILER__should_goto_valid = false;

    if(x>y) {
        __COMPILER__should_goto_invalid = true
    }

    if(!__COMPILER__should_goto_invalid) {
        for(int a = 0; a < 10; a++) {
            if(a*y+x > 128) __COMPILER__should_goto_invalid = true;
            if(!__COMPILER__should_goto_invalid) {
                if(a*y+x < 3) __COMPILER__should_goto_invalid = true;
            }

            // internal condition code

            if(__COMPILER__should_goto_invalid) {
                condition = false;
            }
        }
    }

    if(!__COMPILER__should_goto_invalid) {
        __COMPILER__should_goto_valid = true;
    }

    if(!__COMPILER__should_goto_valid) {
        if(__COMPILER__should_goto_invalid) {
            print("Invalid Value");
            while(1){};
        }
    }
}

But i don't think that this will work for more labels and other statements.


I believe that in the future we might come up with a way to accomplish this.