Up: TkScript Reference Guide » Operators
TkScript

#### reference guide | Operators

1. Operators
 Up: TkScript Reference Guide » Operators » Operators

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

1.1. Unary operators
Unary operators require only one expression / argument.

Unary sign `-`:
`int i = 42; int j = - i; // j => -42 `

Bitwise not `~`:
`int i = 0b1111111100000000; int j = ~ i; // j => -65281  (32bits!) `

Logical not `!`:
`boolean a = true; boolean b = ! a; // b => false (0) `
1.2. Binary operators
Binary operators require two expressions / arguments.

An expression formed of two expressions joined with a binary operator is also called a double-arg expression.

1.2.1. Arithmetic operators
`int i; i = 2 + 3;  //  +  Add i = 2 - 3;  //  -  Subtract i = 2 * 3;  //  *  Multiply i = 6 / 2;  //  /  Divide  i = 6 % 5;  //  %  Modulo (floats will be cast to int !) `
1.2.2. Bitwise operators
`int i; i =   1 << 8;  //  <<  Logic shift left i = 256 >> 8;  //  >>  Arithmetic shift right i =   1  | 2;  //   |  Bitwise OR i =   3  & 1;  //   &  Bitwise AND i =   1  ^ 1;  //   ^  Bitwise EOR / XOR `
1.2.3. Relational operators
`boolean b; b = (1 == 1);  //  ==  Compare equal b = (1 != 0);  //  !=  Compare not equal b = (1 <= 1);  //  <=  Compare less or equal b = (1  < 2);  //   <  Compare less than b = (1 >= 1);  //  >=  Compare greater or equal b = (2  > 1);  //   >  Compare greater than `
1.2.3.1. float
Since the bit representation of two otherwise equal floating point values may differ, floats are compared using an EPSILON threshold.

The thresholds are currently defined as
`#define FLT_EPSILON 0.000001f #define DBL_EPSILON 0.000000000001 `
1.2.4. Logic operators
`boolean b; b = (true  || false);  //  ||  Logic OR b = (true  && true);   //  &&  Logic AND b = (false ^^ false);  //  ^^  Logic EOR `
1.3. Ternary operators
Ternary operators require three expressions / arguments.

An expression formed of three expressions joined by two operators is also called a ternary expression.

1.3.1. The mini-if expression
TkScript supports the ternary mini-if expression.

If the first expression evaluates to `true`, the result of the second expression is returned, otherwise the result of the third expression.
`boolean b = true; `String` s = b ? "true" : "false"; `

Also see here: `The "mini-if" ternary expression`.
1.3.2. The range expression
The range expression can be used to check whether a value is within a certain range:
`float f = 3.1415;  boolean b = (0 <= f < 2PI); `

Also see here: `The range expression`.
2. Assignment operators
 Up: TkScript Reference Guide » Operators » Assignment operators
If the `=` char is preceded by an arithmetic operator, the previous value of a memory location is taken into account when assigning a new value.
The assignment operators are basically shortforms for the following assignment operations:
 ``` l *= ; equals l = l * ; l /= ; equals l = l / ; l %= ; equals l = l % ; l &= ; equals l = l & ; l += ; equals l = l + ; l -= ; equals l = l - ; l |= ; equals l = l | ; l ^= ; equals l = l ^ ; l >>= ; equals l = l >> ; l <<= ; equals l = l << ; ```

The following assignment targets are currently possible:
` myvar          <assignop> <expr>;  MyModule.myvar <assignop> <expr>;   myarrayvar    [] <assignop> <expr>;  myhashvar     [] <assignop> <expr>;   mynativeclassinstance.mymember <assignop> <expr>;   myclassinstance.mymember      <assignop> <expr>;  myclassinstance.myarrayvar [] <assignop> <expr>;  myclassinstance.myhashvar  [] <assignop> <expr>; `
2.1. Pointer assignment operator
The `<=` pointer assignment operator is used to assign object references, along with the ownership flag, to a variable or class member.

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."; // Change "s" and "t" print s; `

Example:
`// Declare script class class MyClass {    int my_member;     static New(int _myMember) : MyClass     {       local MyClass c;       c.init(_myMember);       return deref c;    }     public method init(int _myMember) {       my_member = _myMember;    } }  // Spawn new instance of MyClass and assign to "o" `Object` o <= MyClass.New(42); `

For more about `Object` references, see here: `Object references (pointers)`.
3. Operator priorities
 Up: TkScript Reference Guide » Operators » Operator priorities
TkScript uses the following operator priority levels (in highest to lowest order):
 ``` ., (), [], ++, --, ~, !, (unary +), (unary -) *, /, % +, - <<, >> <, >, !=, ==, <=, >= &, ^, | &&, || ? ```

Example:
`int a = 2, b = 5; print a == 1+1 && b==1 + (1 << 1+1);  `

I recommend not to rely on operator priorities so much since it makes static code analysis harder. Instead, try to use brackets () to clarify the evaluation order:
`int a = 2, b = 5; print (a == (1+1)) && ( b == (1 + (1 << (1+1))) ); `
3.1. Object operator priorities

