I checked in basic support for switch statements. Switch statements currently work with Int literals and Enums. Eventually I would like support Str literals and Type literals also, but for now I'm just going to implement what I call a "tableswitch" which maps to Java's tableswitch opcode and .NET's switch opcode. The Fan model is closer to .NET - it's a bit more elegant. The fcode looks like this:
u1 Switch opcode
u2 count
u2 jump-0
u2 jump-1
u2 jump-n
So it's basically a simple jump table. Just like .NET it's always zero based, so I do the appropriate math in the compiler ahead of time. The jumps are absolute offsets into the fcode like the other Jump opcodes.
Note that breaks are implicit at the end of case statements - as a matter of fact you can't even include them without a compiler error. However, you can group multiple case statements together:
switch (i)
{
case 0:
case 2: return "even"
case 1:
case 3: return "odd"
default: return "default"
}
andySat 27 May 2006
Looks good - I like it. We definitely need to support Str eventually.
brianFri 20 Oct 2006
The switch statement now supports switching on any expression type. If the switch condition is an Int or Enum and all the cases are int/enum literals, then the compiler will generate an efficient Tableswitch instruction. If the condition is not an Int or Enum or if any of the cases are not literals, then the switch statements will be generated as a series of equality checks using Obj.equals(). The case expressions themselves may be any valid expression. This enables switching on strings or types, plus all sorts of other wacked out things.
brian Sat 27 May 2006
I checked in basic support for switch statements. Switch statements currently work with Int literals and Enums. Eventually I would like support Str literals and Type literals also, but for now I'm just going to implement what I call a "tableswitch" which maps to Java's tableswitch opcode and .NET's switch opcode. The Fan model is closer to .NET - it's a bit more elegant. The fcode looks like this:
So it's basically a simple jump table. Just like .NET it's always zero based, so I do the appropriate math in the compiler ahead of time. The jumps are absolute offsets into the fcode like the other Jump opcodes.
Note that breaks are implicit at the end of case statements - as a matter of fact you can't even include them without a compiler error. However, you can group multiple case statements together:
andy Sat 27 May 2006
Looks good - I like it. We definitely need to support Str eventually.
brian Fri 20 Oct 2006
The switch statement now supports switching on any expression type. If the switch condition is an Int or Enum and all the cases are int/enum literals, then the compiler will generate an efficient Tableswitch instruction. If the condition is not an Int or Enum or if any of the cases are not literals, then the switch statements will be generated as a series of equality checks using Obj.equals(). The case expressions themselves may be any valid expression. This enables switching on strings or types, plus all sorts of other wacked out things.