Skip to content

Conditional compilation

Lorenzi edited this page Jun 19, 2023 · 1 revision

Conditional Compilation

From v0.13.1 onwards

You can enable or disable a section of code by surrounding it with one of the conditional directives: #if, #elif, and #else.

The inner code can include instruction invocations, constant or label declarations, and any other top-level declaration, such as #ruledef or #bankdef. You can nest the conditional directives to any depth.

It's not currently possible, though, to use these directives inside another declaration, such as inside a #ruledef block to conditionally include certain rule patterns β€” you can only surround the entire #ruledef block.

The code must be surrounded by braces {}, so there's no dedicated terminating directive.

The condition expressions must return a boolean value.

For example:

greeting_index = 1

#if greeting_index == 0
{
    #d "Good morning, world!"
}
#elif greeting_index == 1
{
    #d "Good afternoon, world!"
}
#else
{
    #d "Good evening, world!"
}

The conditional directives are especially useful with the command-line defines. You can use -dNAME or -dNAME=VALUE to overwrite a constant declaration in the code. If you don't specify a value, a boolean true is assumed.

Note that you must have a constant declared with the same name in the code, contrary to what a C compiler would allow. This means that, if you don't specify a command-line define with a certain name, the declaration in the code is essentially the default value. You can use a default value like greeting_index = {} (an empty code block, which has the void type) to make it so any use of the default value in an expression results in a type error.

For example, you could run the app with: customasm greeting.asm -dgreeting_index=1