Basic Language Mechanisms

From HLKitWiki
Jump to: navigation, search

Context: HL KitKit Reference 

The scripting language supports the following basic language mechanisms:

Assignment Variables and attributes can be assigned values from other variables, attributes, and literal values. Assignment statements typically take the traditional form "x = y".
Numeric Assignment Four additional assignment behaviors are supported that are exclusive to numeric values. The first two are the increment ("+=") assignment and decrement ("–=") assignment. Both are essentially a shorthand notation for the statements "x=x+y" and "x=x–y", respectively. Consequently, the statement "x+=y" is equivalent to "x=x+y". The other two are the multiply ("*=") assignment and divide ("/=") assignment, which are a shorthand for the statements "x=x*y" and "x=x/y", respectively. This assignment syntax can be incredibly convenient when identifiers are used with lengthy context transitions and target references.
String Assignment One additional assignment behavior is supported that is exclusive to string values. It is the concatenate ("&=") assignment, which is essentially a shorthand notation for the statement "x=x&y". Consequently, the statement "x&=y" is equivalent to "x=x&y". This can be incredibly convenient when identifiers are used with lengthy context transitions and target references.
Arithmetic Expressions Complex arithmetic expressions are fully supported by the scripting language. All of the standard operators are supported (+–*/), as is the use of parentheses to control the order of evaluation in an expression. An additional operator (%) is supported, which returns the remainder after dividing "x" by "y" (also called the "modulus").
String Expressions Strings can be concatenated together through string expressions. To concatenate two strings, use the '&' operator (e.g. "str1 & str2"). No other string operators are supported.
Implicit Type Casting The scripting language will automatically convert a variable or attribute between the two types when necessary. If you assign a numeric variable to a string, the value is converted to a string automatically (e.g. the value 521 is converted to the string "521"). Conversely, assigning a string variable to a number converts the string before the assignment takes place. However, going from a string to a number only converts the leading numeric portion of the string, so the string "1234hello789" is converted to the value 1234.

IMPORTANT! Type casting only occurs with individual variables/attributes and not with entire expressions. Therefore, it is not valid to assign an arithmetic expression to a string, although it is valid to use a string variable within an arithmetic expression. If you want to assign an arithmetic expression to a string variable, you must first assign the expression result to a numeric variable and then assign that variable to the string. Similarly, it is valid to use a string variable as a parameter to an intrinsic function where a number is required, or vice versa.

The following sample code demonstrates the basic language mechanisms in use:

~Declare a variable and put a number into it.
var myint as number
myint = 7

~Declare another variable and set it equal to an arithmetic expression.
var temp as number
temp = (myint + 100) * 3

~Decrement temp by the value of "myint" times three.
temp -= myint * 3

~Set the value of a string to a numeric value, converting it to a string.
var mystr as string
mystr = temp

~Append the contents of two strings
mystr = "123" & mystr

~Append text to the end of the current string
mystr &= "123"

~Set a numeric variable to a string, automatically converting the result.
myint = mystr