Repository navigation
Preprocessor
The compiler supports preprocessor-like statements.
#ifdef KEYWORD
controlled text
#endif { KEYWORD }
This block is called a conditional group. Controlled text will be compiled if and only if KEYWORD is defined. We say that the conditional succeeds if KEYWORD is defined, fails if it is not.
#ifndef KEYWORD
controlled text
#endif { KEYWORD }
The ‘#ifndef‘ directive is the opposite of the ‘#ifdef‘, the controlled text will be compiled if and only if KEYWORD is not defined.
The ‘#if‘ directive allows you to test the value of an arithmetic expression, rather than the mere existence of one keyword. Its syntax is
#if A == 1
controlled text
#endif { A == 1 }
expression is a Spin CONstant expression.
The preprocessor calculates the value of expression, if the value comes out to be nonzero, the ‘#if‘ succeeds and the controlled text is compiled, otherwise it is skipped.
The ‘#else‘ directive can be added to a conditional to provide alternative text to be compiled if the condition fails. This is what it looks like:
#if expression
text-if-true
#else /* Not expression */
text-if-false
#endif /* Not expression */
If expression is nonzero, the text-if-true is compiled and the text-if-false is skipped. If expression is zero, the opposite happens.
You can use ‘#else‘ with ‘#ifdef‘ and ‘#ifndef‘, too.
The directive ‘#error’ causes the compiler to report a fatal error. The text after the directive is used as the error message.
You would use ‘#error’ inside of a conditional that detects a missing parameter or a combination of parameters that you know the program does not properly support.
#ifndef USB_BASE_PIN
#error "USB_BASE_PIN not defined"
#endif
The directive ‘#warning’ is like ‘#error’, but causes the compiler to issue a warning and continue.
#ifndef USB_BASE_PIN
#define USB_BASE_PIN 16
#warning "USB_BASE_PIN not defined, using default 16"
#endif