PreProcessor Commands: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (wip)
(added documentation of upcoming new macros)
Line 6: Line 6:


== Parsing ==
== Parsing ==
* '''[[Config.cpp]]''' - parsed when PBO is binarized.
* '''[[Config.cpp]]''' - parsed when PBO is binarized.
** [[localize]] cannot be used in macros, as it would hardcode string of current language instead of creating reference.
** [[localize]] cannot be used in macros, as it would hardcode string of current language instead of creating reference.
Line 14: Line 13:


== Macros ==
== Macros ==
=== Comments ===
=== Comments ===
A comment is a line within code that is not actually processed by the game engine. They are used to make code more readable or to add notes for future reference. The preprocessor removes all comments from the file before it is processed. Therefore, comments are never actually "seen" by the game engine.
A comment is a line within code that is not actually processed by the game engine. They are used to make code more readable or to add notes for future reference. The preprocessor removes all comments from the file before it is processed. Therefore, comments are never actually "seen" by the game engine.


Line 94: Line 91:
</syntaxhighlight>
</syntaxhighlight>


The argument replacement is performed before expansion of the macro body. That means one doesn't have to worry about name-conflicts between argument-names of the current macro and already defined macros:
The argument replacement is performed before the expansion of the macro body. That means one doesn't have to worry about name-conflicts between argument-names of the current macro and already defined macros:
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
#define ONE foo
#define ONE foo
Line 102: Line 99:


==== Replacing parts of words ====
==== Replacing parts of words ====
By default you can only replace whole words by arguments. If you need to replace only part of a word, you can use the ''##'' instruction. This is necessary when either the start or the end of the argument connects to another character that is not a ''';''' (semi-colon) or &nbsp; (space).
By default, you can only replace whole words by arguments. If you need to replace only part of a word, you can use the ''##'' instruction. This is necessary when either the start or the end of the argument connects to another character that is not a ''';''' (semi-colon) or &nbsp; (space).
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
class NAME##_Button_Slider: RscText \
class NAME##_Button_Slider: RscText \
Line 125: Line 122:


=== #undef ===
=== #undef ===
Undefine (delete) a macro previously set by the use of #define.
Undefine (delete) a macro previously set by the use of #define.
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 132: Line 128:


=== #ifdef ===
=== #ifdef ===
You can use a simple if-then construction to check whether a certain set of definitions has already been made:
You can use a simple if-then construction to check whether a certain set of definitions has already been made:
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 143: Line 138:


=== #ifndef ===
=== #ifndef ===
Same as #ifdef, but checks for absence of definition instead.
Same as #ifdef, but checks for absence of definition instead.
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 161: Line 155:


=== #endif ===
=== #endif ===
This ends a conditional block as shown in the descriptions of #ifdef and #ifndef above.
This ends a conditional block as shown in the descriptions of #ifdef and #ifndef above.


=== #include ===
=== #include ===
Copies the code from a target file and pastes it where #include directive is.
Copies the code from a target file and pastes it where #include directive is.
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 199: Line 191:


=== # ===
=== # ===
'#' (single hash) operator wraps the text with quotation marks.
'#' (single hash) operator wraps the text with quotation marks.
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 208: Line 199:
</syntaxhighlight>
</syntaxhighlight>


