Home AMX User Forum AMX General Discussion

SWITCH CASE compile error

Please explain why I get a compile error at the last DATA_EVENT.

//
Starting NetLinx Compile - Version[2.5.2.420] [10-06-2015 11:55:32]
// ERROR: (0): C10580: Internal Error: Major system error occurred during code generation



DATA_EVENT[dvDEVICE] // this works fine
{
ONLINE: { }
STRING:
{
IF(LENGTH_ARRAY(DATA.TEXT)>0)
{
STACK_VAR CHAR cCMD[20];
cCMD = DATA.TEXT;
SWITCH(cCMD)
{
CASE '01': { }
CASE '02': { }
CASE '03': { }
CASE '04': { }
CASE '05': { }
CASE '06': { }
CASE '07': { }
CASE '08': { }
CASE '09': { }
CASE '10': { }
}
}
}
COMMAND: { }
}


DATA_EVENT[dvDEVICE] // this also works fine
{
ONLINE: { }
STRING:
{
IF(LENGTH_ARRAY(DATA.TEXT)>0)
{
STACK_VAR CHAR cCMD[20];
cCMD = DATA.TEXT;
SWITCH(cCMD)
{
CASE 'n1': { }
CASE 'n2': { }
CASE 'n3': { }
CASE 'n4': { }
CASE 'n5': { }
CASE 'n6': { }
CASE 'n7': { }
CASE 'n8': { }
CASE 'n9': { }
CASE 'n10': { }
}
}
}
COMMAND: { }
}



DATA_EVENT[dvDEVICE] // compile error!!!
{
ONLINE: { }
STRING:
{
IF(LENGTH_ARRAY(DATA.TEXT)>0)
{
STACK_VAR CHAR cCMD[20];
cCMD = DATA.TEXT;
SWITCH(cCMD)
{
CASE '1': { }
CASE '2': { }
CASE '3': { }
CASE '4': { }
CASE '5': { }
CASE '6': { }
CASE '7': { }
CASE '8': { }
CASE '9': { }
CASE '10': { } // remove this to remove the error
}
}
}
COMMAND: { }
}


