|Powered by QM on a Linux server|
KnowledgeBase 00083: Conditional Compilation
Application developers sometimes need to write programs that can be compiled on different multivalue systems where there may be syntactic or semantic variations in how a source line will be handled. Several multivalue products, but unfortunately not all, provide compiler directives that allow conditional compilation.
For those products that do not provide this as a built-in part of the multivalue Basic language, QM includes the source code for a pre-processor named PREPROC in the BP file of the QMSYS account which should itself compile on most systems without modification.
The $IFDEF Compiler Directive
Conditional compilation is controlled by use of the $IFDEF directive.
$IFDEF name ...statements... $ELSE ...statements... $ENDIFIn this example, the statements under the $IFDEF line will be compiled only if name has been defined using $DEFINE, EQUATE or via the $BASIC.OPTIONS record.
The $ELSE element is optional. If present, the statements under the $ELSE line will be compiled only if name has not been defined.
There is also a $IFNDEF that inverts the condition, compiling the lines under the $IFNDEF if name is not defined and those under the $ELSE if it is defined.
More complex conditions can be constructed using AND or OR (or their symbolic variants & and !) to form a compound expression that is evaluated left to right. This ability is present in the QMBasic compiler but not in the pre-processor prior to release 3.0-3.
Conditional compilation constructs can be nested to any depth though excessive nesting can make programs difficult to read.
Standard Defined Names
When compiling programs on QM, the name "QM" is always defined. Thus a program might include a section that reads
$IFDEF QM STATE = SUBSTITUTE(STATE, STATE.CODES, STATE.TEXT) $ELSE N = DCOUNT(STATE, @VM) FOR I = 1 to N LOCATE(STATE<I>, STATE.CODES, 1; POS) STATE<I> + STATE.TEXT<POS> NEXT I $ENDIF
Although they should rarely be needed because compiled programs are portable across all platforms on QM, there are also platform specific names defined such as QM.LINUX, QM.WINDOWS, QM.MAC, etc. Note that these are dependent on where the program is compiled, not the platform on which it is executed (which can be determined using the SYSTEM(1010) function).
The ability to use conditional compilation is particularly valuable while migrating to QM as it allows any changes needed to application prorgams to be included in the same source code that is used for the previous platform, thus removing the need to continually apply developments to two source streams.
00082: Setting QMBasic Compiler Options