Difference between revisions of "Talk:LSL Switch Statement"
(It's do-able) |
|||
Line 1: | Line 1: | ||
Speaking from experience, implementing a ''switch'' statement is within the purview of the compiler's parser and code-generator. To implement such requires only comparison and both conditional and unconditional branching support within the Sim's interpreter. The same applies to ''break'' and ''continue'' statements as well. | Speaking from experience, implementing a ''switch'' statement is within the purview of the compiler's parser and code-generator. To implement such requires only comparison and both conditional and unconditional branching support within the Sim's interpreter. The same applies to ''break'' and ''continue'' statements as well. [[User:Num Skall|Num Skall]] | ||
The VM supports those opcodes already. The bytecode for something like | |||
<pre> | |||
switch (variable) | |||
{ | |||
case condition_1: | |||
a; | |||
break; | |||
case condition_2: | |||
b; | |||
default: | |||
c; | |||
} | |||
</pre> | |||
would be... | |||
<pre> | |||
variable; | |||
DUP | |||
PUSH condition_1 | |||
EQ | |||
JUMPNIF case2 | |||
a; | |||
JUMP end | |||
@case2 | |||
DUP | |||
PUSH condition_2 | |||
EQ | |||
JUMPNIF default | |||
b; | |||
@default | |||
c; | |||
@end | |||
POP | |||
</pre> | |||
An important thing to remember is that there is an extra variable on the stack that needs to be popped off. So if [[return]] or [[state]] are used in a switch statement then the condition needs to be popped off. You can't just keep calling 'variable' as it may be a function that returns a different value for each call. See: [http://www.libsecondlife.org/wiki/LSO] for info on the assembly used. |
Revision as of 20:47, 20 April 2007
Speaking from experience, implementing a switch statement is within the purview of the compiler's parser and code-generator. To implement such requires only comparison and both conditional and unconditional branching support within the Sim's interpreter. The same applies to break and continue statements as well. Num Skall
The VM supports those opcodes already. The bytecode for something like
switch (variable) { case condition_1: a; break; case condition_2: b; default: c; }
would be...
variable; DUP PUSH condition_1 EQ JUMPNIF case2 a; JUMP end @case2 DUP PUSH condition_2 EQ JUMPNIF default b; @default c; @end POP
An important thing to remember is that there is an extra variable on the stack that needs to be popped off. So if return or state are used in a switch statement then the condition needs to be popped off. You can't just keep calling 'variable' as it may be a function that returns a different value for each call. See: [1] for info on the assembly used.