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

Switch default cases are not handled correctly #29

Closed tustin2121 closed 4 years ago

tustin2121 commented 4 years ago

Something appears to be wrong with switch parsing, such that the output code involving a default case is downright wrong. It seems like it is not handling the default case as a normal "case" like you'd expect from a higher level language with such a concept, but as something separate entirely. Either this should be fixed or the behavior should be made plain in the documentation.

Found in an earlier version, verified in the playground.


Input code:

script(local) Puzzle_IlexForest_BirdSpotD6 {
    switch(var(VAR_FACING)) {
            default:
            case DIR_NORTH:
            case DIR_EAST:
            ilex_runto(D4, Puzzle_IlexForest_Movement_D6_D4)
        case DIR_WEST:
            ilex_runto(DtoE, Puzzle_IlexForest_Movement_D6_DtoE)
    }
}

Expected output (unoptimised):

Puzzle_IlexForest_BirdSpotD6:
    switch VAR_FACING
    case DIR_NORTH, Puzzle_IlexForest_BirdSpotD6_2
    case DIR_EAST, Puzzle_IlexForest_BirdSpotD6_2
    case DIR_WEST, Puzzle_IlexForest_BirdSpotD6_3
    goto Puzzle_IlexForest_BirdSpotD6_2

Puzzle_IlexForest_BirdSpotD6_2:
    ilex_runto D4, Puzzle_IlexForest_Movement_D6_D4
    return

Puzzle_IlexForest_BirdSpotD6_3:
    ilex_runto DtoE, Puzzle_IlexForest_Movement_D6_DtoE
    return

Actual output (unoptimised):

Puzzle_IlexForest_BirdSpotD6:
    switch VAR_FACING
    case DIR_NORTH, Puzzle_IlexForest_BirdSpotD6_2
    case DIR_EAST, Puzzle_IlexForest_BirdSpotD6_2
    case DIR_WEST, Puzzle_IlexForest_BirdSpotD6_3
    goto Puzzle_IlexForest_BirdSpotD6_4

Puzzle_IlexForest_BirdSpotD6_2:
    ilex_runto D4, Puzzle_IlexForest_Movement_D6_D4
    return

Puzzle_IlexForest_BirdSpotD6_3:
    ilex_runto DtoE, Puzzle_IlexForest_Movement_D6_DtoE
    return

Puzzle_IlexForest_BirdSpotD6_4: @ EMPTY case!
    return

Input code:

script(local) Puzzle_IlexForest_BirdSpotD6 {
    switch(var(VAR_FACING)) {
            case DIR_EAST:
            default:
            ilex_runto(D4, Puzzle_IlexForest_Movement_D6_D4)
        case DIR_WEST:
            ilex_runto(DtoE, Puzzle_IlexForest_Movement_D6_DtoE)
    }
}

Expected output (unoptimised):

Puzzle_IlexForest_BirdSpotD6:
    switch VAR_FACING
    case DIR_EAST, Puzzle_IlexForest_BirdSpotD6_3
    case DIR_WEST, Puzzle_IlexForest_BirdSpotD6_2
    goto Puzzle_IlexForest_BirdSpotD6_3

Puzzle_IlexForest_BirdSpotD6_2:
    ilex_runto DtoE, Puzzle_IlexForest_Movement_D6_DtoE
    return

Puzzle_IlexForest_BirdSpotD6_3:
    ilex_runto D4, Puzzle_IlexForest_Movement_D6_D4
    return

Actual output (unoptimised):

Puzzle_IlexForest_BirdSpotD6:
    switch VAR_FACING
    case DIR_EAST, Puzzle_IlexForest_BirdSpotD6_2  @ Downright WRONG
    case DIR_WEST, Puzzle_IlexForest_BirdSpotD6_2
    goto Puzzle_IlexForest_BirdSpotD6_3

Puzzle_IlexForest_BirdSpotD6_2:
    ilex_runto DtoE, Puzzle_IlexForest_Movement_D6_DtoE
    return

Puzzle_IlexForest_BirdSpotD6_3:
    ilex_runto D4, Puzzle_IlexForest_Movement_D6_D4
    return
huderlem commented 4 years ago

Good catch. I don't have any tests for this type of default usage, but I do remember taking this into account when it was implemented. Either way, I'll look into it this weekend.

huderlem commented 4 years ago

Fixed in 4b0e526111bd74d15ca50098ae057d1b199251c3

Available in Release 2.8.1

huderlem commented 4 years ago

@tustin2121 The playground is also updated, if you want to check that out.