Skip to content

Commit

Permalink
Add support for using enums in case statement
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 authored and ForNeVeR committed Sep 2, 2024
1 parent 18d6d82 commit c13819f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenEnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,17 @@ void test()
{
work(Green);
}");

[Fact]
public Task EnumInCase() => DoTest(@"
enum Colour { Red, Green, Blue };
void test()
{
enum Colour x = 42;
switch (x) {
case Blue:
break;
}
}");
}
2 changes: 1 addition & 1 deletion Cesium.CodeGen.Tests/CodeGenSwitchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void NonConstEval() => DoesNotCompile(@"int main()
int x = 0;
switch(x) { case 1 + x: break; };
return 1;
}", "Expression Cesium.CodeGen.Ir.Expressions.IdentifierExpression cannot be evaluated as constant expression.");
}", "Expression Cesium.CodeGen.Ir.Expressions.GetValueExpression cannot be evaluated as constant expression.");

[Fact]
public Task MultiCases() => DoTest(@"int main()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
System.Void <Module>::test()
Locals:
System.Int32 V_0
System.Int32 V_1
IL_0000: ldc.i4.s 42
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: stloc.1
IL_0005: ldloc.1
IL_0006: ldc.i4.2
IL_0007: ceq
IL_0009: brfalse IL_0013
IL_000e: br IL_0019
IL_0013: nop
IL_0014: br IL_001f
IL_0019: nop
IL_001a: br IL_001f
IL_001f: nop
IL_0020: ret
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Lowering/BlockItemLowering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void ProcessCaseStatement(CaseStatement caseStatement, BlockScope switchBlock, s
{
if (caseStatement.Expression != null)
{
var constValue = ConstantEvaluator.GetConstantValue(caseStatement.Expression);
var constValue = ConstantEvaluator.GetConstantValue(caseStatement.Expression.Lower(switchBlock));
switchBlock.SwitchCases.Add(new SwitchCase(new ConstantLiteralExpression(constValue), label));
}
else
Expand Down

0 comments on commit c13819f

Please sign in to comment.