Documentation‎ > ‎

Conditional compilation

The GENERATE_IF statement can be used to produce output only
when a condition is true.  All alternative code paths are still
parsed and verified.  This is useful in libraries where different
code must be produced depending on the situation.

The BUILD_IF statement can be used to build code only when
a condition is true.  This allows skipping code which would not
compile, e.g. a missing enum value.  This can be used to build
code with different versions of the compiler, with different features
or for different purposes (testing, profiling).

GENERATE_IF

Example:

  GENERATE_IF ZC.lang == "C"
  >>>
     fputs("this is C code", stdout);
  <<<
  GENERATE_ELSEIF ZC.lang == "JS"
  >>>
     alert("this is JavaScript");
  <<<
  GENERATE_ELSE
     ZC.error("Language " .. ZC.lang .. " not supported)
  }

All alternative code paths are still parsed and resolved.  Thus even when
producing C code an error in the JavaScript code will be noticed.

The structure of the statement is:

     GENERATE_IF boolean_expr
        statements
     GENERATE_ELSEIF boolean_expr
        statements
     GENERATE_ELSE
        statements
     }

The GENERATE_ELSEIF can appear any number of times.
The GENERATE_ELSE is optional.
For "boolean_expr" see the Compile time expression section below.

BUILD_IF

Examples:

  BUILD_IF ZC.has("thread")  # compiler has thread support

     # run jobs in parallel
     job1.start()
     job2.start()
     job1.wait()
     job2.wait()
  BUILD_ELSE
     # run jobs sequentially
     job1.run()

     job2.run()
  }

  BUILD_IF Color.has("purple")
     c = Color.purple  # purple is available
  BUILD_ELSE
     c = Color.red     # there is no purple, use red
  }

The alternate code paths are all parsed, to be able to find the
end of the BUILD_IF statements.  Thus the syntax must be
correct, a missing } will be noticed.  But only when the condition
evaluates to true will the code be resolved and produced.  This
allows for using variables that don't exist, enum values that are
not defined, etc.

The structure of the statement is:

     BUILD_IF boolean_expr
        statements
     BUILD_ELSEIF boolean_expr
        statements
     BUILD_ELSE
        statements
     }

The BUILD_ELSEIF can appear any number of times.
The BUILD_ELSE is optional.
For "boolean_expr" see the next section.

Compile time expression

The boolean_expr only supports these operators:
     ||
     &&
     ==
     !=

These values are supported:
    TRUE
    FALSE
    "string constant"
    ZC.lang           # string: "C" when producing C code, or "JS" when producing JavaScript
    ZC.permu          # string, e.g.: "ie5"
    ZC.have("backtrace")  # boolean, TRUE when stack backtrace is available