|
Using Preprocessor |
Top Previous Next |
Tibbo Basic compiler includes a preprocessor that understands several directives.
#define and #undef
The #define directive assigns a replacement token for an identifier. Before compilation, each occurrence of this identifier will be replaced with the token, for example:
#define ABC x(5) 'now ABC will imply 'x(5)' ABC=20 'now it is the same as writing x(5)=20
|
The #undef directive "destroys" the definition made earlier with #define :
#define ABC x(5) 'define ... #undef ABC 'destroy ... ABC=20 'you will get a compilation error on this line (compiler will try to process this as "=20")
|
#if - #else - #elif -- #endif
These directives are used to conditionally include certain portions of the source code into the compilation process (or exclude from it). Here is an example:
#define OPTION 0 'set to 0, 1, or 2 to select different blocks of code for compilation ... #If OPTION=0 s="ABC" 'will be compiled when OPTION=0 #elif OPTION=1 s="DEF" 'will be compiled when OPTION=1 #Else s="123" 'will be compiled when OPTION=2, 3, etc. #endif
|
You can improve on this example and add meaning to 0, 1, and 2:
#define RED 0 #define GREEN 1 #define BLUE 2 ... #define OPTION BLUE ... #If OPTION=RED s="ABC" 'will be compiled when OPTION=RED (0) #elif OPTION=GREEN s="DEF" 'will be compiled when OPTION=GREEN (1) #Else s="123" 'will be compiled when neither RED, nor GREEN #endif
|
You can also write like this:
#If OPTION x=33 'will be compiled in if OPTION evaluates to any value except 0. Will not be compiled in if OPTION evaluates to 0. #endif
|
Preprocessor directives are not "strict". You don't have to define something before using it. During #if evaluation, all undefined identifiers will be replaced with 0:
#If WEIRDNESS 'undefined identifier x=33 'will not be compiled in #endif
|
Do not confuse compile-time definitions such as "#define OPTION 2" and actual application code like "const COLOR=2" or "x=3". They are from two different worlds and you can't use the latter as part of #if directives. For example, the following will not work:
#define OPTION 0 'preprocessor directive Const COLOR=2 'constant used by your application
#If OPTION=COLOR 'to a confused programmer, this looks like 0=2, but COLOR is not #defined, hence, it will be evaluated to 0 'hence, this code will be compiled in! #endif
|
The #if directive also understands expressions, for example:
#define RED 0 #define GREEN 1 #define BLUE 2 ... #define OPTION 2 ... #If OPTION=GREEN+1 'will be compiled in, because GREEN=1, hence the entire expression evaluates to 2, and OPTION=2 #endif
|
#ifdef - #else - #endif
#ifdef and #ifndef# are like #if, but instead of evaluating an expression they simply checks if specified definition exists:
#ifdef OPTION s="X" 'will be compiled if OPTION is defined #Else s="1" 'will be compiled if OPTION is not defined #endif
|
#ifndef is like #ifdef, but in reverse:
#ifndef OPTION s="X" 'will be compiled if OPTION is not defined #Else s="1" 'will be compiled if OPTION is defined #endif
|