New feature: switch statements

Status
Not open for further replies.

Plombo

OpenBOR Developer
Staff member
The engine now supports switch statements like in C.  I don't feel like explaining switch statements right now for those unaware of what they are, so look them up.

Like in C, the switch expression and case labels must both be integers.  The case labels must be integer constants, not constant expressions.

Maybe I should make it also work with strings.  That would be useful.

EDIT 2/8/2013: It works with strings now.
 
Very nice.

Maybe I should make it also work with strings.  That would be useful.
If it is just a syntax sugar for something like if else,  I'd say make string constant available. :)
 
utunnels said:
Maybe I should make it also work with strings.  That would be useful.
If it is just a syntax sugar for something like if else,  I'd say make string constant available. :)

It's only syntax sugar for if/else to the extent that it is in most C-like languages.  I think the actual code it emits is a bit more efficient in terms of instruction count than a complex if/else if block.  From an actual instruction perspective, the example switch statement below would be compiled into something like the script instruction list under it:
Code:
switch(i) {
case 3: var="3"; break;
case 7: var="7";
case 8: var2=9;
}
Code:
LOAD i
CONSTINT 3
Branch_EQUAL L7
CONSTINT 7
Branch_EQUAL L8
CONSTINT 8
Branch_EQUAL L9
PJUMP endLabel [or location of "default" if it is defined]
PUSH

L7: NOOP
CONSTSTR "3"
SAVE var
JUMP endLabel
L8: NOOP
CONSTSTR "7"
SAVE var
JUMP endLabel
L9: NOOP
CONSTINT 9
SAVE var2

endLabel: POP

Branch_EQUAL is a new opcode that branches if the top two stack entries are equal, but only pops the top one since it would be counterproductive to have to repeatedly push the result of the switch expression onto the stack.  PJUMP is just like a JUMP, but it also pops the top entry from the data stack (the switch expression result).

Anyway, with the way the script engine works, it should be fairly easy to extend it to strings.  All that's needed is to come up with a better way of sending the case values from Parser_Case_label to Parser_Select_stmt, since currently the jump instructions are generated from a List where each name is the jump target label and the value is the token source for the integer constant.  String support will require including the type of constant along with that information.

But the main reason it doesn't support strings yet is that I didn't think of it until after the implementation was finished. ;)
 
Maybe it's time to bring stack back for compiled script. As you can see, now it is opcodes which hold values (which should be on a stack). It was a desperate optimization for speed. But now scripts are more and more complex, so are those bugs (for example, call an animation script within itself).

My idea is giving each value an index instead of an address, for example, load opcode has its index relative to the stack top of current scope.

It is actaually off-topic, but maybe you can leave some space to such changes if you feel necessary.
;D

 
Awesome sause! I've wanted switch statements for a long time. Great work as always.

DC
 
http://www.chronocrash.com/forum/index.php?action=tpmod;dl=item82


Edit* Please remove the pervious link. There was a bug in that build.

http://www.chronocrash.com/forum/index.php?action=tpmod;dl=item83

Download link for this new feature.
 
Status
Not open for further replies.
Back
Top Bottom