elikaski / BF-it

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

Implement bracket-less scopes #33

Closed NeeEoo closed 3 years ago

NeeEoo commented 3 years ago

After reading more, the braces that defines a scope is in fact a statement. And for, if, else and while inner code wants a statement.

while ( <expression> )
    <statement>

for ( <expression> ; <expression> ; <expression> )
    <statement>

if (<expression>)
    <statement1>
else
    <statement2>

So, this could maybe be merged and then removed later in another pull request that fixes it so that for, if, else and while wants a statement in its inner code.

NeeEoo commented 3 years ago

Do you know a way to get the token after a statement?

Since if the compiler allows non-bracketed statements in compile_if, the self.parser.find_matching() wouldn't work.

If the compiler compiles the statement before checking if there exists an else token the stack pointer would be wrong.

NeeEoo commented 3 years ago

One way that i can think of is to save the current token index to a variable, and run compile_statement and then save the current token index to another variable, then set the token index back to what it was before, and then we get the token.

But that seems a bit unnecessary to compile the statement twice. Once during checking and then again when converting to code.

NeeEoo commented 3 years ago

@elikaski I also need help with the for loop. Since it handles the inner scope differently than the other.

elikaski commented 3 years ago

fixed in 0d0bc8800dd10fb8669cd5cb4259038dbf575418

NeeEoo commented 3 years ago

Thank you. The code for if else looks a lot better than mine.

NeeEoo commented 3 years ago

The only thing with the new implementation is that else-less ifs takes up a little more bytes.

elikaski commented 3 years ago

You're right, we do waste a cell for if without else but that's a price I'm willing to pay 🤷

NeeEoo commented 3 years ago

Might be able to fix it sometime in the future.