Page 1 of 1
NXC: enum constants and switch/case
Posted: 01 Nov 2010, 17:26
by mrblp
Hello all,
I wrote a little state machine with the states being of an enum type. Sadly NXC does not accept the enum constants as case constants. May this be changed in a future release?
Code: Select all
enum drive_state_t
{
drive_state_straight = 1,
drive_state_curve = 2,
}
drive_state;
// Initialize state variable
drive_state = drive_state_straight;
[...]
switch (drive_state)
{
// case drive_state_straight:
case 1:
// Straight
SR_Straight(70, FALSE, 20);
drive_state = drive_state_curve;
break;
// case drive_state_curve:
case 2:
// Curve
SR_Curve(70, FALSE, 90);
drive_state = drive_state_straight;
break;
default:
drive_state = drive_state_straight;
break;
}
This code only compiles with the magic numbers. But the enum constants are in fact constants so I do not see why they do not work.
Bye marvin
Re: NXC: enum constants and switch/case
Posted: 01 Nov 2010, 18:10
by physics-matt
Seems to work for me using the enum constants. Your code (suitably modified to remove references to SR_Straight) compiles fine and the following also compiles and runs as expected:
Code: Select all
enum MyEnum { foo=1, bar=2 };
task main()
{
MyEnum a = bar;
switch (a)
{
case foo:
TextOut( 0, LCD_LINE1, "foo" );
break;
case bar:
TextOut( 0, LCD_LINE1, "bar" );
break;
}
while(true)
{}
}
What version of BricxCC are you using? I'm using Build 3.3.8.8 built 29-06-2010
Matt
Re: NXC: enum constants and switch/case
Posted: 01 Nov 2010, 21:02
by mrblp
Hello,
physics-matt wrote:Seems to work for me using the enum constants. Your code (suitably modified to remove references to SR_Straight) compiles fine and the following also compiles and runs as expected:
No, it doesn't
The difference between your code and mine is that the enum is defined globally (your code) vs locally (my code). That is why my code does not work. That makes it even less understandable for me why my code does not work... I think there should be no difference between globally and locally defined enums.
Ciao marvin
Re: NXC: enum constants and switch/case
Posted: 01 Nov 2010, 21:10
by HaWe
hi, what's the difference between
and
?
Re: NXC: enum constants and switch/case
Posted: 02 Nov 2010, 07:41
by physics-matt
mrblp wrote:
The difference between your code and mine is that the enum is defined globally (your code) vs locally (my code).
Sounds like it's a job for John H then.
Is that the only thing that goes wrong when you define the enum locally?
Matt
Re: NXC: enum constants and switch/case
Posted: 02 Nov 2010, 11:08
by spillerrec
I can confirm that it doesn't compile when the enum is declared locally while it do work if it is declared globally. (Using the newest release in the /test_release folder.) Furthermore, the error messages seems to return wrong line numbers.
Code: Select all
Error: Error parsing expression: drive_state_curve
Re: NXC: enum constants and switch/case
Posted: 02 Nov 2010, 14:07
by afanofosc
I will investigate the problem and fix it. Fortunately, it sounds like there is an easy work around (move the enum type declaration).
John Hansen
Re: NXC: enum constants and switch/case
Posted: 03 Nov 2010, 22:00
by afanofosc
The test release zip I uploaded this morning should fix the problem with local enum constants not working in switch case labels. Line number error reporting issues remain, however. Also, the way case labels are implemented is not exactly standard C. Prior to my changes yesterday you could get away with using a global variable name as a case label, for example. You used to be able to have duplicate case labels as well. Now I have some checks in place that enforce the rule that case labels must be constants and that they must be unique, but there are still some things you can do with case labels in C that you can't do in NXC and some things you can do in NXC that you can't do in C.
Please let me know if you encounter any problems with the support for switch statements and case labels in NXC.
John Hansen
Re: NXC: enum constants and switch/case
Posted: 04 Nov 2010, 17:46
by mrblp
Hello John,
afanofosc wrote:The test release zip I uploaded this morning should fix the problem with local enum constants not working in switch case labels.
Now it works as expected by me
Thanks a lot
Ciao marvin