operators - List of operators in Sncode.
The following operators are supported in Sncode:
All operators works will all type of variables: - undefined - integer - double - string - null - associative array - array - boolean.
Each operators as is own set of rules for the conversion unless casting is used.
For numerical operators, undefined and null values are considered equal to 0 whereas boolean values are treated as 1 when true and 0 when false.
For string operators, undefined and null values are treated as an empty string whereas boolean values are treated as "true" when true and "false" when false.
Here is the order for operators precedence:
Usage:
a = b + c;
a = b / c;// if b and c are integer, the result a will be an integer. If one of operands is double, a will be double.
| Operator | Description |
|---|---|
+ |
Addition |
- |
Substraction |
* |
Multiplication |
/ |
Division |
% |
Modulo |
** |
Power |
These operators assign a value to their left operand based on the value of their right operand.
Usage:
a = 1; a += 2; // a now equals 3
| Operator | Description |
|---|---|
= |
Assignment |
+= |
Addition assignment |
-= |
Subtraction assignment |
/= |
Division assignment |
%= |
Remainder assignment |
*= |
Multiplication assignment |
|= |
Bitwise OR assignment |
&= |
Bitwise AND assignment |
.+= |
String concatenation assignment |
>>= |
Left shift assignment |
<<= |
Right shift assignment |
< <= > >= != <> <⇒
• < : less than
• > : greater than
• <= : less or equal
• >= : greater or uqual
• <=> : compare
• -1 if less than
• 0 if equal
• 1 if greater than
• == : equal
• != : not equal
lt -- less than. Compare the length of 2 strings
le -- less. Compare the length of 2 strings
gt -- greater than. Compare the length of 2 strings
ge -- greater. Compare the length of 2 strings
eq -- equal
ne -- not equal
cmp -- compare two string but return -1, 0, or 1.
st -- check is the first string start with the second string. "/usr/local/website" st "/usr" return true; "/usr/local/website" st ".usr" return false
ns -- check is the first string not start with the second string. "/usr/local/website" ns "/usr" return false; "/usr/local/website" ns ".usr" return true
Usage:
if "2016-01-01" =~ "^(?P\d\d\d\d)-(?P\d\d)-(?P\d\d)$" then
| Operator | Description |
|---|---|
=~ |
Does the left expression match the pattern in the right expression (boolean) |
!~ |
Does the left expression NOT match the pattern in the right expression (boolean) |
Regular expressions support special flags to alter their behavior:
Usage:
if "2016" =~:iasm "^(?P\d\d\d\d)$" then
| Operator | Description |
|---|---|
x |
extended (PCRE_EXTENDED) |
i |
case insensitive (PCRE_CASELESS) |
m |
multiline (PCRE_MULTILINE) |
s |
dot match all (PCRE_DOTALL) |
For more details, please consult the PCRE Documentation.
Usage:
a = "Hello" .+ " World";
| Operator | Description |
|---|---|
.+ |
Appends the right expression to the left one |
-
Usage:
if !a || (a && b) then // if a is false or both variables are true
| Operator | Description |
|---|---|
! |
Not (is the expression false) |
&& |
And (are both expressions are true) |
|| |
Or (is either one of the expressions true) |
In Sncode, logical operators rely on lazy evaluation, which means that in both of these cases, the function fact won't even be called:
if 0 && fact(1) then // 0 means false so we don't need to go further
and
if 1 || fact(1) then // 1 means true so we don't need to go further
The benefits of lazy evaluation include:
~(not) |(or) &(and) ^(xor) <<(left shift) >>(right shift)
Usage:
if -e "/path/to/file.txt" then
If the path starts with a /, it is automatically prefixed with the path to the root directory. Otherwise, it is prefixed with the path of the current script.
| Operator | Description |
|---|---|
-e |
Check if the file exists (boolean) |
-z |
Check if the file has a size equal to 0 (boolean) |
-r |
Check if the file can be read (boolean) |
-w |
Check if the file can be open for writing (boolean) |
-x |
Check if the file can be executed (boolean) |
-l |
Check if the path points to a link (boolean) |
-f |
Check if the path points to a file (boolean) |
-d |
Check if the path points to a directory (boolean) |
-s |
Return the size of the file (in bytes) |
-m |
Return the modification time of the file (in secs.) since epoch |
-a |
Return the last access time of the file (in secs.) since epoch |
-c |
Return the creation time of file (in secs.) since epoch |
-u |
Return the owner (user) of the file |
-g |
Return the owner group of the file |
How to group operators
Written by Pierre Laplante <laplante@sednove.com>, Jean-Georges Guenard, <jg@sednove.com>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis.
1.0 2014-09-09 21:24:14 laplante@sednove.com