question

Serge A avatar image
0 Likes"
Serge A asked Serge A commented

Parantheses in switch statement in Flexscript

I noticed that Flexscript (as of ver. 2017), doesn't support parentheses around the case values. The following example cannot be built:

#define FOO (1)  // follow the best practice of C macros here
#define BAR (2)

int n = 2;
switch (n) {
    case FOO: // <-- syntax error
        pt("foo"); pr();
        break;
    case BAR:
        pt("bar"); pr();
        break;
}

The error is:

syntax error, unexpected '(', expecting integer or hexadecimal integer or '-'

On a related note, what is the recommended way to have multiple numeric or string constants defined and shared between multiple nodefunctions? There are Global Macros and Global Variables, but is there a way to avoid polluting the global namespace and not having to copy-paste pages of code?

FlexSim 17.0.1
flexscriptmacroswitch statementdefine
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Matt Long avatar image
3 Likes"
Matt Long answered Serge A commented

Not sure I've ever seen a switch statement written with parenthesis around the cases. Remove the parenthesis and you'll be just fine.

As for sharing numeric/string constants between multiple functions, the only way to do this is to define it as a global macro or global variable. There are ways of making 'hidden' global variables using Model Libraries. You can refer to the Miscellaneous / Custom Libraries section of the user manual for information on creating Model Libraries.

· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Serge A avatar image Serge A commented ·

Thank you for pointing to Miscellaneous / ModelLibraries node as a way to hide global data. That answers my second question.

As for the first one, it's not really a question, but rather an observation of an inconsistency in the language parser implementation and a request for enhancement. ((2) + (2)) is a valid expression elsewhere. For instance, it's a valid expression in the if statement. I don't see why it shouldn't be allowed in switch-case statements.

The workarounds (removing parentheses or using if statements instead of switch in flexscript) are obvious, but the point is, it may be desirable to keep parentheses around #define values when those are expressions themselves to make sure the order of operations is always correct. The following is an example of when not adding parentheses in macro definitions may have unforeseen consequences elsewhere in the code:

#define VALUE 2
#define BAD_MACRO VALUE > 2
#define GOOD_MACRO ((VALUE) > 2)

int wrong_condition = BAD_MACRO - 1;    // +1.0
int correct_condition = GOOD_MACRO - 1; // -1.0

// unfortunately, GOOD_MACRO can be used in IF, but not in CASE
1 Like 1 ·
Phil BoBo avatar image Phil BoBo ♦♦ Serge A commented ·

I'll add a note to the dev list to look into how we can handle expressions that evaluate to a constant integer in switch case statements better.

For now, you need to use an integer literal in a switch case statement; not an expression that evaluates to a constant integer.

The following is valid code in C++, but won't build with FlexScript:

#define VALUE (2)
int fred = 5;
int bob = 0;
switch (fred)
{
case (1):
	bob = 1;
	break;
case VALUE:
	bob = 2;
	break;
case 2 + 3:
	bob = 5;
	break;
default:
	bob = 3;
}
return bob;
0 Likes 0 ·
Serge A avatar image Serge A Phil BoBo ♦♦ commented ·

Thank you. That's very kind of you.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.