Difference between revisions of "LSL Operators"

From Second Life Wiki
Jump to: navigation, search
(work in progress)
(Operator precedence)
Line 40: Line 40:
 
| <nowiki>|</nowiki>  || Bitwise OR  || four = 4 <nowiki>|</nowiki> 4;
 
| <nowiki>|</nowiki>  || Bitwise OR  || four = 4 <nowiki>|</nowiki> 4;
 
six = 4 <nowiki>|</nowiki> 2;
 
six = 4 <nowiki>|</nowiki> 2;
|-
 
| &&  ||Comparison AND  || isFalse = (FALSE && TRUE);
 
 
|-
 
|-
 
| <nowiki>||</nowiki>  || Comparison OR  || isTrue = (FALSE <nowiki>||</nowiki> TRUE);
 
| <nowiki>||</nowiki>  || Comparison OR  || isTrue = (FALSE <nowiki>||</nowiki> TRUE);
 +
|-
 +
| &&  ||Comparison AND  || isFalse = (FALSE && TRUE);
 
|-
 
|-
 
| = += -= *= /= %=  || Assignment  || four = 4;
 
| = += -= *= /= %=  || Assignment  || four = 4;
 
|}
 
|}
 +
 +
 +
'''Note:''' Unlike most languages, in LSL the || operator takes precedence over the && operator. Thus, this expression: (TRUE && TRUE || FALSE && FALSE) will evaluate as FALSE, as it is reduced to (TRUE && TRUE && FALSE).
  
 
{| {{Prettytable}}
 
{| {{Prettytable}}

Revision as of 04:33, 6 October 2007

Operators are used to cause an operation (or mathematical action) to be performed on two operands. The easy and common example is 1 + 2 where 1 and 2 are operands, and the + is the operator.

This concept can be extended much further with LSL since operands can be variables with the special case of the assignment operators requiring that the left hand side be a variable.

Operator Description Usage Example
() [] . Parenthesis, Brackets, and Dot do this second (do this first)
(type) Typecasting message = "The result is:" + (string) result;
 ! ~ ++ -- NOT, One's Complement, Increment, Decrement counter++;
* / % Multiply, Divide, Modulus rollover = (count + 1)%5;
- Subtraction one = 3 - 2;
+ Addition or joining Strings two = 1+1;

text = "Hello" + "World";

+ Concatenation or joining Lists myList = [1, 2, 3] + [4, 5];

newList = oldList + addList;

<< >> Left Shift, Right Shift eight = 4 << 1;
< <= > >= Less Than, Less Than Or Equal To,

Greater Than, Greater Than or Equal To

isFalse = (6 <= 4);
==  != Comparison Equal, Comparison Not Equal isFalse = ("this" == "that");
& Bitwise AND zero = 4 & 2;

four = 4 & 4;

^ Bitwise XOR zero = 4 ^ 4;

six = 4 ^ 2;

| Bitwise OR four = 4 | 4;

six = 4 | 2;

|| Comparison OR isTrue = (FALSE || TRUE);
&& Comparison AND isFalse = (FALSE && TRUE);
= += -= *= /= %= Assignment four = 4;


Note: Unlike most languages, in LSL the || operator takes precedence over the && operator. Thus, this expression: (TRUE && TRUE || FALSE && FALSE) will evaluate as FALSE, as it is reduced to (TRUE && TRUE && FALSE).

+ Operator

left + right

Left Type Right Type Result Type Description
integer integer integer Adds left and right
integer float float Adds left and right
float integer float Adds left and right
string string string Concatenates right onto the end of left.
list * list Concatenates right onto the end of left.
* list list Affixes left onto the start of right.
vector vector vector Adds left and right
rotation rotation rotation Adds left and right
Not useful for combining rotations, use * or / instead.