Expression Evaluation in Scripts

From C4 Engine Wiki

Jump to: navigation, search

A script in the C4 Engine can contain methods of the type “Evaluate Expression”, sometimes called an expression method for short. An expression method holds a text string representing a mathematical expression that gets evaluated when script execution reaches the method.

For an expression method, a text box is displayed on the left side of the Method Info window where a textual expression may be entered. (The Method Info window is opened by selecting a method and then typing Ctrl-I.) A field for designating an output variable also appears in the lower-left corner of the Method Info window.

If there is a syntax error in an expression, then the method is colored yellow in the script viewport.

Operators

Expressions can use any of the operators from the following list. They are listed in order of evaluation precedence, which matches the precedence rules for C++. Parentheses can be used to group subexpressions.

Operator

Description

Variable types accepted

Notes

*

Multiply

boolean, integer, float, color, vector, point

Colors can only be multiplied by other colors.

Vectors and points can only be multiplied by other vectors and points.

Multiplication is componentwise for colors, vectors, and points.

/

Divide

boolean, integer, float, color, vector, point

A color, vector, or point type can only appear as the left operand.

Integer division by zero is defined to be zero.

%

Modulo

boolean, integer

If the right operand is zero, then the result is defined to be zero.

+

Plus

boolean, integer, float, string, color, vector, point

Colors can only be added to other colors.

Vectors and points can only be added to other vectors and points.

A unary plus is accepted and performs no operation.

Minus or Negate

boolean, integer, float, color, vector, point

Colors can only be subtracted from other colors.

Vectors and points can only be subtracted from other vectors and points.

<<

Shift Left

boolean, integer


>>

Shift Right

boolean, integer

The shift is always signed.

<

Less Than

boolean, integer, float

Always returns a boolean result.

>

Greater Than

boolean, integer, float

Always returns a boolean result.

<=

Less Than or Equal

boolean, integer, float

Always returns a boolean result.

>=

Greater Than or Equal

boolean, integer, float

Always returns a boolean result.

==

Equal

boolean, integer, float, string

Always returns a boolean result.

!=

Not Equal

boolean, integer, float, string

Always returns a boolean result.

&

Bitwise AND

boolean, integer


^

Bitwise XOR

boolean, integer


|

Bitwise OR

boolean, integer


~

Bitwise NOT

boolean, integer

This is a unary operator.

Note that the logical operators && and || are not present in the table. These were intentionally left out of the scripting language because they are not necessary. The same results can be attained by using the & and | operators instead. If the evaluation semantics of && and || are needed, then you can use conditional execution to evaluate the expression in pieces.

Operands

The operands in an expression can be literal integers or floats, literal strings in double quotes, or the names of any variables defined for the script. In any binary operation appearing in an expression, a mismatch between the types of the two operands causes one of the operands to be promoted to the type of the other. The possible promotions are those in the following list:

  • Boolean can be promoted to integer, float, or string.
  • Integer can be promoted to float or string.
  • Float can be promoted to string.

Result

An expression always produces an output value, and the boolean result for the expression method reflects the value of the expression. If the output value is not stored in a variable, then the boolean result is still valid. If the value of the expression is not boolean, then one of the following conversions is made to arrive at a boolean result:

  • For an integer, the value 0 becomes false, and any other value becomes true.
  • For a float, the value 0.0 becomes false, and any other value becomes true.
  • For a string, the empty string becomes false, and any other string becomes true.
Personal tools