Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elseif and switch Support #160

Open
HzanRsxa2959 opened this issue Jul 24, 2021 · 9 comments
Open

elseif and switch Support #160

HzanRsxa2959 opened this issue Jul 24, 2021 · 9 comments

Comments

@HzanRsxa2959
Copy link

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.

@x87 x87 added this to the Language Specification milestone Jul 25, 2021
@MatiDragon-YT
Copy link
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

@MatiDragon-YT
Copy link
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

@XMDS
Copy link

XMDS commented Jul 27, 2021

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

@MatiDragon-YT
Copy link
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 :(

@wmysterio
Copy link

wmysterio commented Jul 28, 2021

@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.

@MatiDragon-YT
Copy link
Member

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

Exactly @wmysterio

@OrionSR
Copy link

OrionSR commented Sep 6, 2023

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

@x87
Copy link
Collaborator

x87 commented Nov 21, 2023

// 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

@x87 x87 mentioned this issue Nov 22, 2023
59 tasks
@MiranDMC
Copy link

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
Projects
None yet
Development

No branches or pull requests

7 participants