NXC: enum constants and switch/case

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

NXC: enum constants and switch/case

Post 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
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
physics-matt
Posts: 76
Joined: 29 Sep 2010, 06:57

Re: NXC: enum constants and switch/case

Post 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
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

Re: NXC: enum constants and switch/case

Post 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
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: enum constants and switch/case

Post by HaWe »

hi, what's the difference between

Code: Select all

enum MyEnum { foo=1, bar=2 };
and

Code: Select all

#define foo=1;
#define bar=2;
?
physics-matt
Posts: 76
Joined: 29 Sep 2010, 06:57

Re: NXC: enum constants and switch/case

Post 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
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: enum constants and switch/case

Post 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
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: enum constants and switch/case

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: enum constants and switch/case

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

Re: NXC: enum constants and switch/case

Post 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
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests