Syntax
begin enum [[not] output] [start [,inc]]
_constName1 [= staticExpression1]
_constName2 [= staticExpression2]
end enum
Description
This statement begins a block of "enumerated constant" definition lines. The block must be terminated with the end enum statement. All of the constants defined in this block are global, regardless of where in the program the block appears.
The begin enum...end enum block is "non-executable," which implies that it won't be repeated or skipped if it appears within any kind of "conditional execution" block, such as for...next, if...end if, do...until, etc. (but it can be conditionally included or excluded if it appears inside a #if block).
Each _constName represents a symbolic constant name that has not previously been defined, and each staticExpression represents an integer expression which consists only of:
- integer literal constants;
- previously-defined symbolic constant names;
- operators (like +, -, *, /, >, =);
- parentheses
In particular, it can't contain variables or function references.
The begin enum block assigns values to each of the _constName symbolic constants as follows:
- If the _constName is followed by = staticExpression, then _constName is assigned the value of staticExpression;
- If the _constName is not followed by = staticExpression, then _constName is assigned the value of the _constName in the line above it, plus the value of inc;
- If the very first _constName is not followed by = staticExpression, then it's assigned the value of start.
The start and inc parameters, if included, must each be a static integer expression. The default value of start is 0, and the default value of inc is 1.
Example
In the following, the dwarves are assigned values of 1 through 7; _snowWhite
is assigned the value 100, and _thePrince is assigned the value 101.
begin enum 1
_docDwarf
_sneezy
_grumpy
_sleepy
_dopey
_happy
_bashful
_snowWhite = 100
_thePrince
end enum
Output Option:
The output option, introduced in FB 5.6.1, improves the readability of translated C code. Without this option, _constants and static expressions in FB source are translated to 'magic numbers' whose names are lost.
Consider this FB code:
begin enum
_foo = 3
_bar = 42
end enum
if ( _foo == _bar ) then ...
The C translation has magic numbers:
if ( 3 == 42 ) { ... }
Here's the same example, with the output
option added:
begin enum output
_foo = 3
_bar = 42
end enum
if ( _foo == _bar ) ...
In the C translation, an enum statement is output for each constant, allowing its name to be used later:
enum { foo = 3 };
enum { bar = 42 };
...
if ( foo == bar ) { ... } // symbols instead of numbers
Not Output Option:
The not output option improves the readability of translated C code in the same way as output. This form is used for system constants already known to the compiler, mainly those in FB Headers files. The C translation doesn't contain enum statement for the constants, because they would cause compile-time duplicate definition errors.