darknesswind / NutCracker

fork from DamianXVI's squirrel decompiler
20 stars 9 forks source link

loop flags are not reset in nested loops, causing bad output #24

Open AdamMil opened 5 years ago

AdamMil commented 5 years ago

When new state blocks are pushed onto the state stack, the loop state is not reset. This causes failures processing nested loops. Try compiling and decompiling this code:

while(true)
{
    if (true)
    {
        if (true)
        {
                continue;
        }
    }

    for(local i = 0; i <= 10; i++)
    {
        if (true)
        {
                continue;
        }
        else if (true)
        {
            if (true)
            {
            }
            else
            {
            }
        }
    }
}

The output is:

while (true)
{
    if (true)
    {
        if (true)
        {
            continue;
        }
    }

    local i = 0;

    while (i <= 10)
    {
        if (true)
        {
            continue;
        }
        else if (true)
        {
            if (true)
            {
            }
            else
            {
            }
        }

        i++;
    }
}

The 'for' loop has changed into an infinite loop. I think when pushing states onto the state stack, the loop flags should be cleared:

// While block found - push loop block
BlockState prevBlockState = state.m_BlockState;
state.m_BlockState.inLoop = BlockState::WhileLoop;
state.m_BlockState.loopFlags = 0; // CLEAR THE LOOP FLAGS