Difference between revisions of "User:Kadah Coba/Firestorm Preprocessor"

From Second Life Wiki
Jump to navigation Jump to search
(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...")
 
m
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Firestorm Preprocessor =
= LSL Preprocessor =


A few of the preproc tricks that may be useful. No real organization, just mainly notes.
A few of the preproc tricks that may be useful. No real organization, just mainly notes.
Line 7: Line 7:


====== Relative includes ======
====== 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.
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.
<source lang="lsl2">
<source lang="lsl2">
#include "./classes/lawmower.lsl"
#include "./classes/lawmower.lsl"
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(#list + ": " + llDumpList2String(list,", "));
#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];
Line 58: Line 60:
//Output: Something
//Output: Something
</source>
</source>
====== Compile Error ======
<source lang="lsl2">
file.lsl(last_line_number): warning: generic lexer warning: Unterminated 'C++' style comment
</source>
Last line of source files needs to be blank
= So I heard you like macros =
It would seem that the Boost Library Preprocessor[http://www.boost.org/doc/libs/1_59_0/libs/preprocessor/doc/index.html] stuff works. I have not tested much of it, but what I have, has worked.
====== Requirements ======
A copy of Boost[http://www.boost.org/users/download/] 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.
<source lang="lsl2">
#include <boost/preprocessor.hpp>
</source>
Optimally, include just what is needed instead. Most of the macros may not be useful for LSL anyway.
<source lang="lsl2">
#include <boost/preprocessor/arithmetic/add.hpp>
integer int_five = BOOST_PP_ADD(2,3);
</source>
====== Usage ======
See documentation[http://www.boost.org/doc/libs/1_59_0/libs/preprocessor/doc/index.html]

Latest revision as of 21:02, 23 October 2022

LSL 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
Compile Error
file.lsl(last_line_number): warning: generic lexer warning: Unterminated 'C++' style comment

Last line of source files needs to be blank

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]