2.2. Constant Expression Folding

The Felix macro processor also performs constant folding; that is, evaluation of certain expressions involving only literals. The following operations are folded:
opdescr
+int
-int
int+int
int-int
int*int
int/int
int%int
int<int
int>int
int<=int
int>=int
int==int
int!=int
string+string
string==string
string!=string
string*intcat n copies
string stringcat
string intcat ISO10646
not bool
bool && bool
bool or bool
bool == bool
bool != bool
if bool then expr1 else expr2 endif

The critical operation here is that a conditional with a constant first argument is replaced by one of the two expressions, and the other is elided. See the example for an application: the implication is that the elided expression, whilst it must be well formed syntactically, does not need to be type correct, nor do symbols in it need to be defined.

Note that the fold uses compiler intrinsic operations on integers, bools, and strings: user defined functions are not applied and do not need to be defined. Consequently, the result may disagree with a user defined function applied at run time. If necessary, constant folding can be blocked with the noexpand mark 'noexpand'.

Note: the expressions

  "" 999
  u"" 999
are semantically, but not physically equivalent. The first operation is the UTF-8 representation of 999, whilst the second is the UCS-32 value 999.

Start C++ section to tut/examples/mac125.flx[1 /1 ]
     1: include "std";
     2: header '''
     3: #define flx_unix 0
     4: #define flx_windows 0
     5: #define testhook 1
     6: #if flx_unix
     7: #include <dlopen.h>
     8: #elif flx_windows
     9: #include <windows.h>
    10: #else
    11: #include <stdio.h>
    12: #endif
    13: ''';
    14: macro val Unix = case 1 of 2;
    15: macro val Windows = case 1 of 2;
    16: 
    17: fun dlopen:string -> address = 'dlopen($1.data())';
    18: fun LoadLibrary:string -> int = 'LoadLibrary($.data())';
    19: fun testhook:string -> int = 'printf("LoadTheLibrary(%s)\\n",$1.data())';
    20: 
    21: macro val openlib =
    22:   if Unix then dlopen
    23:   elif Windows then LoadLibrary
    24:   else testhook endif
    25: ;
    26: 
    27: macro val ext =
    28:   if Unix then ".so"
    29:   elif Windows then ".DLL"
    30:   else ".lib_ext" endif
    31: ;
    32: // conditional compilation
    33: val lib = openlib ("MyLibrary" ext);
    34: 
End C++ section to tut/examples/mac125.flx[1]