Overview of KoalaScript Language
Click the following links to
see the each language in KoalaScript:
The syntax
of KoalaScript is very similar to C and C++ programming languages.
Basic rules:
Data Types
KoalaScript supports the following
data types:
For example: a[1]. The subscription of an array starts from zero.
Variables
KoalaScript 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 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.
Statements
KoalaScript supports the following
types of statements:
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 is the last statement if not.
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 is the last statement of not.
Multiple statements enclosed by a pair of "{" and "}". For example:
{
a = 1234;
b = "abcd";
}
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;
}
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;
}
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.
Operators
KoalaScript supports the following
categories of operators:
+ - * / %
+ (Append second string to first string)
== (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)
&& (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.
Functions
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