Expressions
An expression is a sequence of operators and operands with a result. For example, the expression (3 * 4) has a result of 12. An expression can also have side effects. For example, a function may update the value of a controller global variable (which is not directly part of the expression) before returning a value.
An expression can have of one or more of the following:
-
Literals (3.0 or "Hello World")
-
Data Types and Variables ($myVar or $iglobal[0])
-
Operators and Intrinsic Functions (+ or <<)
-
Aerotech Standard Library API Functions (specifically those that return a value, StatusGetAxisItem())
-
User-Defined Functions (specifically those that return a value, MyAdd())
Literals
A literal is a token with a constant value that is known at compilation time, such as 1.5. This is unlike expressions that are known only at runtime, such as the position command of an axis.
Numeric Literals
Numeric literals include both integer literals and floating-point literals.
Integer Literals
Integer literals are any constant value that can be represented by the fundamental integer data type (integer). Integer literals can be either of the following types.
-
Decimal literals (base 10)
0|[1-9][0-9]*
-
Hexadecimal literals (base 16)
0x[0-9A-Fa-f]+
The example that follows shows how to correctly use integer literal types.
$hexval = 0xBEEF
$myVal = -0xaf
$xorVal = 0xA0A0A0A0 ^ 0xFFFFFFFF
Floating-Point Literals
Floating-point literals are any constant value that can be represented by a fundamental floating-point type (real). Floating-point literals include all of the strings that match the following regular expressions. Note that floating-point literals contain a superset of integer literals (for example, 1234 is a valid floating-point literal).
[0-9]+[Ee][+-]?[0-9]+
[0-9]*"."[0-9]+([Ee][+-]?[0-9]+)?
[0-9]+"."[0-9]*([Ee][+-]?[0-9]+)?
The example that follows shows many different ways that proper floating-point literals can be formed.
$myVar = -8
$expVal = 2E+09
$three = 3.
$dVal = -1e-3
$someVal = .3
$decimal = 0.25
$oneThousand = 1.e3
Care must be taken when using exponential notation with G-code due to the existence of the E command, which is part of the G-code dialect. Refer to G-Code and Exponential Notation for more information.
String Literals
A string literal is defined as a sequence of zero or more characters surrounded by double quote characters, as described by the following regular expression.
\"(\\.|[^\\"])*\"
The previous regular expression can be broken down into:
-
An opening double quote character (")
-
Any number (zero or more) of the following:
-
A backslash character (\) followed by any other character
-
Any character that is not a double quote character (any character except ")
-
-
A closing double quote character (")
The need for this seemingly odd way of specifying a string literal is due to the need to support escape sequences (such as the newline escape sequence \n) within a string literal. The escape sequence \" inserts a double quote character (") into the text of the string literal itself. Therefore, "Hello\" is an invalid string literal because the trailing double quote character (") is actually part of the escape sequence \", and not the string literal as a whole.
The example that follows shows proper usage of string literal types.
$myStr = StringUpper("abc123")
$myStr = ""
$myStr = "Hello" + "World"
String Character Escape Sequences
In addition to normal printable characters, escape sequences may be used in a string in order to use special characters that can’t be represented with normal keys on a keyboard.
Escape sequences can be useful for when a string must be displayed or printed, or be sent to another device (e.g., over a serial communication protocol or through a network socket).
// When printed, this string will display as the following:
// Hello
// World!
$myStr = "Hello\nWorld!"
// This string will be indented due to the tab (\t) escape sequence:
// Hey
$myStr2 = "\tHey"
Acceptable escape sequences are as follows. Specifying an invalid escape sequence, such as \c, within a string literal will cause a compiler error.
Escape Sequence | Definition |
---|---|
\a | Alert (“bell”) |
\b | Backspace |
\f | Form feed |
\n | Newline (or line-feed) |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\" | Double quote |
\\ | Backslash |
\nnn | ASCII![]() |
\xhh | ASCII![]() |
\Uhhhh | Unicode 16-bit code point (h=a hex digit 0-F) |
\Uhhhhhhhh | Unicode 32-bit code point (h=a hex digit 0-F) |
1. Octal escape sequences can only contain up to 3 octal digits. After parsing 3 octal digits, the compiler will continue parsing a string literal as normal, even if there are more octal digits. For example, the "\1024" string will be parsed as "\102" (the ASCII |
|
2. Hexadecimal escape sequences may contain any number of hexadecimal characters (0-F). However, this escape sequence can only be used to specify individual bytes, so sequences such as "\xFFFF" that are larger than the range that can be specified within a byte will be considered a compilation error. |
Operators and Intrinsic Functions
Mathematical, Logical, and Bitwise Operators
The following tables show a list of all operators that can be performed with numeric (those having a data type of integer or real) operands.
Assignment and Compound Assignment Operators
Assignment operators assign a value to a variable. Therefore, each of these operators must have a variable on the left hand side. However, the right hand side may be any expression whose data type is valid for the variable on the left hand side.
Operator | Name | Description |
---|---|---|
a = b
|
Simple assignment | Stores the value of b into a . |
a += b
|
Addition assignment | Calculates the expression a+b and stores the result into a . |
a -= b
|
Subtraction assignment | Calculates the expression a-b and stores the result into a . |
a *= b
|
Multiplication assignment | Calculates the expression a*b and stores the result into a . |
a /= b
|
Division assignment | Calculates the expression a/b and stores the result into a . |
a %= b
|
Modulo assignment | Calculates the expression a%b and stores the result into a . |
Assignment and Compound Assignment Operators Example
// Assign the value 3.5 to $rglobal[0]. $rglobal[0] = 3.5 // $rglobal[0] = 3.5 // Compute the sum of $rglobal[1] and 2.0 and assign the result to // $rglobal[1]. $rglobal[1] += 2.0 // $rglobal[1] = $rglobal[1] + 2.0 // Compute the difference of $iglobal[2] and 100 and assign the result // to $iglobal[2]. $iglobal[2] -= 100 // $iglobal[2] = $iglobal[2] - 100 // Compute the product of $iglobal[3] and 10 and assign the result to // $iglobal[3]. $iglobal[3] *= 10 // $iglobal[3] = $iglobal[3] * 10 // Compute the quotient of $rglobal[4] and 3.14 and assign the result // to $rglobal[4]. $rglobal[4] /= 3.14 // $rglobal[4] = $rglobal[4] / 3.14 // Compute the modulus of $iglobal[5] and 2 and assign the result to // $iglobal[5]. $iglobal[5] %= 2 // $iglobal[5] = $iglobal[5] % 2
Increment and Decrement Operators
Increment and decrement operators increase or decrease a numeric variable or property by a value of 1, respectively. The operand must be a variable or property.
Operator | Name | Description |
---|---|---|
a++
|
Increment | Increases the value of a by 1. |
a--
|
Decrement | Decreases the value of a by 1. |
Increment and Decrement Operators Example
// Add 1 to the value of $rglobal[0] and assign the result to // $rglobal[0]. $rglobal[0]++ // $rglobal[0] = $rglobal[0] + 1 // Subtract 1 from the value of $iglobal[1] and assign the result to // $iglobal[1]. $iglobal[1]-- // $iglobal[1] = $iglobal[1] - 1
Arithmetic Operators
Arithmetic operators do mathematical operations, such as addition and subtraction, on one or two numeric operands.
Operator | Name | Description |
---|---|---|
-a
|
Unary negation | Produces the negative of a . |
+a
|
Unary plus | Produces the value of a . |
a + b
|
Addition | Produces the sum of a and b . |
a - b
|
Subtraction | Produces the difference of a and b , where b is subtracted from a . |
a * b
|
Multiplication | Produces the product of a and b . |
a / b
|
Division | Produces the quotient of a and b , where a is divided by b . |
a % b
|
Modulo (Remainder) | Produces the modulus of a and b , where a is divided by b and the result is the remainder of the operation. |
a ** b
|
Exponentiation (Power) | Produces the result of a to the power of b . |
Arithmetic Operators Example
var $a as real = 4.0 var $b as real = 6.0 // Unary negation produces the negative of the operand. $rglobal[0] = -$a // $rglobal[0] = -4.0 // Unary plus produces the same value as the operand. $rglobal[1] = +$a // $rglobal[1] = 4.0 // Addition produces the sum of the operands. $rglobal[2] = $a + $b // $rglobal[2] = 10.0 $rglobal[3] = $a + $b + 10 // $rglobal[3] = 20.0 // Subtraction produces the difference of the operands. $rglobal[4] = $a - $b // $rglobal[4] = -2.0 $rglobal[5] = $b - $a // $rglobal[5] = 2.0 // Multiplication produces the product of the operands. $rglobal[6] = $a * $b // $rglobal[6] = 24.0 // Division produces the quotient of the operands. $rglobal[7] = $a / $b // $rglobal[7] = 0.66666... // When both operands of a division operation use the integer data // type, the result is an integer and any remainder is discarded. var $int1 as integer = 10 var $int2 as integer = 3 $rglobal[8] = $int1 / $int2 // $rglobal[8] = 3 $rglobal[9] = -199 / 100 // $rglobal[9] = -1 // You can prevent the remainder from being discarded when dividing two // integer operands by introducing an operand with the real data type. $rglobal[10] = 1.0 * $int1 / $int2 // $rglobal[10] = 3.33333... $rglobal[11] = -199.0 / 100 // $rglobal[11] = -1.99 // Modulo produces the remainder after performing a division operation // on the operands. $rglobal[12] = 20 % 4.5 // $rglobal[12] = 2.0 // 20 / 4.5 = 4.4444... // 20 - (4 * 4.5) = 2.0 $rglobal[13] = -2 % 10 // $rglobal[13] = -2 // -2 / 10 = -0.2 // -2 - (0 * 10) = -2 // The modulo operator in other programming languages can produce // different results compared to AeroScript. For example, some // programming languages evaluate -2 % 10 to a value of 8. AeroScript // can generate the same result by adding the second operand. $rglobal[14] = -2 % 10 // $rglobal[14] = -2 $rglobal[14] += 10 // $rglobal[14] = 8
Comparison and Relational Operators
Comparison and relational operators determine the relationship between two numeric operands.
Operator | Name | Description |
---|---|---|
a == b
|
Equal | Produces 1 if a is equal to b , if not produces 0. |
a != b
|
Not equal | Produces 1 if a is not equal to b , if not produces 0. |
a > b
|
Greater than | Produces 1 if a is greater than b , if not produces 0. |
a >= b
|
Greater than or equal to | Produces 1 if a is greater than or equal to b , if not produces 0. |
a < b
|
Less than | Produces 1 if a is less than b , if not produces 0. |
a <= b
|
Less than or equal to | Produces 1 if a is less than or equal to b , if not produces 0. |
Comparison and Relational Operators Example
// The "=" operator produces 1 if both operands are equal. $rglobal[0] = 10 == 10 // $rglobal[0] = 1 $rglobal[1] = 10 == 20 // $rglobal[1] = 0 // The "!=" operator produces 1 if the operands are not equal. $rglobal[2] = 10 != 10 // $rglobal[2] = 0 $rglobal[3] = 10 != 20 // $rglobal[3] = 1 // The ">" operator produces 1 if the operand on the left is greater // than the operand on the right. $rglobal[4] = 100 > -100 // $rglobal[4] = 1 $rglobal[5] = -100 > 100 // $rglobal[5] = 0 // The ">=" operator produces 1 if the operand on the left is greater // than or equal to the operand on the right. $rglobal[6] = 10 >= 5 // $rglobal[6] = 1 $rglobal[7] = 10 >= 10 // $rglobal[7] = 1 $rglobal[8] = 10 >= 20 // $rglobal[8] = 0 // The "<" operator produces 1 if the operand on the left is less than // the operand on the right. $rglobal[9] = 10 < 20 // $rglobal[9] = 1 $rglobal[10] = 20 < 10 // $rglobal[10] = 0 // The "<=" operator produces 1 if the operand on the left is less than // or equal to the operand on the right. $rglobal[11] = 7 <= 15 // $rglobal[11] = 1 $rglobal[12] = 15 <= 15 // $rglobal[12] = 1 $rglobal[13] = 20 <= 15 // $rglobal[13] = 0 // Comparison and relational operators are commonly used to execute // code conditionally. The following loop will run until $iglobal[0] // is not less than 10. $iglobal[0] = 0 while $iglobal[0] < 10 $iglobal[0]++ end
Logical Operators
Logical operators calculate if a condition is true or false.
Operator | Name | Description |
---|---|---|
!a
|
Logical NOT | Produces 1 if a has a value of 0, if not, produces 0. |
a && b
|
Logical AND | Produces 1 if a and b have non-zero values, if not, produces 0. |
a || b
|
Logical OR |
Produces 1 if |
Logical Operators Example
// Logical NOT produces 1 if the operand is zero or 0 otherwise. $rglobal[0] = !33.3 // $rglobal[0] = 0 $rglobal[1] = !0 // $rglobal[1] = 1 // Logical AND produces 1 if both operands are non-zero. $rglobal[2] = 5 && 10 // $rglobal[2] = 1 $rglobal[3] = 5 && 0 // $rglobal[3] = 0 $rglobal[4] = 0 && 10 // $rglobal[4] = 0 $rglobal[5] = 0 && 0 // $rglobal[5] = 0 // Logical OR produces 1 if either operand is non-zero. $rglobal[6] = 5 || 10 // $rglobal[6] = 1 $rglobal[7] = 5 || 0 // $rglobal[7] = 1 $rglobal[8] = 0 || 10 // $rglobal[8] = 1 $rglobal[9] = 0 || 0 // $rglobal[9] = 0 // Logical operators are commonly used to execute code conditionally. // The following loop will run as both $iglobal[0] and $iglobal[1] are // positive. $iglobal[0] = 1000 $iglobal[1] = 75 while ($iglobal[0] > 0) && ($iglobal[1] > 0) $iglobal[0] -= 20 $iglobal[1] -= 2 end
Bitwise Operators
Bitwise operators do operations on individual bits of one or two numeric operands. The operands that you specify to bitwise operations must have integer values. For example, 10 and 3.000000 are integer values, but 3.500000 is not an integer value.
Operator | Name | Description |
---|---|---|
a & b
|
Bitwise AND | Compares each bit of a to the corresponding bit of b . If both bits are 1, the corresponding bit in the result is set to 1. If not, the corresponding bit in the result is set to 0. |
a | b
|
Bitwise OR | Compares each bit of a to the corresponding bit of b . If either bit is 1, the corresponding bit in the result is set to 1. If not, the corresponding bit in the result is set to 0. |
a ^ b
|
Bitwise XOR | Compares each bit of a to the corresponding bit of b . If one bit is 1, and the other bit is 0, the corresponding bit in the result is set to 1. If not, the corresponding bit in the result is set to 0. |
~a
|
Bitwise NOT |
Produces the result of performing the logical negation of each bit of |
a << b
|
Bitwise left shift | Produces the result of shifting the value of a to the left by b bits. The most significant b bits are discarded and the least significant b bits are replaced with 0 bits. |
a >> b
|
Bitwise right shift | Produces the result of shifting the value of a to the right by b bits. The least significant b bits are discarded and the most significant b bits are replaced with the most significant bit of a . |
Bitwise Operators Example
// Bitwise AND produces a 1 bit when both operand bits are 1. $iglobal[0] = 986 & // 1111011010 437 // 0110110101 // $iglobal[0] = 400 (0110010000) // Bitwise OR produces a 1 bit when either operand bit is 1. $iglobal[1] = 0xaa | // 10101010 0x55 // 01010101 // $iglobal[1] = 255 (11111111) // Bitwise XOR produces a 1 bit when one operand bit is 0 and one // operand bit is 1. $iglobal[2] = 369 ^ // 101110001 99 // 001100011 // $iglobal[2] = 274 (100010010) // Bitwise NOT produces a 1 bit when the operand bit is 0 and produces // a 0 bit when the operand bit is 1. $iglobal[3] = ~0xFFFFFFFF83FAC921 // 0xFFFFFFFF83FAC921 has the following value in binary: // 1111111111111111111111111111111110000011111110101100100100100001 // $iglobal[3] = 2080716510 (1111100000001010011011011011110) // Bitwise left shift discards bits on the left and inserts bits with a // value of 0 on the right. $iglobal[4] = 11269517722451160197 << 3 // 11269517722451160197 has the following value in binary: // 1001110001100101010111101010100011111100101010111010110010000101 // $iglobal[4] = 16369165484771075112 whose binary value is as follows: // 1110001100101010111101010100011111100101010111010110010000101000 // Bitwise right shift discards bits on the right and inserts bits with // the value of the most significant (leftmost) bit on the left. $iglobal[5] = 255 >> 4 // 255 has the following binary value: 11111111 (leftmost bits are 0) // $iglobal[4] = 15 (00001111) $iglobal[6] = 0xb068e13e28a999a9 >> 32 // 0xb068e13e28a999a9 has the following binary value: // 1011000001101000111000010011111000101000101010011001100110101001 // $iglobal[6] = -1335303874 whose binary value is as follows: // 1111111111111111111111111111111110110000011010001110000100111110
Array, Property, and Structure Access Operators
Array, property, and structure operators access an element of an array or property, or a member of a structure.
Operator | Name | Description |
---|---|---|
a[b]
|
Array element indexing | Produces the element of the array or property a at index b . See Data Types and Variables for more information. |
a.b
|
Structure member access | Produces the member b of the structure a at index b . See Data Types and Variables for more information. |
Array, Property, and Structure Operators Example
// This loop makes the following assignments to the $rglobal property. // $rglobal[5] = 5 // $rglobal[6] = 6 // $rglobal[7] = 7 // $rglobal[8] = 8 // $rglobal[9] = 9 // $rglobal[10] = 10 for $iglobal[0] = 5 to 10 $rglobal[$iglobal[0]] = $iglobal[0] end // Multi-dimensional arrays must be accessed using multiple array // element indices. var $myArray[5][5] as integer $myArray[3][4] = 75 // This is an example of accessing a struct member. struct Position $x as real $y as real end var $pos as Position $pos.x = 10.0 $pos.y = 20.0
String Operators
String operators do assignment or concatenation operations on two string operands.
Operator | Name | Description |
---|---|---|
a = b
|
String assignment | Stores the value of string b into the string variable or property a . |
a += b
|
String concatenation assignment | Concatenates strings a and b and stores the result into a . |
a + b
|
String concatenation | Concatenates strings a and b . |
String Operators Example
// Assign the string "position" to $sglobal[0]. $sglobal[0] = "position" // $sglobal[0] = "position" // Compute the concatenation of $sglobal[0] and "command" and assign // the result to $sglobal[0]. $sglobal[0] += " command" // $sglobal[0] = "position command" // Compute the concatenation of "hello" and "world". $sglobal[1] = "hello" + "world" // $sglobal[1] = "helloworld"
Axis Operators
Axis operators produce a value with the axis data type from an integer, real, axis, or string operand.
Operator | Name | Description |
---|---|---|
@a
|
Axis conversion | Converts the operand a with the integer, real, or string data type to a value with the axis data type. |
Axis Operators Example
var $myAxis as axis // Set $myAxis to the axis whose index is 0. $myAxis = @0 // Set $myAxis to the axis whose index matches the value in $rglobal[1]. $myAxis = @$rglobal[1] // Set $myAxis to the axis whose name matches the value in $sglobal[2]. $myAxis = @$sglobal[2] // Set $myAxis to the axis whose name matches the string "X". // Both of the following statements assign axis X to $myAxis. $myAxis = @"X" $myAxis = X
Intrinsic Functions
Intrinsic functions are built into the syntax of AeroScript to perform miscellaneous operations.
Operator | Name | Description |
---|---|---|
argument(a)
|
Custom G-code or M-code argument value | Produces the value of argument a if the argument was specified with a custom G-code or M-code. If not, there is a task error. See for more information. |
defined(a)
|
Custom G-code or M-code argument existence | Produces 1 if the argument a was |
Length(a)
|
Array length | Returns the number of elements in the array variable a. |
Intrinsic Functions Example
// length() produces the number of elements in a one-dimensional array. var $oneDimensionalArray[10] as real $iglobal[0] = length($oneDimensionalArray) // $iglobal[0] = 10 // Arrays can consist of two or three dimensions. var $twoDimensionalArray[5][20] as integer $iglobal[1] = length($twoDimensionalArray) // $iglobal[1] = 5 $iglobal[2] = length($twoDimensionalArray[3]) // $iglobal[2] = 20 var $threeDimensionalArray[2][4][6] as integer $iglobal[3] = length($threeDimensionalArray) // $iglobal[3] = 2 $iglobal[4] = length($threeDimensionalArray[0]) // $iglobal[4] = 4 $iglobal[5] = length($threeDimensionalArray[1][1]) // $iglobal[5] = 6
Operation Result Data Type
An operation with two numeric operands of the same type will yield a result of the same type. For example, the addition of an integer and another integer produces an integer.
When performing an assignment, arithmetic, or relational operation with two numeric operands, one of the operands may be promoted to a different type before the operation takes place. For example, adding an integer value to a real value produces a result whose type is real. Bitwise operations always produce an integer value, regardless of the types of the operands.
A string can only be part of an operation with other string values. For example, you cannot add an integer value to a string value. However, functionality exists in the API to convert between types explicitly. See Variable Type Conversions for more information on this topic.
Other data types, such as axis and handle, can only be used in very specific operations. For example, an error will occur during compilation for an operation where an axis is added to an integer, or a handle is multiplied by a real.
Short-Circuiting
Short-circuiting is when the result of a conditional expression is able to be determined before all of its arguments have been evaluated. This behavior can improve the performance of program execution because in some cases, not all expressions need to be completely evaluated.
Two of the logical operators, && (logical AND) and || (logical OR), have short-circuiting behavior. If the first operand to the && operator is false, then the operation returns false regardless of the value of the second operand. The second operand does not have to be evaluated in this case. If the first operand to the || operator is true, then the operation returns true regardless of the second operand. Again, the second operand does not need to be evaluated in this case.
// This code is never executed regardless of the value of $iglobal[0]
Enable(X)
end
if (1 || $rglobal[0])
// This code is always executed regardless of the value of $rglobal[0]
Enable(X)
end
Short-circuiting behavior can be used to avoid side effects. In the following example, MyFunc() will not execute if $rglobal[0] is less than or equal to 0.
// MyFunc() won't execute in the above conditional
end
Short-circuiting is also commonly used to prevent a value from being used in a conditional expression further to the right, for example, if the programmer knows that evaluating some condition may cause an error. In C/C++, short-circuiting is used in many cases to prevent NULL pointers from being used in an expression. Another example is as follows, where short-circuiting can be used to prevent a division by zero within the conditional expression.
// The expression on the right is not evaluated if $rglobal[0] == 0
// ...
// ...
end
Operator Precedence
Operator precedence defines the rules for specifying the order in which operators in an expression are evaluated. These rules closely match the mathematical rules for order of operation, but are expanded to include operators that aren’t found in normal mathematics, such as bitwise operators. For example, multiplication has higher precedence than addition.
// The expression then becomes $myVal = $myVal + 6
// (the expression does not become $myVal = ($myVal + 2) * 3)
$myVal = $myVal + 2 * 3
Operator associativity is used to determine how to group operands with the same precedence. Left-associative operations, such as addition and multiplication, group operands starting from the left. Right-associative operations, such as exponentiation or variable assignment, group operands starting from the right.
// The expression below is evaluated as $result = ($valA * $valB) * $valC
$result = $valA * $valB * $valC
// Exponentiation is right-associative
// The expression below is evaluated as $result = $valA ** ($valB ** $valC)
$result = $valA ** $valB ** $valC
Parentheses should be used to force operations in an expression to have a specific precedence. In addition, precedence rules vary slightly between languages, and using multiple operators in an expression is error-prone, so parentheses should be used to clarify the intent of an expression.
var $val1 = 1 + 2 * 3 + 4
// Adding parentheses overrides the default precedence, so $val2 = 21
var $val2 = (1 + 2) * (3 + 4)
The table that follows shows the precedence of each operator in the language. Operators that are listed higher in the table have precedence over operators that are listed lower in the table. Operators in the same row have the same precedence.
Operator |
Name |
Associativity |
---|---|---|
|
Array element indexing Function call Structure member access |
Left-to-right |
a ** b
|
Exponentiation (power) |
Right-to-left |
|
Unary plus Logical NOT Bitwise NOT Axis conversion |
Right-to-left |
|
Multiplication Division Modulo (remainder) |
Left-to-right |
|
Addition Subtraction |
Left-to-right |
|
Bitwise left shift Bitwise right shift |
Left-to-right |
|
Less than Less than or equal Greater than Greater than or equal |
Left-to-right |
|
Equal No equal |
Left-to-right |
a & b
|
Bitwise AND |
Left-to-right |
a ^ b
|
Bitwise XOR |
Left-to-right |
a | b
|
Bitwise OR |
Left-to-right |
a && b
|
Logical AND |
Left-to-right |
a || b
|
Logical OR |
Left-to-right |
|
Simple assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulo assignment |
Right-to-left |
|
Increment Decrement |
Left- to-right |
Array Manipulation
Array Manipulation Overview
An array is a data structure that consists of a collection of elements, each having the same data type. Each element in an array can be accessed by its index. Array indices are zero-based, meaning that the first element in an array has an index of 0, and the last element in the array has an index of (array length - 1).
var $array[5] as integer
// Set the first element in the array to 10
$array[0] = 10
// Set the last element in the array to 20
$array[4] = 20
// The following causes an error because the array index is out of bounds
$array[8] = 30
Array indices may be constant values or the result of an expression. Array indices can be specified with an integer or real value, but they must be whole numbers.
var $index as integer
$index = 3
// Set $array[4] to 3
$array[$index + 1] = 5
// This is valid because the index is a whole number
// Set $array[3] to 3
$array[3.00000] = 3
// The following will generate an error
$array[2.5] = 123
Advanced Array Manipulation
Every array has a length (number of elements) that cannot be changed after it is created. Users can retrieve the length of an array by using the intrinsic length() function. This is especially useful in cases where the length of an array is not known, such as when an array is passed into a function that accepts an array of any length.
var $arr1[] as axis = [X, Y, Z]
var $arr2[6] as axis = [X, Y, Z, U, A, B]
MyEnable($arr1)
MyEnable($arr2)
MyEnable([D])
end
function MyEnable($axes[] as axis)
// The arrays of axes passed in can be any size
// Use the length function to get the actual size of the array
var $numAxes as integer = length($axes)
var $index as integer
for $index = 0 to $numAxes - 1
Enable($axes[$index])
end
end
The intrinsic length() function behaves in a straightforward way with one-dimensional arrays. However, for multi-dimensional arrays, the length() function returns the number of elements in the dimension that is specified.
var $len as integer
// Sets $len to 4 because the first dimension of $myArray contains 4
// elements
$len = length($myArray)
// Sets $len to 1 because the second dimension of $myArray contains 1
// element
$len = length($myArray[0])
// Sets $len to 7 because the third dimension of $myArray contains 7
// elements
$len = length($myArray[1][2])
// Causes an error because $myArray[0][0][0] is a scalar (not an array)
$len = length($myArray[0][0][0])
As an alternative to finding the length of an array and iterating over each array element by its index in a for loop, a foreach loop can be used to iterate over an array without needing to take its length into account.
var $sum as integer = 0
// Iterate through each element of $ints[], using $int as the "iterator"
foreach var $int in $ints
$sum += $int
end
Multi-dimensional arrays can be considered an “array of arrays.” For example, a variable that is a 3-by-3 array can also be thought of as an array with 3 elements, and each of those elements is itself an array with 3 elements. This is particularly useful in a case like the following scenario.
var $matrix[3][3] as integer = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
var $sum as integer = 0
// SumRow() takes 1D arrays, but $matrix is a 2D array
// However, $matrix[0], $matrix[1], and $matrix[2] are each 1D arrays
$sum += SumRow($matrix[0])
$sum += SumRow($matrix[1])
$sum += SumRow($matrix[2])
end
// The following function takes a 1-dimensional array and returns its sum
function SumRow($row[] as integer) as integer
var $sum as integer = 0
foreach var $val in $row
$sum += $val
end
return $sum
end
String Manipulation
The string data type is immutable, meaning that the value of a string cannot be changed. As a result, each string modification actually results in the creation of a new string.
var $str2 as string
// Assign a string variable to a string "object"
$myStr = "Hello"
// Create a new string from the character at index 1 in $myStr ("e")
$str2 = StringCharacterAt($myStr, 1)
// Create a new string from a substring of a string
// The following sets $str2 to "ello"
$str2 = StringSubstring($myStr, 1, StringLength($myStr))
// Modifying a character in a string can be accomplished by creating a new
// string from an existing string. The following produces "Yello"
$myStr = "Y" + StringSubstring($myStr, 1, 4)
Next Topic: Statements