Jump to content
Sign in to follow this  
dwringer

Preprocessor macros using arguments - limits?

Recommended Posts

Hello everyone.

I am hoping someone around here with some experience might have some insights into the following problem:

If I create a preprocessor macro in a script to be executed like: "call compile preprocessfile 'myscript.sqf';" as follows:

#define MACRONAME(ARG1,ARG2,ARG3) setVariable [format ["foo%1%2", ARG1, ARG2], ARG3];

Then call it with:

dude MACRONAME("Bar","Baz","SomeString")

It works more or less okay, translating (after preprocessing and the "format" call) into:

dude setVariable ["fooBarBaz", "SomeString"];

BUT: The "SomeString" has had all commas removed from it, i.e.:

dude MACRONAME("Bar","Baz","SomeString, WithCommas") ->

dude setVariable ["fooBarBaz", "SomeString WithCommas"];

The macro breaks completely if I try something like this:

dude MACRONAME("Bar","Baz",["AnArray","OfStuff"])

Clearly, from the errors (and the error messages which I cannot at the moment reproduce) the macros fail to support arguments containing commas.

So my question is this: Is there a way around this, so we CAN use commas inside data representing macro arguments?

Share this post


Link to post
Share on other sites

Yes and no!

Yoo can define the string and then preprocess it, which works ok:

Ex:

_var1 = "AnArray";
_var2 = "OfStuff";

dude MACRONAME("Bar","Baz",[_var1,_var2])

  • Like 1

Share this post


Link to post
Share on other sites

Sweet, thank you very much! I never even thought about doing it that way, hehe.

Although, I should point out that your example does not work either - one problem is that you can't pass an array into a macro AT ALL from what I can tell. The error message is rather confusing: it indicates that something like:

dude MACRONAME("Bar","Baz",[_var1,_var2])

expands into:

dude dude setVariable [...

Why it doubles the word before the macro, I have no idea, thus I am rather confused about the exact behavior (perhaps just something undefined by the devs as a result of stripping the commas from the macro declaration).

But, declaring the array first:

_varArray = ["An","Array","Of","Stuff"];

dude MACRONAME("Bar","Baz",_varArray)

works as well as anything for my purposes.

Thanks again!

Share this post


Link to post
Share on other sites

Cool - I'm at work so my examples may be tainted by the work stuff floating around in my brain, so sorry for that - Will have a look later when I get in :)

Share this post


Link to post
Share on other sites

Just got home and checked examples - yes you're totally correct:

As long as the info is defined (put into a variable prior to being run) then it's acceptable to pass to a preprocessor command. :)

  • Like 1

Share this post


Link to post
Share on other sites

Came here from PreProcessor Commands. Sorry for necroposting.

 

Considering comma as argument separator is known and required feature of any preprocessor (and C preprocessor too, btw).

However, workarounds exist not involving changes to underlying code.

I tested some and at least two of them worked. Tested on 1.42 LegacyPort for Linux (but I think everything worked the same way two years ago).

 

First approach is to define macro for comma and use it instead of comma where comma is not considered to be argument separator.

Second is to define macro simply copying its arguments list on the output.

 

Both methods in one file (test.sqf):

// defining
#define COMMA ,
#define MULTIARGMACRO(arg1,arg2,arg3) someNameUsedByMacro(arg3,arg2,arg1);
// using
MULTIARGMACRO("foo",bar,["baz" COMMA qux])
// another approach
#define COMMA2ARGS(arg1,arg2) arg1,arg2
MULTIARGMACRO(foo,"bar",COMMA2ARGS([baz,"qux"]))

Preprocessing with following command:

diag_log preprocessFile "test.sqf"

gave me this result:

13:22:31 "



someNameUsedByMacro(["baz" , qux],bar,"foo");


someNameUsedByMacro([baz,"qux"],"bar",foo);
"

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×