Expressions

Expressions can be used whenever numbers can be used in a resource script file. An expression is a sequence of operators and operands that are evaluated to derive a numeric result. The result of an expression can be used whenever Resource Compiler expects a numeric value. The precedence and associativity of operators will be the same as the standard C programming language. Expressions may be nested within other expressions.

An example of operator precedence is illustrated by the following expression:

    3 + 4 * 2

Because the multiplication operator (*) has a higher precedence than the addition operator (+), the computation 4 * 2 will be performed first.

An example of operator associativity is illustrated by the following expression:

    8 / 2 * 3

Because both the division (/) and multiplication (*) operators are at the same precedence level, their left-to-right associative property causes the computation 8 / 2 to be performed first.

A Unary Expression consists of one operand and a unary operator.

    (-1) is a unary expression.

A Binary Expression contains two operands separated by one operator.

    (3 + 4)  is a binary arithmetic expression having evaluated value as 7.
    (2 < 1)  is a binary logical expression having evaluated value as 0 (False).
    (1 < 2)  is a binary logical expression having evaluated value as 1 (True).
    (2 << 1) is a binary left shift expression having evaluated value as 4.
    (4 >> 1) is a binary right shift expression having evaluated value as 2.
    (15 % 4) is a binary modulus expression having evaluated value as 3 (remainder).

The following example specifies the resource ID as the number 12:

    ICON 12 myfile.ico

This same resource ID can also be specified in the form of an expression, as shown here:

    #define BASE_ID         10
    #define ICON_OFFSET_ID  2
    ICON (BASE_ID + ICON_OFFSET_ID) myfile.ico

The constants BASE_ID and ICON_OFFSET_ID will be replaced with their macro values. The result of the evaluated expression (10 + 2) will be 12 which is the same as in the first example. The number 10 and 2 are operands in the expression and the symbol plus (+) is the operator applied on operands 10 and 2.

Although the enclosing parentheses are not required, it is a good practice to enclose the expression in parentheses indicating that the whole expression constitutes a single parameter.

The following table mentions the valid operators that can be used in an expression.

┌────────────────────┬────────────────────┬────────────────────┐
│Operator            │Symbol              │Type                │
├────────────────────┼────────────────────┼────────────────────┤
│add                 │+                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│and                 │&                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│and if              │&&                  │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│bitwise exclusive or│^                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│bitwise negation    │~                   │unary               │
├────────────────────┼────────────────────┼────────────────────┤
│bitwise negation    │NOT                 │unary               │
├────────────────────┼────────────────────┼────────────────────┤
│divide              │/                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│equals              │=                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│greater than        │>                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│left shift          │<<                  │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│less than           │<                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│less than           │<                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│logical negation    │!                   │unary               │
├────────────────────┼────────────────────┼────────────────────┤
│minus               │-                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│modulus             │%                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│multiply            │*                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│not equal           │!=                  │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│not less than       │>=                  │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│not greater than    │<=                  │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│or                  │|                   │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│or else             │||                  │binary              │
├────────────────────┼────────────────────┼────────────────────┤
│right shift         │>>                  │binary              │
└────────────────────┴────────────────────┴────────────────────┘

Note: The unary NOT operator is used only in the style field of resource statements. If operator NOT is encountered with the operands in an expression, it will be translated as a ~ character.

Any typecast with an operand will be ignored and a warning message will be generated. For example, the expression ((long)3 + (short)2) has typecast (long) with operand 3 and typecast (short) with operand 2. The typecasts have no effect on expression evaluation.