Expressions

Expressions

Expressions in scripts can contain any of the standard C operators:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • ~ (binary invert [one’s compliment])
  • ^ (binary xor)
  • & (binary and)
  • | (binary or)
  • % (modulus)
  • ++ (increment [prefix and postfix])
  • -- (decrement [prefix and postfix])
  • ?: (ternary)
  • << (shift left)
  • >> (shift right)

Parentheses “(” and “)” can be used to group expressions. For example:

(13 + 358) * (12 ^ 3)

is a valid expression. The following comparison operators can be used:

  • < (less than)
  • > (greater than)
  • <= (less than or equal)
  • >= (greater than or equal)
  • == (equal)
  • != (not equal)
  • ! (not)

For example,

(64 > 32)

would return the value 1. Any of the assignment operators +=, -=, *=, /=, &=, ^=, %=, |=, <<=, or >>= can also be used.

 

Numbers

Numbers may be entered in 3 formats: octal, hexadecimal, and decimal. See Introduction to Number Systems.

  • Decimal: 64
  • Octal: 0100 (0 at the beginning of the number)
  • Hexadecimal: 0x40

 

Characters

As with C, characters are treated internally as numeric constants, and as such can be used in the same types of mathematical operations as any numbers.

Characters are single quotes (') that enclose the desired character value. For example, 'A' represents the character A. Escape sequences are the same as in C:

  • '\a': Bell (alert)
  • '\b': Backspace
  • '\f': Form feed
  • '\n': New line
  • '\r': Carriage return
  • '\t': Horizontal tab
  • '\v': Vertical tab
  • '\'': Single quotation mark
  • '\"': Double quotation mark
  • '\\': Backslash
  • '\?': Literal question mark
  • '\ooo': ASCII character in octal notation
  • '\xhh': ASCII character in hexadecimal notation

Character quotes can surround entire strings (for example, 'L. Spiro'), however only the first character will be used as the character constant ('L' in this case).

Octal sequences are interpreted up to the first non-octal character, and then are cast down to either type char or type short (wchar_t) in the case of Unicode strings. This is in contrast with C, which would have the programmer specify two octal characters to form a single Unicode character (for example, L'\032\040').

Hexadecimal sequences are interpreted up to the first non-hexadecimal character, and then are cast down to either type char or type short (wchar_t) in the case of Unicode strings. This is in contrast with C, which would have the programmer specify two hexadecimal characters to form a single Unicode character (for example, L'\x32\x40').

Create a Unicode character by placing an L in front of it, for example, L'A'.

 

Strings

Strings are exactly the same as in C with the exception of how octal and hexadecimal escape sequences are parsed. As mentioned above, octal and hexadecimal escape sequences are parsed until the first invalid character and then cast down to either char or short (wchar_t).

Copyright © 2006 Shawn (L. Spiro) Wilcoxen