1.2.2. Conditionals

Felix provides the usual conditional directives #ifdef, #ifndef, #if, #elif, #else, #endif. [Add #switch/#case/#endswitch]

The #if and #elif directives require an argument.

The balance of the line is first tokenised by the standard Felix tokeniser and thus may contain Felix literals and Felix identifiers.

The line is then expanded by macro substitution. The result is then parsed as an expression and constant folded and must yield a boolean constant. Note that every name encountered must name a preprocessor macro.

Start felix section to tut/macros/mac-1.02.02-0.flx[1 /1 ]
     1: #line 123 "./lpsrc/flx_tut_macro.pak"
     2: #import <flx.flxh>
     3: print "Defining FIRST"; endl;
     4: #define FIRST
     5: #ifdef FIRST
     6: print "detected FIRST"; endl;
     7: #else
     8: print "BAD"; endl;
     9: #endif
End felix section to tut/macros/mac-1.02.02-0.flx[1]
Start data section to tut/macros/mac-1.02.02-0.expect[1 /1 ]
     1: Defining FIRST
     2: detected FIRST
End data section to tut/macros/mac-1.02.02-0.expect[1]
Start felix section to tut/macros/mac-1.02.02-1.flx[1 /1 ]
     1: #line 139 "./lpsrc/flx_tut_macro.pak"
     2: #import <flx.flxh>
     3: #define FIRST 1
     4: #define SECOND 2
     5: #if FIRST == 1 and SECOND == 2
     6: print "OK"; endl;
     7: #else
     8: print "BAD"; endl;
     9: #endif
End felix section to tut/macros/mac-1.02.02-1.flx[1]
Start data section to tut/macros/mac-1.02.02-1.expect[1 /1 ]
     1: OK
End data section to tut/macros/mac-1.02.02-1.expect[1]
Start felix section to tut/macros/mac-1.02.02-2.flx[1 /1 ]
     1: #line 154 "./lpsrc/flx_tut_macro.pak"
     2: #import <flx.flxh>
     3: #define FIRST 1
     4: #define SECOND 2
     5: #if if FIRST == 1 then SECOND == 2 else 1 == 0 endif
     6: print "OK"; endl;
     7: #else
     8: print "BAD"; endl;
     9: #endif
End felix section to tut/macros/mac-1.02.02-2.flx[1]
Start data section to tut/macros/mac-1.02.02-2.expect[1 /1 ]
     1: OK
End data section to tut/macros/mac-1.02.02-2.expect[1]
Start felix section to tut/macros/mac-1.02.02-3.flx[1 /1 ]
     1: #line 170 "./lpsrc/flx_tut_macro.pak"
     2: #define FIRST 1
End felix section to tut/macros/mac-1.02.02-3.flx[1]
Start data section to tut/macros/mac-1.02.02-3.expect[1 /1 ]
End data section to tut/macros/mac-1.02.02-3.expect[1]
Start felix section to tut/macros/mac-1.02.02-4.flx[1 /1 ]
     1: #line 177 "./lpsrc/flx_tut_macro.pak"
     2: #import <flx.flxh>
     3: #import "mac-1.02.02-3.flx"
     4: #ifdef FIRST
     5: print "OK";
     6: #else
     7: print "BAD";
     8: #endif
     9: endl;
End felix section to tut/macros/mac-1.02.02-4.flx[1]
Start data section to tut/macros/mac-1.02.02-4.expect[1 /1 ]
     1: OK
End data section to tut/macros/mac-1.02.02-4.expect[1]