Difference between revisions of "Operators"

From MiniScript Wiki
Jump to navigation Jump to search
(About chained comparison)
Line 46: Line 46:
 
|}
 
|}
  
== Chained comparison ==
+
== Chaining Operators ==
 +
 
 +
Most operators in MiniScript can be combined into chains, for example <c>39 + 2 + 1</c>.  The details depend on the type of operator:
 +
 
 +
=== Arithmetic Operators ===
 +
 
 +
In an expression involving a chain of arithmetic operators, the highest-precedent operators are evaluated first, as in standard algebra.  You can always use parentheses to break such a chain into smaller chunks, forcing a different evaluation order.
 +
 
 +
{| class="wikitable"
 +
|-
 +
! Code !! Result
 +
|-
 +
| <c>2 + 5 * 8</c> || 42
 +
|-
 +
| <c>(2 + 5) * 8</c> || 56
 +
|-
 +
| <c>3 ^ 2 + 4 ^ 2</c> || 25
 +
|}
 +
 
 +
=== Comparison Operators ===
 +
 
 +
In the case of comparison operators (<c>></c>, <c><</c>, <c>>=</c> and <c><=</c>), operands are compared in pairs from left to right.  All comparisons must be true for the complete chained expression to be considered true.  The most common use of this is to check whether a variable is in a certain range, with an expression like <c>0 < x < 10</c>, which is true only if <c>x</c> is between 0 and 10 (non-inclusive).
 +
 
 +
{| class="wikitable"
 +
|-
 +
! Code !! Given !! Result || Read As
 +
|-
 +
| <c>0 < x < 10</c> || x = 5 || true || x is between 0 and 10
 +
|-
 +
| <c>0 < x < 10</c> || x = 10 || false || x is between 0 and 10
 +
|-
 +
| <c>0 < x <= 10</c> || x = 10 || true || x is between 0 and 10 (including 10)
 +
|-
 +
| <c>0 < x < 10 < y</c> || x = 5; y = 15 || true || x is between 0 and 10, and 10 is less than y
 +
|}
  
Operators <c>></c>, <c><</c>, <c>>=</c> and <c><=</c> support chaining:
 
  
<ms>
 
x = 42
 
if 13 < x <= 100 then  // same as if (13 < x) and (x <= 100) ...
 
print "foo"
 
end if
 
</ms>
 
  
 
[[Category:Language]]
 
[[Category:Language]]

Revision as of 16:29, 5 October 2023

MiniScript defines the following operators (listed in order from lowest to highest precedence):

Operator Meaning
A = B assignment
A or B logical OR: true if either operand is true
A and B logical AND: true if both operands are true
not A logical negation: true if its operand is false, and vice versa
A isa B type checking
A == B equality comparison: true if operands are equal
A != B inequality comparison: true if operands are not equal
A > B greater-than comparison
A < B less-than comparison
A >= B greater-than or equal-to comparison
A <= B less-than or equal-to comparison
A + B addition or concatenation
A - B subtraction or string trimming
A * B multiplication or replication
A / B division or reduction
-A unary minus (numeric negation)
new A instantiation
@A address-of (reference function without invoking it)
A ^ B power: A raised to the power of B
A.B dot operator

Chaining Operators

Most operators in MiniScript can be combined into chains, for example 39 + 2 + 1. The details depend on the type of operator:

Arithmetic Operators

In an expression involving a chain of arithmetic operators, the highest-precedent operators are evaluated first, as in standard algebra. You can always use parentheses to break such a chain into smaller chunks, forcing a different evaluation order.

Code Result
2 + 5 * 8 42
(2 + 5) * 8 56
3 ^ 2 + 4 ^ 2 25

Comparison Operators

In the case of comparison operators (>, <, >= and <=), operands are compared in pairs from left to right. All comparisons must be true for the complete chained expression to be considered true. The most common use of this is to check whether a variable is in a certain range, with an expression like 0 < x < 10, which is true only if x is between 0 and 10 (non-inclusive).

Code Given Result Read As
0 < x < 10 x = 5 true x is between 0 and 10
0 < x < 10 x = 10 false x is between 0 and 10
0 < x <= 10 x = 10 true x is between 0 and 10 (including 10)
0 < x < 10 < y x = 5; y = 15 true x is between 0 and 10, and 10 is less than y