When objects are used in double-arg expressions, the scriptengine needs to decide which class type is used when spawning new objects.
This is done by querying their operator priorities.

TkScript / YAC use the following object operator priorities (in lowest to highest order):
`#define YAC_OP_PRIO_BOOLEAN  0x00001000 #define YAC_OP_PRIO_BYTE     0x00002000 #define YAC_OP_PRIO_SHORT    0x00003000 #define YAC_OP_PRIO_INTEGER  0x00004000 #define YAC_OP_PRIO_LONG     0x00005000 #define YAC_OP_PRIO_FLOAT    0x00006000 #define YAC_OP_PRIO_DOUBLE   0x00007000 #define YAC_OP_PRIO_STRING   0x00010000 `

Native C++ classes intended to be used in e.g. arithmetic expressions should overwrite their `yacOperatorPriority(`) method and return a priority between 0x1000 (boolean) and 0x10000 (string).

The following core API classes support querying their operator priority: `Boolean`, `Byte`, `Double`, `Float`, `Integer`, `Long`, `Short`, `String`, `UnsignedByte`, `UnsignedShort`, `UnsignedInteger`, `UnsignedLong`.

Example:
``Double` d = 1.0 / 3; float f = 10; trace #(d * f);  // Result is a new Double object  `

The `String` class has the highest operator priority:
``String` s = "i="; trace #(s + 42); // Result is a new String object `
4. Array operator
 Up: TkScript Reference Guide » Operators » Array operator
Some array-like container classes support the `[]` array operator which is used to read or write an array element.

Array elements are indexed from 0..numElements. The array index is usually an `int` value.

``Object` o <= [1, 2, 3]; // uses array initializer expression to create a new array object print o; `
4.2. Writing an array element

Arrays should be allocated before use:
``IntArray` pal; pal.alloc(256);        // Create space for 256 elements trace pal.numElements; // Print out number of used elements (0) trace pal.maxElements; // Print out total number of available elements (256) pal = #ff00eedd;    // Write first element `

Writing to an invalid index may resize the array and modify both `numElements` and `maxElements`:
``IntArray` ia; ia = 42; `
4.3. Array objects
4.3.1. PointerArray
A `PointerArray` can store object references along with the ownership flag.
Notice how deletable object references need to be wrapped in a `Value` object if they are passed to a native code method.
However, when writing array elements with the `[]` array operator, no wrapping is necessary.
``PointerArray` pa;  // wrap deletable String object in Value object and pass to add method pa.add(#(`String`("hello, world.")));   // replace array element #0 by constant string pa = "some string";   // Store deletable 64bit int object pa = UnsignedLong.News("0xffffffffffffffff");  `
4.3.2. Reading object references from arrays
Reading an object reference from an array `Object` using the `[]` operator never unlinks the object from its container:
``ValueArray` va; va = `String`("world"); trace #(va["hello"]); // Return non-deletable reference to String in ValueArray  `String` sHello <= va.getDeref(0); // Grab ownership for String in ValueArray trace #(deref sHello); `
4.4. The HashTable class
 Up: TkScript reference guide / Expressions » The #[] hash table expression
Some API classes, e.g. `HashTable`, can be indexed with a String (instead of an integer).

You could also call this an unordered container.

Example:
`// Instanciate hash table and create space for at least 32 elements `HashTable` ht; ht.alloc(32);  // Write simple ht["myhashelement"] = 42;  // Read simple print ht["myhashelement"];  // Hash of hashes ht["my value"] = #[a=1, b=3.1415, c="Hello, world.", d=#["nested a"=42, "nested b"=2PI]]; trace ht["my value"]; `

Reading an object reference in a `HashTable` using the `[]` operator never unlinks the object from its container:
``HashTable` ht; ht["hello"] = `String`("world"); trace #(ht["hello"]); // Return non-deletable reference to String in HashTable  `String` sHello <= ht.getDeref("hello"); // Grab ownership for String in HashTable trace #(deref sHello); `

Also see `The #[] hash table expression`.
5. The stream operator
 Up: TkScript Reference Guide » Operators » The stream operator TkScript reference guide / Classes » Members » Tags TkScript reference guide / Objects » Objects » The YAC_Object interface » Streams
The `<<` operator is used to (de-)serialize objects from and to I/O stream objects.

The (de-)serialization mechanism in TkScript also supports complex datastructures but in order to avoid infinite recursion, the following conditions must be met for any object member (array/hash element, class field, ..) to be serialized:
• Must be deletable
• Must not be a composite object within a composite object (e.g. an array of arrays)
• Class fields must have been tagged using the "tag" keyword to be considered for serialization

Example:
``String` s = "hello, world."; `Buffer` b;     // instanciate a Buffer(Stream) b.size = 512; // and allocate 512 bytes b << s;       // serialize the string into the Buffer(Stream) s = "...";    // unset string trace s;  b.offset = 0; // reset stream offset s << b;       // deserialize the string from the Buffer(Stream) trace "s=" + s; `

`auto-generated by "DOG", the TkScript document generator. Wed, 31/Dec/2008 15:53:35`