Qore Programming Language Reference Manual 2.3.0
Loading...
Searching...
No Matches
Conditional Parsing and Parse Defines

Qore supports conditional parsing with parse defines similar to the C/C++ preprocessor. The following parse directives are supported: %define, %else, %elif, %endif, %if, %ifdef, and %ifndef.

These directives allow for the existence (or lack thereof) of a parse define to affect which code is parsed into the program at parse time.

The %if Directive

The %if directive supports C preprocessor-style boolean expressions using the defined() function. This allows for more complex conditional logic than the simple %ifdef directive.

Syntax

%if defined(SYMBOL)
# code included if SYMBOL is defined
%endif

Supported Operators

The %if directive supports the following operators:

  • defined(SYMBOL) - returns true if SYMBOL is defined
  • ! - logical NOT operator
  • && - logical AND operator
  • || - logical OR operator
  • () - parentheses for grouping

Examples

# Simple defined check
%if defined(DEBUG)
printf("Debug mode enabled\n");
%endif
# Negation
%if !defined(PRODUCTION)
printf("Not in production\n");
%endif
# AND operator
%if defined(FEATURE_A) && defined(FEATURE_B)
printf("Both features enabled\n");
%endif
# OR operator
%if defined(LINUX) || defined(MACOS)
printf("Unix-like system\n");
%endif
# Complex expression with parentheses
%if (defined(UNIX) || defined(LINUX)) && !defined(EMBEDDED)
printf("Full Unix system\n");
%endif

The %elif Directive

The %elif directive provides else-if functionality, allowing multiple conditions to be tested in sequence. It uses the same expression syntax as %if.

%if defined(WINDOWS)
printf("Windows\n");
%elif defined(LINUX)
printf("Linux\n");
%elif defined(MACOS)
printf("macOS\n");
%else
printf("Unknown platform\n");
%endif

Legacy Directives

The %ifdef and %ifndef directives provide simple existence checks and remain supported for backwards compatibility:

%ifdef DEBUG
printf("Debug mode\n");
%endif
%ifndef RELEASE
printf("Not a release build\n");
%endif

Using Parse Defines

Basically, the above allow for the existence (or lack thereof) of a parse define to affect which code is parsed into the program at parse time.

Parse defines are defined on the command-line (or through the C++ API when executed in embedded code), as well as created automatically based on sytem options (see Qore::Option for a list of option constants that are also defined as parse defines); all library options (if the option is True, then it is defined as True, if the option is False, then it is not defined at all).

Note that "Unix" is defined on all Unix platforms (also on Cygwin), while "Windows" is defined on native Windows ports (but not on Cygwin, as this is treated as Unix when compiling, as all Unix features are available).

Additionally, the following options are defined in every program (however they are not yet useful when parsing as the value of parse options cannot be used yet at parse time; only the existence or lack thereof can affect parsing in this version of Qore when parsing at least).

Qore Parse Defines

Define Value
QoreVersionString Version string for the Qore library
QoreVersionMajor Major version for the Qore library
QoreVersionMinor Minor version for the Qore library
QoreVersionSub Sub version for the Qore library
QoreVersionBuild Build version for the Qore library
QoreVersionBits 32 or 64 depending on the library target
QorePlatformCPU The CPU targeted by the library
QorePlatformOS The OS targeted by the library

Additionally, only if the Qore library was compiled with debugging support, the following parse define is present (otherwise it is not defined):

Qore Optional Parse Defines

Define Value
QoreDebug True
See also
Qore::Option for a list of option constants that are also defined as parse defines

Here is an example of using parse defines in a program:

%ifndef HAVE_TERMIOS
printf("This program requires UNIX TermIOS features to be present; it does not run on platforms without this feature (current platform: %s); exiting...\n", Qore::PlatformOS);
exit(1);
%endif

Furthermore, parse defines can be manipulated in embedded code using the following functions: