Menu

Conditional compilation

Will Pittenger

Overview

One thing that JAVA lacks is the ability to compile select parts of the code only when needed. This is something that C, C++, and C# all have. Now ASIL has something similar. Unlike other #if in C, C++, and C#; ASIL's support is integrated. This means that a conditional block must begin and end in the same parent block. (This prevents the situation where a closing brace ends up outside the #if block. A lot of syntax errors could hide that way.)

Please note that ASIL doesn't have or need a preprocessor like C/C++ does. The values in a conditional variable can't be treated as code! Yet, unlike C#, conditional variables can have values.

Declaring a conditional variable

You can use any const type needed as long as that type was compiled by a different project. There are two syntax options. The first declares a variable that can only be used in one way: was the variable declared or not. The second stores a value and can be referenced anywhere you'd use a normal variable. All conditional variables must be global in nature. Compiler developers should provide a means to "declare" any conditional variable outside the file. Conditional variables declared in this manner may not be duplicated in the code. All typed conditional variables must be initialized immediately. Conditional variables may not be changed once declared.

conditional test ' Can only be referenced inside **conditionalif** statements.  May not be compared.
conditional int test2 = 7 ' Can be used anywhere below the point where it is declared.

Deleting a conditional variable

Conditional compilation requires that you be able to cause the conditional variable to be deleted. Use the syntax shown below. Please note a file can undeclare conditional variables only if those variables were declared in that file. So conditional variables declared through a makefile or the compiler command line can't be undeclared.

conditional test ' now it's here
conditional - test ' now it's gone
conditional int test2 = 7 ' Now we have this one
conditional - test2 ' Now it's gone too

Referencing a conditional variable

Conditional compilation requires use of the conditionalif keyword in place of if. It's ASIL's counterpart of #if in C, C++, and C#. It works like a normal if statement and even uses then and else keywords. However, it can only reference conditional variables. Normal variables are unavailable. Note: If you want to test for a conditional's existence, prefix the conditional's identifier with the exists keyword. If the conditional's identifier is used alone, it must be a boolean or the expression must evaluate to a boolean. Any conditional variable that's referenced without the exists keyword must exist. If it doesn't, the compiler must issue an error.

conditional b
conditional int c = 43

conditionalif exists b then
  class conditonalClass
    conditionalif c then ' If c doesn't exist, it is false; if it does exist, it is true; it's value doesn't matter
      ' Some code that only compiles if c is declared

    conditionalif c = 34 then
      ' Some code that only compiles if c equals 34
    else
      ' Some code that only compiles if c doesn't equal 34

Related

Wiki: Home
Wiki: Keywords
Wiki: keywords-conditional
Wiki: keywords-conditionalif
Wiki: keywords-else
Wiki: keywords-exists
Wiki: keywords-if
Wiki: keywords-then
Wiki: keywords-var