Squirrel Operators: Difference between revisions

From SigMod
Jump to navigation Jump to search
No edit summary
No edit summary
Line 262: Line 262:
For most purposes the above definitions for AND & OR are sufficient, however Squirrel does not simply return boolean true or false for these operators, rather it returns a specific operand passed to the operator based on the result.
For most purposes the above definitions for AND & OR are sufficient, however Squirrel does not simply return boolean true or false for these operators, rather it returns a specific operand passed to the operator based on the result.


Logical AND, if either operand evaluates to false, will return the '''first''' operand that evaluates to false. Otherwise if both evaluate true the operator will return the '''last''' operand.
Logical AND will return the '''first''' operand that evaluates to false, if none do and both evaluate true the operator will return the '''last''' operand.


Logical OR will return the first operand that evaluates to true, if none do, it will return the '''last''' operand.
Logical OR will return the first operand that evaluates to true, if none do, it will return the '''last''' operand.

Revision as of 20:45, 27 October 2023

BACK
HOME
NEXT

Now that you know how to create data and store it into variables, we can now go over how to modify and operate on that data. Operators are used for this purpose, they take one or more operands and produce a result value. Operators which take one operand are called unary operators, two operands are binary operators, and three are ternary operators. This section is quite long so don't worry about memorizing every single operator, just browse through and become familiar with the various types of operators and their uses, you'll have plenty of examples throughout the rest of the guide to work with.




Arithmetic

Unary Operators

Operator Symbol Form Operation
Unary minus - -x Negation of x
local x = 50;
x = -x; // -50


Binary Operators

Operator Symbol Form Operation
Addition + x + y x plus y
Subtraction - x - y x minus y
Multiplication * x * y x multiplied by y
Division / x / y x divided by y
Remainder % x % y the remainder from: x divided by y
local v = 100 + 100; // 200
local w = 0 - 2;     // -2
local x = 5.5 * 100; // 550.0
local y = 20 / 2;    // 10
local z = 23 % 2;    // 1

The addition operators + and += are also able to handle string operands. If one operand is a string, Squirrel will try to convert the other operand to a string as well. This same type conversion happens between floats and integers for any arithmetic operator (not just addition). If one operand is a float and the other an integer, the resulting value will be a float.

local str1 = "Hello,";
local str2 = " World!";
str1 += str2; // Hello World!
// See next section for +=

local float = 10 - 7.0 // 3.0




Assignment

Operator Symbol Form Operation
Assignment = x = y assign value y to variable x
Addition Assignment += x += y assign x + y to variable x
Subtraction Assignment -= x -= y assign x - y to variable x
Multiplication Assignment *= x *= y assign x * y to variable x
Division Assignment /= x /= y assign x / y to variable x
Remainder Assignment %= x %= y assign x % y to variable x
local x = 100;
x += 5;   // equivalent to: x = x + 5
x -= 5;   // 100
x *= 100; // 10,000
x /= 2;   // 5,000
x %= 2;   // 0




Increment/Decrement

Operator Symbol Form Operation
Pre-increment ++ ++x increment x by 1, then return x
Post-increment ++ x++ copy x, increment variable x by 1, then return the copy
Pre-decrement -- --x decrement x by 1, then return x
Post-decrement -- x-- copy x, decrement variable x by 1, then return the copy
local x = 1;
local y;

y = x++; // y = 1, it got the value of x and then x got incremented, x = 2
y = ++x; // y = 3, x got incremented and then y got x's value




Relational

Operator Symbol Form Operation
Equality == x == y true if x equals y, false otherwise
Inequality != x != y true if x does not equal y, false otherwise
Less than < x < y true if x is less than y, false otherwise
Greater than > x > y true if x is greater than y, false otherwise
Less than or equal to <= x <= y true if x is less than or equal to y, false otherwise
Greater than or equal to >= x >= y true if x is greater than or equal to y, false otherwise
local x = 5;
local y = 5;
local z;

z = x == y; // true
z = x != y; // false
z = x > y;  // false
z = x >= y; // true
z = x < y;  // false
z = x <= y; // true


Logical

Operator Symbol Form Operation
Logical NOT ! !x true if x evaluates to false, otherwise false
Logical AND && x && y true if both operands evaluate to true, otherwise false
Logical OR || x || y true if either operand evaluates to true, otherwise false

For most purposes the above definitions for AND & OR are sufficient, however Squirrel does not simply return boolean true or false for these operators, rather it returns a specific operand passed to the operator based on the result.

Logical AND will return the first operand that evaluates to false, if none do and both evaluate true the operator will return the last operand.

Logical OR will return the first operand that evaluates to true, if none do, it will return the last operand.


Certain values evaluate to false, these are: false, null, 0, 0.0. Everything else evaluates to true.

local v = 0 && 1;         // 0
local w = null || "bark"; // "bark"
local x = "" || true;     // ""
local y = !0;             // true
local z = 5 && "0"        // "0"

Bitwise

&, |, ^, ~, <<, >>, >>>

Other Operators

in, instanceof, typeof, comma, ternary


Operator Precedence

-, ~, !, typeof , ++, -- highest /, *, % … +, - <<, >>, >>> <, <=, >, >=, instanceof ==, !=, <=> & ^ &&, in +=, =, -=, /=, *=, %= … , (comma operator) lowest

IntegerLiteral  ::= [1-9][0-9]* | '0x' [0-9A-Fa-f]+ | [.]+ | 0[0-7]+ FloatLiteral  ::= [0-9]+ '.' [0-9]+ FloatLiteral  ::= [0-9]+ '.' 'e'|'E' '+'|'-' [0-9]+ StringLiteral  ::= '"'[.]* '"' VerbatimStringLiteral ::= '@"'[.]* '"'