#IF, #ELSEIF, #ELSE, #ENDIF (Conditional code translation)
Oh Dear – it seems the #ELSE feature is currently broken in SPLat/PC. As a work-around, you can use ELSEIF like this:
#IF EQ( YourThing )
; if TRUE
#ELSEIF EQ( 1, 1 ) ;ELSE is broken, so this is a workaround (it's always TRUE and will only execute if everything prior is FALSE)
; otherwise
#ENDIF
If you’re using builder, then Builder Conditional Segments do work.
From build 327 SPLat/PC supports conditional code translation. What this means is that your .spt source file(s) can contain statements that control which lines actually get included in the final program. NOTE: These instructions do not work at RUN TIME, they are only used when COMPILING your SPLat program, so they allow you to include different pieces of code when compiling.
Example:
Board: #EQU MMI
#IF EQ(Board, MMI)
iStart EQU 7
oMotor EQU 0
#ELSEIF EQ(Board, MS)
iStart EQU 15
oMotor EQU 10
#ELSE
Board type not defined
#ENDIF
What’s happening here?
The first line defines “Board” as an #EQU constant with the value MMI (as a string). The second – #IF – line says “If Board = MMI” then use the lines that follow. Those lines allocate an input and an output. The EQ(..,..) is called a qualifying function, which must always evaluate to True or False. Then comes the #ELSEIF. It says “If the MMI test failed, try for MS instead”. If that test passes, then a different set of allocations will be used. The final #ELSE trips if neither the MMI nor the MS test passes, and results in an error, so the program translation will fail.
As it stands the example will use MMI values. But I only have to edit one line, the very first one, to switch to allocations for (presumably) an MS121.
Conditional translation hash functions
There are four conditional translation hash functions:
#IF
#ELSEIF
#ELSE
#ENDIF
A number of rules apply:
- They must appear in the order listed.
#ELSEIFand#ELSEare optional- There can be more than one
#ELSEIFbut never more than one#ELSE. #IFand#ELSEIFmust have one and only one qualifying function. That’s what determines if the condition will apply.#IF ... #ENDIFblocks may be nested wholly within each other- The #EQU constants used in the qualifying function must be defined before the #IF block.
Qualifying functions
There are a number of qualifying functions that can be use with #IF and #ELSEIF. The arguments can be text strings or numbers, or #EQU defined constants that represent text strings or numbers. Some qualifying functions work on text strings, some on numerical values. The following tables summarises them.
| Function | What it does | Example |
EQ(A,B) | Tests if two #EQUated constants are equal. If they can both be interpreted as numbers, the comparison is performed on the numerical values. If either is not numerical, they are tested for string equality ignoring case (Cat and caT are equal). | EQ(Cat,Dog) failsEQ(Cat,caT) passesEQ(12, 12.0) - passes |
NE(A,B) | Tests if two #EQUated constants are unequal. If they can both be interpreted as numbers, the comparison is performed on the numerical values. If either is not numerical, they are tested for string equality ignoring case. | EQ(Cat,Dog) passesEQ(12, 12.0) passes |
GT(A,B) | Tests if A is numerically greater than B. Both must be numerical values. | RinseTime #EQU 2500#IF GT(RinseTime, 1000) |
GE(A,B) | Tests if A is numerically greater than or equal to B. Both must be numerical values. | #ELSEIF GT(RinseTime, 2000) |
LT(A,B) | Tests if A is numerically less than B. Both must be numerical values. | |
LE(A,B) | Tests if A is numerically less than or equal to B. Both must be numerical values. |
The #EQU constants used in qualifying functions must be defined before they are used.