User:Kadah Coba/Firestorm Preprocessor

From Second Life Wiki
Jump to navigation Jump to search

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 stored on disk that is being included, you can reference includes relative 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

So I heard you like macros

It would seem that the Boost Library Preprocessor[4] stuff works. I have not tested much of it, but what I have, has worked.

Requirements

A copy of Boost[5] in your LSL include path. Only boost\preprocessor\ and boost\preprocesso.hpp are needed.

Including in a project

While you could just do this and include everything, it will take quite a bit longer for the preproc to run.

#include <boost/preprocessor.hpp>

Optimally, include just what is needed instead. Most of the macros may not be useful for LSL anyway.

#include <boost/preprocessor/arithmetic/add.hpp>
integer int_five = BOOST_PP_ADD(2,3);
Usage

See documentation[6]