Preprocessor
(Created page with "= Preprocessor = === Description === The Sputnik preprocessor scans source files and changes them based on special # defined stuff such as #if before sending the source to the ...") |
|||
Line 108: | Line 108: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
// Make "Meep" become a link/alias to the "say" function | // Make "Meep" become a link/alias to the "say" function | ||
+ | // To do this we must place TWO spaces after the first ) | ||
+ | // otherwise Sputnik will not take note of this as a function | ||
#define Meep() say | #define Meep() say | ||
// Call meep which will in turn call "say" | // Call meep which will in turn call "say" | ||
Line 119: | Line 121: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
// Create the old C/C++ min/max functions using #define | // Create the old C/C++ min/max functions using #define | ||
+ | // To do this we must place TWO spaces after the first ) | ||
+ | // otherwise Sputnik will not take note of this as a function | ||
#define c_min($a, $b) ($a < $b ? $a : $b) | #define c_min($a, $b) ($a < $b ? $a : $b) | ||
#define c_max($a, $b) ($a > $b ? $a : $b) | #define c_max($a, $b) ($a > $b ? $a : $b) |
Revision as of 12:40, 22 June 2015
Contents |
Preprocessor
Description
The Sputnik preprocessor scans source files and changes them based on special # defined stuff such as #if before sending the source to the parser to deal with.
#predef
This is used to pre define a variable for use with #if etc
#predef $Test = true #if ($Test) say "True"; #else say "False"; #endif // PRINTS // TRUE
#if, #else, #elsif
This is used to define a conditional statement in your code where you can decide to omit entire sections of your source code from ever being parsed (and thus not run) if the condition is not met.
#predef $Test = 11 #if ($Test == 10) say "It is 10"; #elsif ($Test == 11) say "It is 11"; #else say "No idea what it is"; #endif // PRINTS // It is 11
#define
This is used to create a Sputnik @Macro or a function etc.
Lets define a string macro
#define Test "Hello" say @Test; // PRINTS // Hello
Lets define a static (static in terms of the string itself isnt parsed for variables etc) string macro
#define Test 'Hello' say @Test; // PRINTS // Hello
Lets define a boolean macro
#define Test true say @Test; // PRINTS // true
Lets define a char macro
#define Test @'A' say @Test; // PRINTS // A
Lets define an integer macro
#define Test 100 say @Test; // PRINTS // 100
Lets define a hex integer macro
#define Test 0x7A say @Test; // PRINTS // 122
Lets define an floating point macro
#define Test 100.77 say @Test; // PRINTS // 100.77
Lets define a link to another function (an alias)
// Make "Meep" become a link/alias to the "say" function // To do this we must place TWO spaces after the first ) // otherwise Sputnik will not take note of this as a function #define Meep() say // Call meep which will in turn call "say" meep "hello"; // PRINTS // hello
Lets define to create a function in a shorthand way
// Create the old C/C++ min/max functions using #define // To do this we must place TWO spaces after the first ) // otherwise Sputnik will not take note of this as a function #define c_min($a, $b) ($a < $b ? $a : $b) #define c_max($a, $b) ($a > $b ? $a : $b) // Try them out say c_min(10, 20); say c_max(10, 20); // PRINTS // 10 // 20
Remarks
N/A.