This operator does only work on keywords following the rules for macro-names (see <tt>#define</tt>-section). If one wants to stringify anything else (like e.g. a number), one has to use a stringify-macro that takes an argment and stringifies that (as in the example above).
This operator does only work on keywords following the rules for macro-names (see <tt>#define</tt>-section). If one wants to stringify anything else (like e.g. a number), one has to use a stringify-macro that takes an argument and stringifies that (as in the example above).
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
#define MACRO Test #123
#define MACRO Test #123
Line 215: Line 206:


=== ## ===
=== ## ===
'##' (double hash) operator concatenates what's before the ## with what's after it.
'##' (double hash) operator concatenates what's before the ## with what's after it.
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 226: Line 216:


=== __EXEC ===
=== __EXEC ===
This '''config parser''' macro allows you to assign values to internal variables or just execute arbitrary code. The code inside <tt>__EXEC</tt> macros runs in [[parsingNamespace]] and variables defined in it will also be created in [[parsingNamespace]]. The variables can then be used to create more complex macros:
This '''config parser''' macro allows you to assign values to internal variables or just execute arbitrary code. The code inside <tt>__EXEC</tt> macros runs in [[parsingNamespace]] and variables defined in it will also be created in [[parsingNamespace]]. The variables can then be used to create more complex macros:


Line 242: Line 231:


=== __EVAL ===
=== __EVAL ===
With this '''config parser''' macro you can evaluate expressions, including previously assigned internal variables. Unlike with <tt>__EXEC</tt>, <tt>__EVAL</tt> supports multiple parentheses
With this '''config parser''' macro you can evaluate expressions, including previously assigned internal variables. Unlike with <tt>__EXEC</tt>, <tt>__EVAL</tt> supports multiple parentheses


Line 256: Line 244:


=== __LINE__ ===
=== __LINE__ ===
This keyword gets replaced with the line number in the file where it is found. For example, if __LINE__ is found on the 10th line of a file, the word __LINE__ will be replaced with the number 10.
This keyword gets replaced with the line number in the file where it is found. For example, if __LINE__ is found on the 10th line of a file, the word __LINE__ will be replaced with the number 10.


=== __FILE__ ===
=== __FILE__ ===
This keyword gets replaced with the CURRENT file being processed.
This keyword gets replaced with the CURRENT file being processed.


=== __FILE__ ===
{{Feature_arma3| The following macros are available since {{arma3}} 2.01.}}
 
This keyword gets replaced with the CURRENT file being processed.


=== __DATE_ARR__  ===
=== __DATE_ARR__  ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Is replaced for current date in format [[Array|array]].
Is replaced for current date in format [[Array|array]].
<code>__DATE_ARR__ //2020,10,28,15,17,42</code>
<code>__DATE_ARR__ //2020,10,28,15,17,42</code>


=== __DATE_STR__ ===
=== __DATE_STR__ ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Is replaced for current date in format [[String|string]].
Is replaced for current date in format [[String|string]].
<code>__DATE_STR__ //"2020/10/28, 15:17:42"</code>
<code>__DATE_STR__ //"2020/10/28, 15:17:42"</code>


=== __DATE_STR__ ===
=== __DATE_STR__ ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Is replaced for current date in format [[String|string]].
Is replaced for current date in format [[String|string]].
<code>__DATE_STR__ //"2020/10/28, 15:17:42"</code>
<code>__DATE_STR__ //"2020/10/28, 15:17:42"</code>


=== __DATE_STR_ISO8601__  ===
=== __DATE_STR_ISO8601__  ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Is replaced for current date in format [[String|string]]. The date is presented in ISO8601 standard.
Is replaced for current date in format [[String|string]]. The date is presented in ISO8601 standard.
<code>__DATE_STR_ISO8601__ //"2020-10-28T14:17:42Z"</code>
<code>__DATE_STR_ISO8601__ //"2020-10-28T14:17:42Z"</code>


=== __TIME__ ===
=== __TIME__ ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Is replaced for current time.
Is replaced for current time.
<code>__TIME__ //15:17:42</code>
<code>__TIME__ //15:17:42</code>


=== __TIME_UTC__ ===
=== __TIME_UTC__ ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Is replaced for  UTC time.
Is replaced for  UTC time.
<code>__TIME_UTC__ //14:17:42</code>
<code>__TIME_UTC__ //14:17:42</code>


=== __COUNTER__ ===
=== __COUNTER__ ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Counts 1 up everytime it is defined.
Counts 1 up everytime it is defined.
<code>__COUNTER__ //0
<code>__COUNTER__ //0
__COUNTER__ //2
__COUNTER__ //2
Line 309: Line 286:


=== __COUNTER_RESET__===
=== __COUNTER_RESET__===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Resets counter. See example above.
Resets counter. See example above.


=== __RAND_INT<tt>N</tt>===
=== __RAND_INT<tt>N</tt>===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Gets replaced by a random <tt>N</tt> bit integer. N can be replaced by 8, 16, 32 and 64
Gets replaced by a <tt>N</tt> bit integer. N can be replaced by 8, 16, 32 and 64
<code>__RAND_INT8__ //102</code>
 
=== __RAND_UINT<tt>N</tt>===
{{Feature_arma3| Since {{arma3}} 2.01.}}
Gets replaced by a ''unsigned'' <tt>N</tt> bit integer. N can be replaced by 8, 16, 32 and 64


=== __RAND_UINT<tt>N</tt>===
=== __RAND_UINT<tt>N</tt>===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Gets replaced by a random ''unsigned'' <tt>N</tt> bit integer. N can be replaced by 8, 16, 32 and 64
Gets replaced by a ''unsigned'' <tt>N</tt> bit integer. N can be replaced by 8, 16, 32 and 64
<code>__RAND_UINT8__ //108</code>


===__GAME_VER__ ===
===__GAME_VER__ ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Gets replaced by the game version.
Gets replaced by the game version. <tt>02.00.146790</tt>
<code>__GAME_VER__ //02.00.146790</code>


===__GAME_VER_MAJ__ ===
===__GAME_VER_MAJ__ ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Gets replaced by the ''major'' game version.  
Gets replaced by the ''major'' game version. <tt>02</tt>
<code>__GAME_VER_MAJ__ //02</code>


===__GAME_VER_MIN__  ===
===__GAME_VER_MIN__  ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Gets replaced by the ''minor'' game version. <tt>00</tt>
Gets replaced by the ''minor'' game version. <tt>00</tt>
<code>__GAME_VER_MIN__ //00</code>


===__GAME_BUILD__ ===
===__GAME_BUILD__ ===
{{Feature_arma3| Since {{arma3}} 2.01.}}
'''Description:''' Gets replaced by the build number. <tt>146790</tt>
Gets replaced by the build number. <tt>146790</tt>
<code>__GAME_BUILD__ //146790</code>


== Errors ==
== Errors ==

Revision as of 17:29, 22 December 2020

Template:SideTOC The parser allows you to use macros in configs. Macros are a bit similar to functions in programming and allow you to use a single definition many times in the config, without having to duplicate the whole definition again and again. It also gives you a centralized place to correct errors in this definition. This page mainly refers to Operation Flashpoint, some examples won't work for Arma and Arma 2.
If you really want to dig into the depths of the preprocessor you'll need to confront it with a bunch of edge cases and see how it behaves. If you are interested in this kind of thing, you might want to have a look at the collection of test-cases linked at the end of this page.

Arma 3
In Arma 3, preprocessor commands are case-sensitive!


Parsing


Macros

Comments

A comment is a line within code that is not actually processed by the game engine. They are used to make code more readable or to add notes for future reference. The preprocessor removes all comments from the file before it is processed. Therefore, comments are never actually "seen" by the game engine.

Comments may span multiple lines, or only part of a line if needed.

// this is a single-line comment

/* this is a
multi-line
comment */

mycode = something; // only this part of the line is commented out

myArray = ["apple"/*,"banana*/,"pear"]; // // a portion in the middle of this line is commented out

#define

Using the #define instruction, you can define a keyword and assign a definition to it. The keyword may contain any letter, digit or underscore in arbitrary order, as long as it doesn't start with a digit (RegEx: [a-zA-Z_][0-9a-zA-Z_]*). As an example:

#define true 1

The above means that whenever true is used in a config, the parser will replace this with the value 1.

The define-statement does swallow all spaces in between the macro-keyword and any non-space-character in the body (Note that tabs aren't spaces! They don't get removed)

#define MACRO                     test
MACRO // preprocesses to test (without any spaces)

#define MACRO	test // There's a tab between MACRO and test
MACRO // preprocesses to "	test" (without quotes - they are only used to show that the tab character didn't get removed)

The space between the macro-keyword and the body is also fully optional (though very useful to tell the preprocessor where the macro name ends and where the body begins):

#define MACRO#test
MACRO // preprocesses to "test"

Arguments

You can add arguments to more complex macros, by including them between brackets after the keyword. For the name of the arguments the same rule as for the macro-keyword (see above) apply.

#define CAR(NAME) displayName = NAME;

If you now use CAR("Mini"), this will be replaced with displayName = "Mini";. Multiple arguments can also be used:

#define BLASTOFF(UNIT,RATE) UNIT setVelocity [0,0,RATE];

Macro arguments may be composed of any characters, as long as they do not contain a comma (because commas are used as argument-delimiters). If quotes are being used, they have to be balanced. The same applies to single-quotes This is because String detection is working in macro arguments - Therefore you can even pass in commas as macro argument as long as they are part of a String (This only works with Strings wrapped in double-quotes though). Note however that although the macro gets resolved properly, the comma gets removed from the String (probably a bug).

#define MACRO(arg) arg
MACRO("Some, content") // preprocesses to "Some content" (note the missing comma)

Quote escaping is also not supported in this context (neither with double- nor with single-quotes)

#define MACRO(arg) arg
MACRO("Some ""content""") // preprocesses to "Some ""content"""

Passing arrays with more than one element [el1,el2,...] as arguments into macros as well as any argument containing comas "some, sentence", will need a small workaround:

#define HINTARG(ARG) hint ("Passed argument: " + str ARG)

Incorrect usage:

HINTARG([1,2,3,4,5,6,7,8,9,0]); // ERROR, won't even compile

Correct usage:

#define array1 [1,2,3,4,5,6,7,8,9,0] 
HINTARG(array1); // SUCCESS

The argument replacement is performed before the expansion of the macro body. That means one doesn't have to worry about name-conflicts between argument-names of the current macro and already defined macros:

#define ONE foo
#define TWO(ONE) ONE
TWO(bar) // will preprocess to bar

Replacing parts of words

By default, you can only replace whole words by arguments. If you need to replace only part of a word, you can use the ## instruction. This is necessary when either the start or the end of the argument connects to another character that is not a ; (semi-colon) or   (space).

class NAME##_Button_Slider: RscText \
{ \
	model = \OFP2\Structures\Various\##FOLDER##\##FOLDER; \

You can also use the single # to convert an argument to a string.

statement = (this animate [#SEL, 0]); \

Multi-line

For longer definitions, you can stretch the macro across multiple lines. To create a multi-line definition, each line except the last one should end with a \ character:

#define DRAWBUTTON(NAME)\
	__EXEC(idcNav = idcNav + 4) \
	...
The backslash must be the last character in a line when defining a multi-line macro. Any character (including spaces) after the backslash will cause issues.

#undef

Undefine (delete) a macro previously set by the use of #define.

#undef NAME

#ifdef

You can use a simple if-then construction to check whether a certain set of definitions has already been made:

#ifdef NAME
	...text that will be used if NAME is defined...
#endif

IFDEFs cannot be nested. The preprocessor will generate errors for all inner definitions if the outer definition doesn't exist.

#ifndef

Same as #ifdef, but checks for absence of definition instead.

#ifndef NAME
	...text that will be used if NAME ''isn't'' defined...
#endif

#else

#ifndef NAME
	...text that will be used if NAME is -not- defined...
#else
	...text that will be used if NAME -is- defined...
#endif

#endif

This ends a conditional block as shown in the descriptions of #ifdef and #ifndef above.

#include

Copies the code from a target file and pastes it where #include directive is.

#include "file.hpp"
#include <file.txt> // Brackets are equivalent to quotation marks and may be used in their place.

Source directory is:


You can define a path beginning with:

  • drive (only with Arma_3_Startup_Parameters#Developer_Options -filePatching enabled):
    #include "D:\file.txt"
    
  • PBO with PBOPREFIX:
     #include "\myMod\myAddon\file.txt"
    
  • PBO (keep in mind that in this case, if the PBO's file name will be changed, all '#include' referencing it will need to be updated):
    #include"\myMod\myAddon\file.txt" // Arma 3\@myMod\addons\myAddon.pbo\file.txt;
    


To move to parent directory use '..' (two dots) (Supported in Arma 3 since v1.49.131707):

#include "..\file.sqf"

Preprocessor does not support the use of macros for pre-defined file names.

#define path "codestrip.txt"
#include path // this will cause an error

#

'#' (single hash) operator wraps the text with quotation marks.

#define STRINGIFY(s) #s;
#define FOO 123
test1 = STRINGIFY(123); //test1 = "123";
test2 = STRINGIFY(FOO); //test2 = "123";

This operator does only work on keywords following the rules for macro-names (see #define-section). If one wants to stringify anything else (like e.g. a number), one has to use a stringify-macro that takes an argument and stringifies that (as in the example above).

#define MACRO Test #123
MACRO // preprocesses to Test 123 - note that there aren't any quotes inserted

##

'##' (double hash) operator concatenates what's before the ## with what's after it.

#define GLUE(g1,g2) g1##g2
#define FOO 123
#define BAR 456
test1 = GLUE(123,456); //test1 = 123456;
test2 = GLUE(FOO,BAR); //test2 = 123456;

__EXEC

This config parser macro allows you to assign values to internal variables or just execute arbitrary code. The code inside __EXEC macros runs in parsingNamespace and variables defined in it will also be created in parsingNamespace. The variables can then be used to create more complex macros:

__EXEC(cat = 5 + 1;)
__EXEC(lev = cat - 2;)
_cat = parsingNamespace getVariable "cat"; // 6
_lev = parsingNamespace getVariable "lev"; // 4
__EXEC macros are not suitable for SQF/SQS scripts but can be used in configs, including description.ext
__EXEC doesn't like round brackets () inside expressions. If you need to have grouping, perhaps you could calculate values inside the brackets separately and assign to local variables:

__EXEC(a = (1+2);) // ERROR

__EXEC(_expr = 1+2;) __EXEC(a = _expr;) // OK

__EVAL

With this config parser macro you can evaluate expressions, including previously assigned internal variables. Unlike with __EXEC, __EVAL supports multiple parentheses

w = __EVAL(safeZoneW - (5 * ((1 / (getResolution select 2)) * 1.25 * 4)));

__EVAL macros MUST be assigned to a config property and the expression MUST be terminated with ;. __EVAL can return only 2 types of data: Number and String. Any other type is represented as String, even Boolean type, which will result in either "true" or "false".

__EVAL macros are not suitable for SQF/SQS scripts but can be used in configs, including description.ext. Both global and local variables set in __EXEC are available in __EVAL
__EVAL doesn't like curly brackets {}, if you need to have code in your expression use compile String instead:

result = __EVAL(call {123}); // ERROR

result = __EVAL(call compile "123"); // OK


__LINE__

This keyword gets replaced with the line number in the file where it is found. For example, if __LINE__ is found on the 10th line of a file, the word __LINE__ will be replaced with the number 10.

__FILE__

This keyword gets replaced with the CURRENT file being processed.

Arma 3
The following macros are available since Arma 3 2.01.

__DATE_ARR__

Description: Is replaced for current date in format array. __DATE_ARR__ //2020,10,28,15,17,42

__DATE_STR__

Description: Is replaced for current date in format string. __DATE_STR__ //"2020/10/28, 15:17:42"

__DATE_STR__

Description: Is replaced for current date in format string. __DATE_STR__ //"2020/10/28, 15:17:42"

__DATE_STR_ISO8601__

Description: Is replaced for current date in format string. The date is presented in ISO8601 standard. __DATE_STR_ISO8601__ //"2020-10-28T14:17:42Z"

__TIME__

Description: Is replaced for current time. __TIME__ //15:17:42

__TIME_UTC__

Description: Is replaced for UTC time. __TIME_UTC__ //14:17:42

__COUNTER__

Description: Counts 1 up everytime it is defined. __COUNTER__ //0 __COUNTER__ //2 __COUNTER__ //3 __COUNTER_RESET__ __COUNTER__ //0 __COUNTER__ //1 __COUNTER__ //2

__COUNTER_RESET__

Description: Resets counter. See example above.

__RAND_INTN

Description: Gets replaced by a random N bit integer. N can be replaced by 8, 16, 32 and 64 __RAND_INT8__ //102

__RAND_UINTN

Description: Gets replaced by a random unsigned N bit integer. N can be replaced by 8, 16, 32 and 64 __RAND_UINT8__ //108

__GAME_VER__

Description: Gets replaced by the game version. __GAME_VER__ //02.00.146790

__GAME_VER_MAJ__

Description: Gets replaced by the major game version. __GAME_VER_MAJ__ //02

__GAME_VER_MIN__

Description: Gets replaced by the minor game version. 00 __GAME_VER_MIN__ //00

__GAME_BUILD__

Description: Gets replaced by the build number. 146790 __GAME_BUILD__ //146790

Errors

Error 2

Problem
Preprocessor failed error 2.
How to fix
Add quotation marks in the title, or the file path. (e.g. #include "soGood.sqf").

Error 6

Problem
Preprocessor failed on file X - error 6.
Known reasons
  • "The problem is using #ifdef #ifdef #endif #endif, a.k.a nested #ifdef. This doesn't work in Arma. It's only possible to use #ifdef and #endif once and not nested."
  • #endif without preceding #ifdef or #ifndef

Error 7

Problem
Preprocessor failed on file X - error 7.
Known reasons
The preprocessor encountered an unknown directive. Read, you have probably a typo in the file (something like #inlcude or #defien). Double check all preprocessor directives in that file.


External links