Compiler error with Select Case

A private (members-only) forum for discussing all issues related to the Beta test of Native mode devices.
Locked
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Compiler error with Select Case

Post by Don_Kirby »

The compiler issues a warning for this code:

Code: Select all

					Select Case DataInputNumber
						Case 110 OR 300 OR 600 OR 1200 OR 2400 OR _
						4800 OR	9600 OR 14400 OR 19200 OR 28800 OR _
						38400 OR 56000 OR 57600 OR 115200
							ComBaud = DataInputNumber
					End Select
The error is
RunSetup.c:1921: warning: suggest parentheses around comparison in operand of |
The corresponding C code is

Code: Select all

					{
						int32_t _zv_selectValTemp12;

						_zv_selectValTemp12 = zv_DataInputNumber;
						if (_zv_selectValTemp12 == ((((((((((((110L | 300L) | 600L) | 1200L) | 2400L) | 4800L) | 9600L) | 14400L) | 19200L) | 28800L) | 38400L) | 56000L) | 57600L) | 115200L)
						{
The resulting compiled code does not function correctly, the case statement evaluates true on all values.

Is there a limit to how many possible values there can be for a Case statement? The reasoning behind the single case select statement is simply a matter of readability. See this thread for more information.

-Don
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

The ZBasic compiler did the correct thing according to the code you provided. A case statement can contain an expression and yours is one huge ORed expression. Instead the syntax you need is as follows:

Code: Select all

Select Case DataInputNumber
    Case Is = 110, Is = 300, Is= 600, Is = 1200, Is = 2400,  _
            Is = 4800, Is =  9600, Is = 14400, Is = 19200, Is = 28800, _
            Is = 38400, Is = 56000, Is = 57600, Is = 115200
        ComBaud = DataInputNumber
    Case Else
       ComBaud = DEFAULT_BAUD_DATE
End Select
I also added the selection for a default baud rate if that is needed. The resultant C code is as you would expect with a set of ORed conditions:

Code: Select all

   	if (ZX_BOOL(_zv_selectValTemp01 == 110) |
				ZX_BOOL(_zv_selectValTemp01 == 300) |
				ZX_BOOL(_zv_selectValTemp01 == 600) |
etc
Mike Perks
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Post by Don_Kirby »

mikep wrote:The ZBasic compiler did the correct thing according to the code you provided.
While this may technically be true, it seems odd that the compiler would issue a warning about code itself generated. Now, if I wrote the C code, it would make sense that the warning should point out the improper usage of the case statement. In this case though, shouldn't the preprocessor give the warning (i.e. pointing the user to the ZBasic code rather than the C code)?

On the other hand, I think that the backend compile process has no way of determining the source of the C code it is trying to compile, in which case it cannot possibly know who wrote the faulty code.

In either case, thanks for the insight on the correct usage of the case statement. I'll check the rest of my code for similar errors.


-Don
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Don_Kirby wrote:t seems odd that the compiler would issue a warning about code itself generated.
You've unearthed a situation where the generated code isn't quite what it should be. There should be an additional set of parentheses around the right hand operand of the == operator.

As to the original ZBasic code, this is a situation where the code you wrote is syntactically correct but doesn't accomplish what you intended.

The variation of Mike's suggestion shown below performs identically; the Is = is superfluous.

Code: Select all

Select Case DataInputNumber 
    Case 110, 300, 600, 1200, 2400, 4800, 9600, 14400, _
            19200, 28800, 38400, 56000, 57600, 115200 
      ComBaud = DataInputNumber 
    Case Else 
      ComBaud = DEFAULT_BAUD_RATE 
End Select
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

dkinzer wrote:The variation of Mike's suggestion shown below performs identically; the Is = is superfluous.

Code: Select all

Select Case DataInputNumber 
    Case 110, 300, 600, 1200, 2400, 4800, 9600, 14400, _
            19200, 28800, 38400, 56000, 57600, 115200 
      ComBaud = DataInputNumber 
    Case Else 
      ComBaud = DEFAULT_BAUD_RATE 
End Select
Yup I went a little overboard this morning :)
Mike Perks
Locked