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 ...") |
(→#predef) |
||
(6 intermediate revisions by one user not shown) | |||
Line 4: | Line 4: | ||
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. | 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. | ||
+ | |||
+ | For any of these to work the # must be the FIRST thing on the line (white space is allowed). | ||
+ | |||
+ | === #include-once === | ||
+ | |||
+ | This is used to make a source file is only included once (regardless of how many other source files request its inclusion) | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | #include-once | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === #require-once === | ||
+ | |||
+ | This is used to make a source file is only required once (regardless of how many other source files request its inclusion) | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | #require-once | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === #include "???" === | ||
+ | |||
+ | This is used to make a source file get included directly in the current source file | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | #include "OtherFile.spk" | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Warning - You must make note that this will ALWAYS include the file at the TOP of the source code it does NOT include where you place the #include statement. | ||
+ | |||
+ | So if you do this | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | say "111"; | ||
+ | #include "OtherFile.spk" | ||
+ | say "222"; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | The console will print 111 then 222 THEN it will do the actions from the "OtherFile.spk". | ||
+ | |||
+ | This means you should always place #include statements at the top of your file to keep it obvious where they are being included. | ||
+ | |||
+ | Of course #include statements ARE included in ORDER for example | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | say "111"; | ||
+ | #include "OtherFile.spk" | ||
+ | #include "OtherFile2.spk" | ||
+ | #include "OtherFile3.spk" | ||
+ | #include "OtherFile4.spk" | ||
+ | say "222"; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | This will print 111 then 222 THEN it will execute the actions of "OtherFile.spk" followed byt "OtherFile2.spk" then "OtherFile3.spk" then "OtherFile4.spk". | ||
+ | |||
+ | So order does matter. | ||
=== #predef === | === #predef === | ||
Line 18: | Line 73: | ||
// PRINTS | // PRINTS | ||
// TRUE | // TRUE | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | You may also omit the = and let it assign true for you example | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | #predef $Test | ||
+ | #if ($Test) | ||
+ | say "It is true"; | ||
+ | #else | ||
+ | say "It is false"; | ||
+ | #endif | ||
+ | // PRINTS | ||
+ | // It is true | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === #predefwipe === | ||
+ | |||
+ | Delete all previously assigned preprocessor variables | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | #predef $Test = 11 | ||
+ | #unpredef $Test | ||
+ | #if ($Test == 10) | ||
+ | say "It is 10"; | ||
+ | #elsif ($Test == 11) | ||
+ | say "It is 11"; | ||
+ | #else | ||
+ | say "No idea what it is"; | ||
+ | #endif | ||
+ | // PRINTS | ||
+ | // No idea what it is | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === #unpredef === | ||
+ | |||
+ | Delete a previously assigned preprocessor variable | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | #predef $Test = 11 | ||
+ | #unpredef $Test | ||
+ | #if ($Test == 10) | ||
+ | say "It is 10"; | ||
+ | #elsif ($Test == 11) | ||
+ | say "It is 11"; | ||
+ | #else | ||
+ | say "No idea what it is"; | ||
+ | #endif | ||
+ | // PRINTS | ||
+ | // No idea what it is | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 35: | Line 139: | ||
// PRINTS | // PRINTS | ||
// It is 11 | // It is 11 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of using #unpredef to delete a previously assigned preprocessor variable | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | #predef $Test = 11 | ||
+ | #unpredef $Test | ||
+ | #if ($Test == 10) | ||
+ | say "It is 10"; | ||
+ | #elsif ($Test == 11) | ||
+ | say "It is 11"; | ||
+ | #else | ||
+ | say "No idea what it is"; | ||
+ | #endif | ||
+ | // PRINTS | ||
+ | // No idea what it is | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | You can check using the "#predef $varname" directly which will act like a boolean | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | #predef $Test | ||
+ | #if ($Test) | ||
+ | say "It is true"; | ||
+ | #else | ||
+ | say "It is false"; | ||
+ | #endif | ||
+ | // PRINTS | ||
+ | // It is true | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 108: | Line 241: | ||
<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 113: | Line 248: | ||
// PRINTS | // PRINTS | ||
// hello | // hello | ||
+ | |||
+ | // For a value to be a Function macro the following must be true: | ||
+ | // * It must start with an Identifier with ( ) such as MinValue() | ||
+ | // * There must be TWO yes 2 spaces after the ) of the MinValue() | ||
+ | // * There must be at least ONE character for the body immediately | ||
+ | // after the two spaces | ||
+ | // | ||
+ | // This is how a function is defined | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 119: | Line 262: | ||
<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) | ||
Line 127: | Line 272: | ||
// 10 | // 10 | ||
// 20 | // 20 | ||
+ | |||
+ | // For a value to be a Function macro the following must be true: | ||
+ | // * It must start with an Identifier with ( ) such as c_min() | ||
+ | // * There must be TWO yes 2 spaces after the ) of the c_max() | ||
+ | // * Then there mustbe a ( followed by the function body code | ||
+ | // * and finally ended by a ) | ||
+ | // * The function body does not return a return and a ; since that | ||
+ | // * Is placed automatically for you | ||
+ | // | ||
+ | // This is how a function is defined | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 13:24, 23 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.
For any of these to work the # must be the FIRST thing on the line (white space is allowed).
#include-once
This is used to make a source file is only included once (regardless of how many other source files request its inclusion)
#include-once
#require-once
This is used to make a source file is only required once (regardless of how many other source files request its inclusion)
#require-once
#include "???"
This is used to make a source file get included directly in the current source file
#include "OtherFile.spk"
Warning - You must make note that this will ALWAYS include the file at the TOP of the source code it does NOT include where you place the #include statement.
So if you do this
say "111"; #include "OtherFile.spk" say "222";
The console will print 111 then 222 THEN it will do the actions from the "OtherFile.spk".
This means you should always place #include statements at the top of your file to keep it obvious where they are being included.
Of course #include statements ARE included in ORDER for example
say "111"; #include "OtherFile.spk" #include "OtherFile2.spk" #include "OtherFile3.spk" #include "OtherFile4.spk" say "222";
This will print 111 then 222 THEN it will execute the actions of "OtherFile.spk" followed byt "OtherFile2.spk" then "OtherFile3.spk" then "OtherFile4.spk".
So order does matter.
#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
You may also omit the = and let it assign true for you example
#predef $Test #if ($Test) say "It is true"; #else say "It is false"; #endif // PRINTS // It is true
#predefwipe
Delete all previously assigned preprocessor variables
#predef $Test = 11 #unpredef $Test #if ($Test == 10) say "It is 10"; #elsif ($Test == 11) say "It is 11"; #else say "No idea what it is"; #endif // PRINTS // No idea what it is
#unpredef
Delete a previously assigned preprocessor variable
#predef $Test = 11 #unpredef $Test #if ($Test == 10) say "It is 10"; #elsif ($Test == 11) say "It is 11"; #else say "No idea what it is"; #endif // PRINTS // No idea what it is
#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
Example of using #unpredef to delete a previously assigned preprocessor variable
#predef $Test = 11 #unpredef $Test #if ($Test == 10) say "It is 10"; #elsif ($Test == 11) say "It is 11"; #else say "No idea what it is"; #endif // PRINTS // No idea what it is
You can check using the "#predef $varname" directly which will act like a boolean
#predef $Test #if ($Test) say "It is true"; #else say "It is false"; #endif // PRINTS // It is true
#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 // For a value to be a Function macro the following must be true: // * It must start with an Identifier with ( ) such as MinValue() // * There must be TWO yes 2 spaces after the ) of the MinValue() // * There must be at least ONE character for the body immediately // after the two spaces // // This is how a function is defined
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 // For a value to be a Function macro the following must be true: // * It must start with an Identifier with ( ) such as c_min() // * There must be TWO yes 2 spaces after the ) of the c_max() // * Then there mustbe a ( followed by the function body code // * and finally ended by a ) // * The function body does not return a return and a ; since that // * Is placed automatically for you // // This is how a function is defined
Remarks
N/A.