mlogjs / mlogjs

Compile javascript into Mindustry logic code (mlog)

Home Page:https://mlogjs.github.io/mlogjs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Useless jumps with nested if

hsaturn opened this issue · comments

Hello

Some useless jumps are generated with nested if.
Here is the smallest code I've found, but with large code, I saw a bunch of 7 useless jumps !!

var c;
if (c < 10) {
    if (c < 5)
        c = 5;
    else
        c = 10;
}
else
    c = 23;

The generated code is

0 jump 6 greaterThanEq c:1:4 10
1 jump 4 greaterThanEq c:1:4 5
2 set c:1:4 5
3 jump 5 always  --> JUMP TO 5
4 set c:1:4 10
5 jump 7 always --> THEN JUMP TO 7 !
6 set c:1:4 23
7 end

As we can see, the jump at line 3 goes to line 5 wich jumps then to line 7.
I think this is easy to add a pass to optimize this, without having to remove a line (which could cause some issue with indirect jumps such as those made with @counter (arrays etc..))

Best regards, MLogJs is so great !

Hi, thanks for filing an issue. This problem will be fixed by #222.