huderlem / poryscript

High-level scripting language for gen 3 pokemon decompilation projects
https://www.huderlem.com/poryscript-playground/
MIT License
191 stars 21 forks source link

Empty switch case statements produce unexpected output #53

Closed tustin2121 closed 1 year ago

tustin2121 commented 1 year ago

If the user has a script with only a switch statement, with a case statement that has no commands, poryscript does not throw an error and produces undesirable results. It does not write the implied return command at the end of the function, causing unexpected control flow. See the following example:

Input:

script StoryPart3_SetupSubquests {
    call(FunctionCall)
}
script FunctionCall {
    switch (var(VAR_TEMP_1)) {
        case 10:
    }
}
script StoryPart3_NapTransition {
    msgbox("Shouldn't get here")
}

Expected Output:

StoryPart3_SetupSubquests::
    call FunctionCall
    return

FunctionCall::
    switch VAR_TEMP_1  @ optionally optimize this line away
        return

StoryPart3_NapTransition::
    msgbox StoryPart3_NapTransition_Text_0
    return

StoryPart3_NapTransition_Text_0:
    .string "Shouldn't get here$"

Actual Output:

StoryPart3_SetupSubquests::
    call FunctionCall
    return

FunctionCall::
    switch VAR_TEMP_1

StoryPart3_NapTransition::
    msgbox StoryPart3_NapTransition_Text_0
    return

StoryPart3_NapTransition_Text_0:
    .string "Shouldn't get here$"

If StoryPart3_SetupSubquests gets run, instead of calling FunctionCall and returning while doing nothing, control flow instead ends up at the script below it, as there's no return statement at the bottom of the outputted FunctionCall script.

huderlem commented 1 year ago

Fixed by fd03793f9cd2d61e1bd893253b8eedbbdaed56ee