Difference between revisions of "User:Kadah Coba/Firestorm Preprocessor"
Kadah Coba (talk | contribs) (Created page with "= Firestorm Preprocessor = A few of the preproc tricks that may be useful. No real organization, just mainly notes. Since most of the FS preproc functionality comes via Boos...") |
Kadah Coba (talk | contribs) m (Fixed tokens to strings (will handle macro expansion)) |
||
Line 14: | Line 14: | ||
====== Turn tokens in to strings ====== | ====== Turn tokens in to strings ====== | ||
Useful for adding debugging to macros. | Useful for adding debugging to macros.[http://www.decompile.com/cpp/faq/file_and_line_error_string.htm] | ||
<source lang="lsl2"> | <source lang="lsl2"> | ||
#define printlist(list) llOwnerSay( | #define STRINGIFY(x) #x | ||
#define TOSTRING(x) STRINGIFY(x) | |||
#define printlist(list) llOwnerSay(TOSTRING(list) + ": " + llDumpList2String(list,", ")); | |||
list somelist = [1,2]; | list somelist = [1,2]; |
Revision as of 00:04, 3 September 2015
Firestorm Preprocessor
A few of the preproc tricks that may be useful. No real organization, just mainly notes.
Since most of the FS preproc functionality comes via Boost::Wave, pretty much an standard C style preproc stuff seems to work. Like many of these [1] [2].
Relative includes
If you have a large project hosting on disk that is being included, you can reference includes local to the file instead of using static paths for everything.
#include "./classes/lawmower.lsl"
Turn tokens in to strings
Useful for adding debugging to macros.[3]
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define printlist(list) llOwnerSay(TOSTRING(list) + ": " + llDumpList2String(list,", "));
list somelist = [1,2];
printlist(somelist);
//Output: somelist: 1, 2
Works with any token too.
#define PRINT_TOKEN(token) llOwnerSay(#token + " is " + (string)(token))
integer x = 1; integer y = 2;
PRINT_TOKEN(x + y);
//Preproc output: llOwnerSay("x + y" + " is " + (string)(x + y));
//Output: x + y is 3
Multi-line macros
Using the escape character (\) at the end of a line on a macro will wrap lines. This is very helpful for saving memory on not using sub functions but still having the organization/reusability. More helpful on memory saving from not using sub functions that pass large amounts of variables or lists, or when more than one return is needed since passed variables on macros are like pointers.
#define OS(a,b) if (a > 1) {\
llOwnerSay((string)a);\
} else {\
llOwnerSay((string)b);\
}
OS(1234,5678);
//Preproc output: if (1234 > 1) { llOwnerSay((string)1234);} else { llOwnerSay((string)5678);};
Note: Single-line comments (\\) will cause undesired effects. Use block comments (/* */) instead with the escape character at EOL.
Construct variable names and such
Say you need to concatenate strings in to a variable name.
#define printstring(a,b) llOwnerSay(a ## _ ## b)
string test_string = "Something";
printstring( test, string );
//Preproc output: llOwnerSay(test_string);
//Output: Something