Conditional compilation
Parts of a program can be compiled or left out depending on constants: the target operating system, a debug switch, whether something is already defined. The choice is made while compiling, so the code that is left out costs nothing, and it is not even checked. It may call procedures or use structures that only exist on another system.
CompilerIf ✅
CompilerIf condition
...
CompilerElseIf condition
...
CompilerElse
...
CompilerEndIf
The conditions must be constant: numbers, constants, compiler functions like SizeOf and Defined, and the operators between them. They are worked out in source order, so a condition can use constants defined earlier in the program, but not later ones.
#Debug = 1
OpenConsole()
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
PrintN("Windows")
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
PrintN("Linux")
CompilerElse
PrintN("something else")
CompilerEndIf
CompilerIf #Debug And SizeOf(Integer) = 8
PrintN("64-bit debug build")
CompilerEndIf
Output:
Windows
64-bit debug build
And and Or stop as soon as the answer is known, so the right-hand side can depend on the left: CompilerIf Defined(#Size, #PB_Constant) And #Size > 100 doesn't complain when #Size doesn't exist.
CompilerSelect ✅
CompilerSelect constant
CompilerCase value [, value ...]
...
CompilerDefault
...
CompilerEndSelect
#Level = 3
OpenConsole()
CompilerSelect #Level
CompilerCase 1, 2
PrintN("small")
CompilerCase 3, 4
PrintN("medium")
CompilerDefault
PrintN("large")
CompilerEndSelect
Output:
medium
CompilerError and CompilerWarning ✅
CompilerError "message"
CompilerWarning "message"
Reached in a branch that is compiled, CompilerError stops the compile with the message, and CompilerWarning prints it and carries on. Typically used to refuse an unsupported setup:
CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
CompilerError "This program only runs on Windows"
CompilerEndIf
Defined ✅
Result = Defined(Name, Kind)
1 if Name is already known as Kind at this point of the source, else 0. A constant is written with its #, everything else by its bare name: an array, list or map without ().
| Kind | Name is |
|---|---|
#PB_Constant |
a constant: #Width |
#PB_Variable |
a variable |
#PB_Array, #PB_List, #PB_Map |
an array, list or map |
#PB_Structure |
a structure |
#PB_Procedure |
a procedure in the program |
#PB_Function |
a library function: Len, SortArray, … |
#PB_Prototype |
a Prototype |
#PB_Interface |
an Interface |
It is often used to give a constant a default value that an including program can override:
#Width = 800
CompilerIf Not Defined(#Width, #PB_Constant)
#Width = 640
CompilerEndIf
CompilerIf Not Defined(#Height, #PB_Constant)
#Height = 480
CompilerEndIf
OpenConsole()
PrintN(Str(#Width) + " x " + Str(#Height))
Output:
800 x 480
#PB_Module is 1 for a module that appears above it in the source (see Modules). #PB_Label and #PB_OSFunction exist, but always give 0 for now.
Inside structures, enumerations, interfaces and Select ✅
CompilerIf can also choose fields in a Structure (inside a StructureUnion too), items in an Enumeration, methods in an Interface, and whole Case blocks in a Select. Only the chosen branch's fields, items, methods or cases exist: the structure's size and offsets, the enumeration's numbering and the interface's method slots are worked out as if the others were never written.
#Wide = 1
#Debug = 0
Structure Record
id.i
CompilerIf #Wide
values.l[8]
CompilerElse
values.l[2]
CompilerEndIf
CompilerIf #Debug
trace$
CompilerEndIf
EndStructure
Enumeration
#Red
CompilerIf #Wide
#Orange
CompilerEndIf
#Green
EndEnumeration
Procedure.s Name(c)
Select c
Case #Red
ProcedureReturn "red"
CompilerIf #Wide
Case #Orange
ProcedureReturn "orange"
CompilerEndIf
Default
ProcedureReturn "green"
EndSelect
EndProcedure
OpenConsole()
PrintN(Str(SizeOf(Record)) + " " + Str(#Green) + " " + Name(1))
Output:
40 2 orange
Inside a Select, a CompilerIf must hold whole Case (or Default) blocks, starting with one.
Compiler constants
The constants describing the compile are listed in Constants: #PB_Compiler_OS, #PB_Compiler_Processor, #PB_Compiler_Version and #X_Compiler_Version.
Limits 🟡
CompilerIfcan't appear inside a parameter list.CompilerSelectworks between statements only.- Macros defined, and files included, inside a branch that is left out are still read.