Skip to content

Commit

Permalink
Allow all empty cases in a switch statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
LadyCailin committed Mar 9, 2016
1 parent 05c1adb commit 177b7d5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/laytonsmith/core/functions/BasicLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public ExampleScript[] examples() throws ConfigCompileException {

@Override
public ParseTree optimizeDynamic(Target t, List<ParseTree> children, FileOptions fileOptions) throws ConfigCompileException, ConfigRuntimeException {
if (children.get(1).getData() instanceof CFunction
if (children.size() > 1 && children.get(1).getData() instanceof CFunction
&& new StringHandling.sconcat().getName().equals(children.get(1).getData().val())) {
//This is the brace/case/default usage of switch, probably. We need
//to refactor the data into the old switch format.
Expand Down Expand Up @@ -675,10 +675,10 @@ public int compare(Construct t, Construct t1) {
}
}

if ((children.size() > 3 || children.get(1).getData() instanceof CArray)
if ((children.size() > 3 || (children.size() > 1 && children.get(1).getData() instanceof CArray))
//No point in doing this optimization if there are only 3 args and the case is flat.
//Also, doing this check prevents an inifinite loop during optimization.
&& !children.get(0).getData().isDynamic()) {
&& (children.size() > 0 && !children.get(0).getData().isDynamic())) {
ParseTree toReturn = null;
//The item passed in is constant (or has otherwise been made constant)
//so we can go ahead and condense this down to the single code path
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/laytonsmith/core/OptimizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ public void testSwitch2() throws Exception {
+ "}");
}

@Test
public void testEmptySwitch() throws Exception {
assertEquals("switch(dyn(1))", optimize("switch(dyn(1)){ case 1: case 2: default: }"));
}

// Tests "-" signs in front of values to negate them.
@Test public void testMinusWithoutValueInFront() throws Exception{
assertEquals("assign(@b,neg(@a))", optimize("@b = -@a"));
Expand Down

0 comments on commit 177b7d5

Please sign in to comment.