Comments

  • Did you try to put case '10' at the first place ?
  • tdewildtdewild Posts: 49
    SWITCH(cCMD) // this works fine
    {
    CASE '10': { }
    CASE '1': { }
    CASE '2': { }
    CASE '3': { }
    CASE '4': { }
    CASE '5': { }
    CASE '6': { }
    CASE '7': { }
    CASE '8': { }
    CASE '9': { }
    }

    SWITCH(cCMD) // this works fine
    {
    CASE '10': { }
    CASE '11': { }
    CASE '1': { }
    CASE '2': { }
    CASE '3': { }
    CASE '4': { }
    CASE '5': { }
    CASE '6': { }
    CASE '7': { }
    CASE '8': { }
    CASE '9': { }
    }


    SWITCH(cCMD) // this works fine
    {
    CASE '01': { }
    CASE '02': { }
    CASE '03': { }
    CASE '04': { }
    CASE '05': { }
    CASE '06': { }
    CASE '07': { }
    CASE '08': { }
    CASE '09': { }
    CASE 'NEXT': { }
    }

    SWITCH(cCMD) // SAME COMPILE ERROR
    {
    CASE '10': { }
    CASE '1': { }
    CASE '2': { }
    CASE '3': { }
    CASE '4': { }
    CASE '5': { }
    CASE '6': { }
    CASE '7': { }
    CASE '8': { }
    CASE '9': { }
    CASE 'NEXT': { }
    }

    SWITCH(cCMD) // SAME COMPILE ERROR
    {
    CASE '1': { }
    CASE '2': { }
    CASE '3': { }
    CASE '4': { }
    CASE '5': { }
    CASE '6': { }
    CASE '7': { }
    CASE '8': { }
    CASE '9': { }
    CASE 'NEXT': { }
    }
  • I have already seen this issue, just have to guess in which order to set the case in order it works fine.
    I suppose it is the same for next if you put it at the first place it may work.
    But I have no idea why...
  • RaphayoRaphayo Posts: 111
    Strange question but If you create constant with your value does it compiled?
  • sling100sling100 Posts: 123
    There is a known issue with single char SWITCH/CASE.

    If you put the '10' (2 chars) and 'NEXT' (4 chars) first it will work, but not if you put one of them after a single char CASE. I don't know why - it's caught me out before when trying to trap both '0' and 'false'.

    Simon
  • tdewildtdewild Posts: 49
    I did not know this, no Tech Note about it.

    I was lucky it happend in a small piece of code, easy to find what happend but not why.

    Modify the code so it uses a SELECT/ACTIVE statement instead could be the answer from tech support ;)

    So be ware of a single char in a SWITCH/CASE, a integer will work fine.

    CASE closed.
  • RaphayoRaphayo Posts: 111
    if you place the case with the larger length first in you switch()case, it will compile.
    DATA_EVENT[dvDEVICE] // compile error!!!
    {
    ONLINE: { } 
    STRING:
    { 
    IF(LENGTH_ARRAY(DATA.TEXT)>0)
    {
    STACK_VAR CHAR cCMD[20]; 
    cCMD = DATA.TEXT; 
    SWITCH(cCMD)
    {
    case 'NEXT':{}
    CASE '10': { } // remove this to remove the error
    CASE '1': { }
    CASE '2': { }
    CASE '3': { }
    CASE '4': { }
    CASE '5': { }
    CASE '6': { }
    CASE '7': { }
    CASE '8': { }
    CASE '9': { }
    //case ten:{}
    } 
    } 
    }
    COMMAND: { } 
    }
    
  • a_riot42a_riot42 Posts: 1,624
    Another switch statement oddity, is putting the default case somewhere else rather than at the end. You should be able to do this, and it does compile, but doesn't work as expected.
    Paul
  • viningvining Posts: 4,368
    a_riot42 wrote: »
    Another switch statement oddity, is putting the default case somewhere else rather than at the end. You should be able to do this, and it does compile, but doesn't work as expected.
    Paul
    I would expect it to ignore any cases below the default. I view it as an elevator that always starts at the top floor, the elevator goes down and you have to get off at the 1st open door/floor and "default:" is a door that can't be closed so you have to get off when you get to it. If you put the default at the top of the building you'll never get below it unless you take the stairs.
  • a_riot42a_riot42 Posts: 1,624
    vining wrote: »
    I would expect it to ignore any cases below the default. I view it as an elevator that always starts at the top floor, the elevator goes down and you have to get off at the 1st open door/floor and "default:" is a door that can't be closed so you have to get off when you get to it. If you put the default at the top of the building you'll never get below it unless you take the stairs.

    Well I think of it such that default is the case that is executed when all cases are evaluated but none match. Where it is in the statement shouldn't really matter. Its called the default case, not the 'final' case. In fact, in some cases (no pun intended), I would prefer it to be first case since the default will be the most commonly executed case. IE:
    switch(iButton)
    {
      case default:
      {
        beep()
      }
      case 666:
      {
        summonSatan()
      }
    }
    
  • mushmush Posts: 287
    My understanding is that a switch case statement is internally similar to if, else if & else.
    Therefore, putting default first is like putting else first.
  • a_riot42a_riot42 Posts: 1,624
    mush wrote: »
    My understanding is that a switch case statement is internally similar to if, else if & else.
    Therefore, putting default first is like putting else first.

    I don't think so. Putting an else first doesn't compile, obviously. But the compiler allows you to put the default case anywhere in a switch statement. If its not going to work at runtime, then the compiler shouldn't allow it. In other languages it works fine in any position. Besides, how the compiler wants to implement it should have no bearing on if it works as intended. There really is no reason that these two button events should have different results yet compile without issue, no warnings, no errors, etc. The help file states the default case has to come last, yet the compiler doesn't enforce it.
    button_event[vdvTest, 1]
    {
        push:
        {
            stack_var integer i
            
            switch(i)
            {
                default:{}
                case 1:{}
            }
    }
    
    
    button_event[vdvTest, 1]
    {
        push:
        {
            stack_var integer i
            
            switch(i)
            {
                case 1:{}
                default:{}
            }
    }
    
Sign In or Register to comment.