back to index

Operators

Operators play an import role in assign-statements and expressions; complex expressions are created by combining two or more expressions using operators.

Operator classes and priorities

  1. Arithmetic and bit operators (+ - * / % | ^ & << >> ~)
  2. Boolean operators (! || && ^^)
  3. Relational operators (== <= != >= < >)
  4. Post- and pre-increment/decrement operators (++ --)
  5. Assignment operators (= *= /= %= &= += -= |= ^= >>= <<=)

Example:

    // first evaluate the expression 3*2, then subtract the result (6) // from 20, then add 10. Thus, the result is 24.
    int i = 10 + 20 - 3 * 2;

In order to create self contained subexpressions, for instance to assign priorities to them, an expression may be enclosed in () brackets:

Example:

    // first evaluate the expr (20-3), then multiply the result by 2 and // finally add 10. Thus the result is 44.
    int i = 10 + (20-3) * 2;

Assignment operators

The assignment operators are used to store the result value of an expression in a memory location, e.g. global and local variables, array and hashtable elements and class members.

The special assignment operator <= is used to copy objects by reference resp. bind "volatile" objects. Example:

String s,t;
s<=t; // delete previous "s" object, make "s" reference "t" (read-only)
      // "s" is valid until "t" is deleted.
t="hello, world.";
print s;

If the = char is preceded by an arithmetic operator, the previous value of a memory location can be taken into account when calculating its new seizure.

Consequently, the assignment operators are shortforms for the following assignment operations:

    l *= <expression>; equals l = l * <expression>;
    l /= <expression>; equals l = l / <expression>;
    l %= <expression>; equals l = l % <expression>;
    l &= <expression>; equals l = l & <expression>;
    l += <expression>; equals l = l + <expression>;
    l -= <expression>; equals l = l - <expression>;
    l |= <expression>; equals l = l | <expression>;
    l ^= <expression>; equals l = l ^ <expression>;
    l >>= <expression>; equals l = l >> <expression>;
    l <<= <expression>; equals l = l << <expression>;

Caveat: The target of an assignment must not be a complex expression, a.b.c.d=e; is not allowed for instance. The reason for this is that TkScript is not a strictly typed language and it very hard to tell (especially in case of such indirect assignments) which type an object actually has at runtime (i.e. which members/methods are actually available).

The following assignment targets are currently possible:

    myvar <assignop> <expr>;
    MyModule.myvar <assignop> <expr>;
    myclassinstance.member <assignop> <expr>;
    myarrayvar[] <assignop> <expr>;
    myhashvar[] <assignop> <expr>;
    myapiclassinstance.mymember <assignop> <expr>;
    myclassinstance.mymember <assignop> <expr>;
    myclassinstance.myarrayvar[] <assignop> <expr>;
    myclassinstance.myhashvar[] <assignop> <expr>;

The order of evaluation of subexpressions on the same priority level is not defined and rather up to the runtime module being used (e.g. JIT runtime vs. interpreted mode) so please do not assume left-to-right resp. right-to-left order evaluation.

Short-Circuit evaluation is not supported, i.e. all subexpressions are evaluated regardless whether the result of the entire expression could be determined already by its first subexpression:

Example:

    if(i && j && k) { /* ... */ }

This statement can be optimized like this:

Example:

    if i if j if k { /* ... */ }

Unary operators

Unary operators have the highest evaluation priority. In contrary to C++ or Java there is no unary typecast operator; this functionality is rather provided by builtin function calls (tcint(), tcfloat(), tcchar(), tcstring()).

Example:

    int i = -1; // negation, result is -1.

Example:

    int i = ~1; // bitwise not, result is -2 (0xFFFFFFFE on 32bit architectures)


Example:

    int i = !1; // logical not, result is 0 

Binary operators

Ternary operators

The streaming << operator

The << operator is used to (de-)serialize objects from and to I/O stream objects.

Example:

    String s="hello, world.";

    Buffer b; b.size=512; // instanciate a Buffer(Stream)

    b << s; // serialize the string into the Buffer(Stream)

    s="..."; trace s; // unset string

    b.offset=0; // reset stream offset

    s << b; // deserialize the string from the Buffer(Stream)

    trace "s="+s;

Also see class serialization.

Example: please see testfileio.tks.html .


back to index

TkScript and the TkScript documentation are (c) Copyright 2001-2004 by Bastian Spiegel.