The $if Directive
Previous Topic  Next Topic 

The $if (constant_expression) directive allows for conditional compilation.     This is useful for allowing the same code to be compiled in two or more different forms (say for different hardware implementations).    There are three types of $if statement allowed:


$if (<x> streq <y>)

$if (<x> strneq <y>)

$if (<x>)


<x> and <y> represent constant strings, integers or declared constant identifiers.     In the first two forms a string comparison is carried out between <x> and <y> and the compiler will compile the following code (or not) depending on the result of the comparison.   For example:


$if(HARDWARE_TYPE streq "ACULAB")

     SMplay(vox_chan, "test.vox");

$else

   $if(HARDWARE_TYPE streq "DIALOGIC")

      dx_play(vox_chan,"test.vox");

   $else

      applog("Application compiled for unsupported hardware type=",HARDWARE_TYPE);

   $end

$endif



The constant identifier HARDWARE_TYPE is assumed to be declared in a const declaration somewhere in scope, else specified using the D option of the TCL compiler (See TCL compiler reference).           In the above application only one line will get compiled depending on the value of HARDWARE_TYPE.


The code in the lines that are not compiled do not even need to conform completely to the TE language syntax, however the text is still parsed token by token and so there are some restrictions as to what can be contained in the non-compiled side of the $if..$else..$endif statement.      Most notably a string token cannot span more than one line of the source code.


For example the following will compile successfully and the the compiler will simply excluding all text for the $else side of the following code:


const NO_NONSENSE= "1";


$if (NO_NONSENSE streq "1")

      // This code must be valid since it will get compiled

     applog("Not nonsense");

$else

     blah blah this doesnt need to make any sense since it will not get compiled!

     akjsd;lksad;kds;

     ;laksd;lask;laskd

     No syntax errors to be seen anywhere..

     but dont change NO_NONSENSE to 2 or the compiler will explode

$endif



However in the following excerpt a string in the $else side of the $if directive spans more than one line and will result in a syntax error:


const NO_NONSENSE= "1";


$if (NO_NONSENSE streq "1")

      // This code must be valid since it will get compiled

     applog("Not nonsense");

$else

     " This string will cause the compiler to fail with

       a sytax error because a string token cannot span

       more than one line of the source code"

$endif



The final form of the $if statement is $if(<x>).       This requires that a constant name be given and the compiler simply checks if this name has been defined.   Even if the value of the constant is blank or zero this statement will hold true.   For example:


const X_IS_DEFINED = "";


$if(X_IS_DEFINED)

    applog("X_IS_DEFINED is defined");

$endif


The applog statement would get compiled since X_IS_DEFINED is defined.     The same effect could be achieved by compiling with the D option in the TCL compiler.


$if …$else…$endif directives can be nested to any level..