Skip to content

Proposal: Bringing preprocessor in line with Firestorm: "switch case" #38

@Nensec

Description

@Nensec

This stems from #32

In order to keep compatibility with existing code of people having used the well established Firestorm viewer build-in preprocessor it is important to support its features, or at least its syntax. One such syntax is the ability to make switch cases. Whilst there isn't much of any optimization to be done, I think, if people do use this syntax they would have to rewrite it in order to use this preprocessor.

In my experience what this ends up translating to is if...else if but instead of having the code blocks inside of if block they have put jump and the actual code is outside of the if...else if block, defined by a (randomly generated) label that the jump would use. I don't quite see a reason why this would not just translate to putting the code directly into the blocks themselves, but they probably had a reason to do it this way. Probably to support fallthrough into the next case?

Their wiki has the following example:

default
{
    state_entry()
    {
        integer i;
        switch(i)
        {
            case 1:
            {
                llOwnerSay("1");
                // fallthrough to case 2
            }
            case 2:
            {
                llOwnerSay("1 or 2");
                // no fallthrough
                break;
            }
            case 3:
            {
                llOwnerSay("3");
                // fallthrough to default
            }
            default:
            {
                llOwnerSay("3 or default");
            }
        }
    }
}

Note that the colon ':' after default or case is not needed if a block (an opening curly brace { introducing a series of statements) comes immediately next. For example:

switch(x)
{
    case 1: // needs colon
    case 2  // colon optional as curly brace opens next
        {
            llOwnerSay("x is 1 or 2");
            break;
        }
    default  // colon optional as curly brace opens next
        {
            llOwnerSay("x is neither 1 nor 2");
        }
}

For completeness sake, I put their first example into a script to give you the example output (note, I cleaned up the output for readability, there are mis-aligned indentations and additional newlines normally present in the translated output):

state_entry()
{
    integer i;
    {
        if((i) == (1))jump cnmkaa;
        if((i) == (2))jump cO1yR6;
        if((i) == (3))jump cD4fcZ;
        jump cuvXAt;
        @cnmkaa;
        {
            llOwnerSay("1");                
        }
        @cO1yR6;
        {
            llOwnerSay("1 or 2");                
            jump cEBGBp;
        }
        @cD4fcZ;
        {
            llOwnerSay("3");                
        }
        @cuvXAt;
        {
            llOwnerSay("3 or default");
        }
        @cEBGBp;
    }
}

For more information see: https://wiki.firestormviewer.org/fs_preprocessor#switch_case_addition

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions