specialize

specialize

Specialize a code snippet for a set of values.

Usage: For an existing C variable NAME use this as

#pragma CMOD amend specialize NAME [value1 … ]

Here values should be numeric and admissible for a == operator, or the special string else. Thus this construct is more powerful than a switch statement, because in addition to integers, floating point values can also be used as values.

A copy of the code snippet enclosed in its own block is created for each value, plus a fallback version if the value else is in the list. Within each of these copies the string ${NAME} is replaced by the actual value within that copy. In the fallback version ${NAME} is replaced by NAME, so not much is gained, there.

If else is not in the list, and thus there is no fallback version, you can add a fallback just after the closing pragma:

#pragma CMOD done
#else {
// code for the general case
}

Example:

The combination of this directive with the stringification feature of meta-variables allows to generate output that does not depend on dynamic interpretation of format strings

#pragma CMOD amend specialize x = false true
puts("x is " #${x})
#pragma CMOD done

Coding and configuration

The following code is needed to enable the sh-module framework.

SRC="$_" . "${0%%/${0##*/}}/import.sh"

Imports

The following sh-modules are imported:

Details