The syntax of KoalaScript is very similar to C and C++ programming languages.
Basic rules:
1. KoalaScript is case sensitive. So "a" and "A" will be different to KoalaScript interpreter.
2. Identifier: name for variables or functions must be started with a letter, then it can be followed by letters, numbers, or "_" (underscore character).
3. Constant: KoalaScript support number constant like 1234, and string constant which is enclosed by quote mark, either single quote mark, or double quote mark, like "abcd".
4. There can be multiple statement within one line, and the line feed (CR-LF) will be treated as a space. All the space or tab characters, unless they are in string constant, are just for separating syntax elements, any extra spaces or tabs will be ignored.
5. Comments: comments start with double slash "//", all text in the same line after "//" will then be ignored.
KoalaScript supports the following data types:
1. Integer, like 1234KoalaScript supports use of variables. Variables can be global or local to a function.
You don't need to declare a variable, but you need to initialize it before you can use it.
You don't need to declare the data type of a variable, KoalaScript will assign the appropriate one.
For example, the following statement:
a = 1234;
will assign a data type of "Integer" to variable "a". Another statement:
a = "abcd";
will change the data type of a to "String".
KoalaScript provides automatic data type conversion between integer and string. When converting string to integer, 0 is used when the string doesn’t represent an integer value.
KoalaScript supports the following types of statements:
1. Assignment
You can assign a value to a variable, like:
a = 1234;
You can assign a value to an element in array, like:
array[10] = "abcd";
You can also assign a value to a property of an object, like:
term.AutoWrap = 0;
All these statement must be ended with semicolon (";"), no matter if it’s the last statement of not.
2. Function or method call
You can call a method of an object, like
term.Wait("abcd", 10);
Or you can call a function you declare yourself, like:
MyFunc(1234);
All these statement must be ended with semicolon (";"), no matter if it’s the last statement of not.
3. Composite
Multiple statements enclosed by a pair of "{" and "}". For example:
{
a = 1234;
b = "abcd";
}
4. Conditional
Format: if (<expression>) <if clause> else <else clause>
This statement first evaluate the <expression>, if the result is not zero, the <if clause> will be execute, otherwise, the <else clause> (if any) will be executed.
For example:
if (a > 10) a = a - 10;
else a = a + 10;
<else clause> may be optional.
Both <if clause> or <else clause> can be composite statement, for example:
if (a > 10) {
a = a - 10;
b = b - 10;
} else {
a = a + 10;
b = b + 10;
}
5. Loop
Format: while (<expression>) <statement>
This statement first evaluate the <expression>, if the result is zero (or boolean false), it's finished, otherwise, the <statement> will be execute, and evaluation of <expression> is repeated and checked again, the loop continues until the result of <expression> gets zero (or boolean false).
For example, the following statements will initialize an array with 10 elements:
Index = 0;
while (Index < 10) {
Array[Index] = 0;
Index = Index + 1;
}
6. Return statement
Format: return;
This statement abort the execution of current function and immediately return. If you use this statement in the main function (the script body), the script will be terminated.
KoalaScript supports the following categories of operators:
1. Arithmetic:
+ - * / %
2. String
+ (Append second string to first string)
3. Comparison
== (Equal to. Can be used with both integer and string values)
!= (Not equal to. Can be used with both integer and string values)
< (Less than. Only for integer values)
> (Greater than. Only for integer values)
<= (Not greater than. Only for integer values)
>= (Not less than. Only for integer values)
4. Boolean
&& (Logical and)
|| (Logical or)
Note: when KoalaScript evaluate a boolean expression, the shortcut method is used. For example, during evaluation of a expression like this:
<expression 1> && <expression 2>
If KoalaScript detects the <expression 1> equals to false, it won’t evaluate the <expression 2> anymore, because no matter <expression 2> is, the final result will be always false.
You can declare your own functions using format:
function <function name>(<parameter list>)
{
<statements>
}
Parameters are separated by comma (","). You don't need to specify the data type of parameters, and if you don't have any parameter, leave the <parameter list> blank.
For example, the following function take a parameter and show a message:
function ShowMessage(param)
{
sys.MessageBox("The parameter is: " + param);
}
Functions must be declared before called.
All variables used in a function are considered local variables to this function unless they are declared in the main function (the script body).
How to Invoke KoalaScript Scripts