A taakScript is a sequence of statements. Compound
statements are enclosed in curly braces (that is, a function definition
is considered a single statement). Legal statements include:
- Function definitions.
- Variable declarations.
- Import statements.
- Conditional statements.
- Looping statements.
- Assignments.
- Expressions.
- Print statements.
Syntax:
var identifier [=
initializer];
Examples:
var x
var list = ["one","two",3];
var nested_list = [0,1,[2,3]];
In general, global variables do not need to be declared in taak.
They are defined when they are used. Inside a function definition, local
variables may be created using the var keyword. The format is the var
keyword followed by an identifier followed by an optional initializer.
An identifier in Taak begins with a letter or underscore (_) followed
by zero or more letters, digits, or underscores. An initializer is any
taak literal, expression or a list initializer.
A list initializer is zero or more initializers separated by commas and
enclosed in square brackets. List initializers may be nested.
Syntax:
function identifier(arglist)
{
var one = 1;
return x + one;
}
Examples:
function addOne(x) {
var one = 1;
return x + one;
}
function doNothing() {
}
A function definition begins with the function keyword followed by the
function name and argument list enclosed in parentheses and the function
body enclosed in curly braces. The function name and argument names must
be legal taak identifiers. The arglist is zero
or more identifiers separated by commas. As in JavaScript the arguments
and return values are not typed. The arguments and any variables declared
inside a function body are local to the function. A function body may
contain one or more return statements.
Syntax:
functionname(arglist);
Examples:
y = addOne(1);
doNothing();
r = range(0, 10);
A function must be defined be fore it can be invoked. A function is invoked
by specifying its name follows by zero or more arguments enclosed in parentheses.
A function may or may not return a value. If no value is returned, its
value will be null.
Syntax:
[new]classname(arglist);
Examples:
s = new String();
a = ArrayList(10);
p = Properties();
The syntax of a new statement is the optional keyword new followed the
Java class name followed by an argument list enclosed in parentheses.
The class name is a fully qualified class name such as com.nevik.TaakCompiler
or an unqualified name such as String for Java classes in the packages
java.lang and java.util.
Syntax:
objectreference.methodname(arglist);
Examples:
a.clear();
a.add(1);
p.add("jsp","ugh");
p.add("perl","ok");
p.add("taak","cool");
The syntax for invoking a Java method is the same as in Java. It begins
with an object reference followed by a dot followed by the method name
followed by an argument list enclosed in parentheses. The parentheses
are required even if the argument list is empty.
In calling Java methods and constructors, the arguments may be converted
to match the method or constructor signature. The following conversions
may be done to find the matching method or constructor:
- If required Character, Byte, Short, Integer, Long, Float, Double are
converted to their primitive counterparts: char, byte, short, int, long,
float, and double.
- Numeric types may be converted to a larger type. For instance, Byte
to Short to Integer to Long to Float to Double.
- Numeric types may be converted to a smaller type. For instance Double
to Float to Long to Integer to Short to Byte.
- A derived class maybe converted to its super class or interface.
Where alternative methods or constructors exist the method with fewer
conversions is preferred over more conversions and conversions to larger
numeric types are preferred over conversions to smaller types.
Syntax:
left_reference = expression;
Examples:
x = 0;
u.name = "Buzz";
list[0] = x;
map["a"] = "apple";
An assignment sets the value of a left_reference. A left_reference is
a reference to a modifiable variable, field, mutator, list member, or
map entry that appears on the left side of an assignments. References
can combined where it is well defined. Some examples will make this clear.
Suppose there are two Java classes defined as follows:
public class User {
private String email;
public Name name;
public List accounts = new ArrayList();
public Map phonebook = new HashMap();
public String getEmail() {return email; }
public void setEmail(String email) {this.email = email;
}
public Boolean isActive {return Boolean.TRUE; }
}
public class Name {
public String first;
public String last;
public String toString() { return first.concat("
").concat(last); }
}
And suppose the variables u and v hold values of type User. Then the
following examples have the corresponding meaning:
| Example |
Meaning |
| u=v; |
Assign v to u |
| u.email="buzz@aol.com"; |
Invoke u.setEmail(“buzz@aol.com”) |
| u.name.last="Lightyear"; |
Assign “Lightyear” to u.name.last. |
| u.accounts[0]="98765"; |
Invoke u.account.set(0, “98765”) |
| u.phonebook["cell"]="877-9877"; |
Invoke u.phonebook.put(“cell”,”877-9877”) |
Similarly, a right_reference is a combination of a variable, field, accessor,
predicate, Java method, list member, or map entry that may appear on the
right hand side of an assignment.
| Example |
Meaning |
| u |
The value of u |
| u.email; |
The value of u.getEmail() |
| u.name.last; |
The value of u.name.last, i.e. "Lightyear' |
| u.accounts[0]; |
The value of u.accounts.get(0) |
| u.phonebook["cell"]; |
The value of u.phonebook.get("cell") |
| u.name.toString(); |
The value of u.name.toString(), i.e. "Buzz Lightyear" |
| u.name.last.substring(0,5); |
u.name.last.substring(0,5), i.e. "Light" |
Syntax:
if (expression) statement
[else statement]
Examples:
if (x == 0) y = "abc";
if (!b) {
A = 0;
} else {
a = 1;
}
if (c == 0) {
x = 1;
} else if (c == 1) {
x = 2;
} else if (c == 2) {
x = 4;
} else {
x = 8;
}
The if statement has the same syntax as in Java with the exception that
the condition does not have to be strictly boolean.
The following table specifies sample truth-values:
| Type |
Truth Value |
| Boolean |
A Boolean has the natural truth-value of its corresponding primitive
type. |
| numeric types |
0 is false. All other values are true. |
| string |
Null or empty string is false. The string "false" is false.
All other values are true. |
| objects |
Null is false. All other values are true. |
Syntax:
while (expression)
statement
Examples:
while (i < n) {
i = i + 1;
}
The while loop has the same syntax as in Java. The condition is evaluated
as in the if statement. Break and continue statements may be used inside
the body.
Syntax:
foreach identifier
in collection statement
Examples:
foreach i in ["one", "two",
"three"] {
println i;
}
foreach acct in accounts {
println acct;
}
foreach n in range(0, 10) {
println n;
}
The foreach loop iterates through a collection. It is like the following
idiom in Java:
Iterator iter = collection.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
System.out.println(obj);
}
To simulate a FORTRAN for statement, the range()
function can be use to generate a collection of integers. For example,
the Taak statement:
foreach n in range(0, 10, 2) {
println n;
}
is equivalent to the following Java statement:
for (int n = 0; n < 10; n += 2) {
System.out.println(n);
}
Syntax:
print arglist;
Examples:
print "hello";
print 1,2,3;
print 1,x+y;
The print statement outputs a string representation of its argument list
to standard output.
Syntax:
println arglist;
Examples:
println "hello";
println 1,2,3;
println 1,x+y;
The println statement is like the print statement, but also appends a
linefeed character.
Syntax:
import "filepath";
Examples:
import "myfunctions.taak";
import "util/options.taak";
The import statement is used to include
other taakScripts in the current script. One good use of import is to
include function definitions in another script. If the file path is not
absolute, it is interpreted relative to the including script.
Following is an example:
myfuncs.taak:
function negative(x) {
return x < 0;
}
function swap(x,y) {
var t = x;
x = y;
y = t;
}
myfuncs.taak:
import "myfuncs.taak";
if (negative(x)) {
swap(a,b);
}
Syntax:
exit;
Examples:
exit;
The exit statement terminates execution of the script.
|