Skip to content

elseif and switch Support #160

Open
Open

Description

@HzanRsxa2959

If elseif support is added first, then switch support could be derived from that.
Otherwise switch support could be added using opcodes 0871 and 0872 (only support integers, though).
GTA3Script already has nested switch support using opcodes 0871 and 0872.

Activity

  1. added this to the Language Specification milestone on Jul 25, 2021
  2. MatiDragon-YT commented on Jul 26, 2021

    @MatiDragon-YT
    Member

    Maybe it is better with conditional jump opcodes

    switch 0@
        case 1
            [...]
        end
        case "2"
            [...]
        end
        case Actor.IsDead()
            [...]
        case 0x4
            [...]
        end
        default
            [...]
        end
    end
    

    TO COMPILE AND DECOMPILE ⬇

    :LABEL_1X // begin SWITH
    XXXX: if
    XXXX: 0@ == 1  // CASE 1
    XXXX: jf @LABEL_2X
    [...]
    XXXX: goto @LABEL_7 // exit SWITH
    :LABEL_2X
    XXXX: if
    XXXX: 0@ == "2"  // CASE 2
    XXXX: jf @LABEL_3X
    [...]
    XXXX: goto @LABEL_7 // exit SWITH
    :LABEL_3X
    XXXX: if
    XXXX: actor 0@ is_dead  // CASE 3
    XXXX: jf @LABEL_5X
    [...]
    :LABEL_4X
    XXXX: if
    XXXX: 0@ == 0x4  // CASE 4
    XXXX: jf @LABEL_5X
    [...]
    XXXX: goto @LABEL_7X // exit SWITH
    :LABEL_5X
    [...]          // DEFAULT
    XXXX: goto @LABEL_7X
    :LABEL_7X // end SWITH
    
  3. MatiDragon-YT commented on Jul 26, 2021

    @MatiDragon-YT
    Member

    The same I think could be applied to ELSE-IF

    if 0@ == 1
        [...]
    else if 0@ == "5"
        [...]
    else
        [...]
    end
    

    TO COMPILE AND DECOMPILE ⬇

    :LABEL_1X // begin ELSE-IF
    XXXX: if
    XXXX: 0@ == 1   // if
    XXXX: jf @LABEL_2X
    [...]
    XXXX: goto @LABEL_4X // exit ELSE-IF
    :LABEL_2X
    XXXX: if
    XXXX: 0@ == "5"   // else if
    XXXX: jf @LABEL_3X
    [...]
    XXXX: goto @LABEL_4X // exit ELSE-IF
    :LABEL_3X
    [...]           // else
    :LABEL_4X // end ELSE-IF
    
  4. XMDS commented on Jul 27, 2021

    @XMDS
    Member

    Maybe it is better with conditional jump opcodes

    switch 0@
        case 1
            [...]
        end
        case "2"
            [...]
        end
        case Actor.IsDead()
            [...]
        case 0x4
            [...]
        end
        default
            [...]
        end
    end
    

    TO COMPILE AND DECOMPILE ⬇

    :LABEL_1X // begin SWITH
    XXXX: if
    XXXX: 0@ == 1  // CASE 1
    XXXX: jf @LABEL_2X
    [...]
    XXXX: goto @LABEL_7 // exit SWITH
    :LABEL_2X
    XXXX: if
    XXXX: 0@ == "2"  // CASE 2
    XXXX: jf @LABEL_3X
    [...]
    XXXX: goto @LABEL_7 // exit SWITH
    :LABEL_3X
    XXXX: if
    XXXX: actor 0@ is_dead  // CASE 3
    XXXX: jf @LABEL_5X
    [...]
    :LABEL_4X
    XXXX: if
    XXXX: 0@ == 0x4  // CASE 4
    XXXX: jf @LABEL_5X
    [...]
    XXXX: goto @LABEL_7X // exit SWITH
    :LABEL_5X
    [...]          // DEFAULT
    XXXX: goto @LABEL_7X
    :LABEL_7X // end SWITH
    

    It is best for SA to give priority to 0871 and 0872.
    Conditional opcodes need to be distinguished,
    But the latter is more suitable for VC and 3. Because VC and 3 do not have these commands

  5. MatiDragon-YT commented on Jul 27, 2021

    @MatiDragon-YT
    Member

    0871 & 0872 could be used in the case that the SWITCH only contains INTERGERS and if it is also the same as the one with which it is compared.
    You couldn't tell if one number is greater than another or something else :(

  6. wmysterio commented on Jul 28, 2021

    @wmysterio

    @MatiDragon-YT

    0871 & 0872 could be used in the case that the SWITCH only contains INTERGERS and if it is also the same as the one with which it is compared.
    You couldn't tell if one number is greater than another or something else :(

    It looks more like "expressions (mapping)" than a classic "switch". However, design compatibility across all games would be a priority for me over a limited case jump table.

  7. MatiDragon-YT commented on Jul 29, 2021

    @MatiDragon-YT
    Member

    It looks more like "expressions (mapping)" than a classic "switch".

    Exactly @wmysterio

  8. OrionSR commented on Sep 6, 2023

    @OrionSR

    Two examples of the switch construct as used in the original source file, Girls.sc - the 2nd example includes a default_jump [true] for when no cases match the key variable:

    /********************************************
    GET CURRENT GIRLFRIEND ORIGIN XYZ & HEADING
    ********************************************/
    GF_Dating_Agent_GetOrigin_X_Y_Z_H:
    	SWITCH iGFidx
    
    		CASE COOCHIE 
    			fOriginX 	= 2401.9646
    			fOriginY 	= -1723.2197
    			fOriginZ 	= 12.6005
    			fOriginH	= 180.0		    
    		BREAK
    
    		CASE MICHELLE
    			fOriginX 	= -1799.5 
    			fOriginY 	= 1195.5 
    			fOriginZ 	= 24.1094 
    			fOriginH	= 180.0
    		BREAK
    
    		CASE KYLIE
    			fOriginX 	= -377.3978 //-398.5166    89.8377
    			fOriginY 	= -1438.6919 //-1426.4675 
    			fOriginZ 	= 24.7209 //24.7110 
    			fOriginH	= 270.0 //90.0
    		BREAK
    
    		CASE BARBARA
    			fOriginX 	= -1398.1010 
    			fOriginY 	= 2636.8730 
    			fOriginZ 	= 54.7031 
    			fOriginH	= 75.0	
    		BREAK
    
    		CASE KATIE
    			fOriginX 	= -2576.8079 
    			fOriginY 	= 1144.7438 
    			fOriginZ 	= 54.7422
    			fOriginH	= 164.0
    		BREAK
    
    		CASE MILLIE
    			fOriginX = 2035.3619  
    			fOriginY = 2732.4106  
    			fOriginZ = 9.8203
    			fOriginH = 0.0			
    		BREAK
    
    	ENDSWITCH
    RETURN
    /********************************************
    	GET GF MEETING ORIGIN XYZ & HEADING
    ********************************************/
    GF_Dating_Agent_GetMeetingOrigin_X_Y_Z_H:
    	SWITCH iGFidx
    
    		CASE MICHELLE
    			fOriginX 	= -2027.5835 
    			fOriginY 	= -118.7022 
    			fOriginZ 	= 1034.0 
    			fOriginH	= 3.4
    		BREAK
    
    		CASE KYLIE
    			fOriginX 	= 257.6682 
    			fOriginY 	= -154.7475 
    			fOriginZ 	= 4.0786 
    			fOriginH	= 268.7731
    		BREAK
    
    		CASE BARBARA
    			fOriginX 	= -1410.0997 
    			fOriginY 	= 2648.7307 
    			fOriginZ 	= 54.6875
    			fOriginH	= 130.0
    		BREAK
    
    		CASE KATIE // commented the co-ords inside the gym, temporarily set her in a park nearby
    			fOriginX 	= -2291.7075 //762.9347 
    			fOriginY 	= -233.0215 //-23.3127 
    			fOriginZ 	= 41.7185 //999.5938 
    			fOriginH	= 360.0 //360.0
    		BREAK
    
    		DEFAULT
    			fOriginX 	= 0.0 
    			fOriginY 	= 0.0 
    			fOriginZ 	= 0.0 
    			fOriginH	= 0.0
    		BREAK
    
    	ENDSWITCH
    RETURN
    
  9. x87 commented on Nov 21, 2023

    @x87
    Collaborator
    // switch on int value
    switch 0@
    
       case 1, 2, 3
        // do something if 0@ == 1 or 2 or 3
    
       case 5
        // do something if 0@ == 5
    
       default // optional
    
       // do something otherwise
    
    end
    
    // switch on string value
    switch 0@v
    
       case "foo"
        // do something if 0@s == "foo"
    
       case "bar"
        // do something if 0@s == "bar"
    
    end
    
  10. MiranDMC commented on Jul 23, 2024

    @MiranDMC

    Switch is implemented. Any chance for elif?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions