Operators - Manhunter07/MFL GitHub Wiki
Notice: Although absolute bars work similar to an operator, they are no subject of this article
There are a bunch of operators available which you might know from the C, Java or many other languages. While most of them work on numbers, booleans, strings and other types might only support a subset. There are unary (one operand) and binary (two operands) operators available. Unary operators are put in front of the value, while binary operators are put between values. The meaning of an operator might vary dependent on their operand count, and while some operators support both unary and binary use, some will only allow either.
Below, you find a table of all operators that are counted as such (thus, are not causing any special behaviour by the compiler and would therefore fall under some kind of language constructs). Please note, that parentheses, commas and dots, absolute bars and thinks like @
and #
are not counted as operators and therefore are not listed here.
References
See also: References#operators
Operator | Unary operation | Binary operation | Result type |
---|---|---|---|
? |
Comparison | Number | |
?= |
Equality | Boolean | |
?: |
Type compatibility | Boolean |
Booleans
See also: Booleans#operators
Operator | Unary operation | Binary operation | Result type |
---|---|---|---|
+ |
Value | AND concatenation | Boolean |
- |
Logical inversion | AND-NOT concatenation | Boolean |
* |
OR concatenation | Boolean | |
? |
Signus | Logical comparison | Number |
?= |
Logical equality | Boolean | |
?: |
Type compatibility | Boolean |
Numbers
Operator | Unary operation | Binary operation | Result type |
---|---|---|---|
+ |
Value | Addition | Number |
- |
Negation | Subtraction | Number |
* |
Multiplication | Number | |
/ |
Division | Number | |
% |
Modulo | Number | |
^ |
Exponentiation | Number | |
~ |
Rounding | Number | |
? |
Signus | Comparison | Number |
~= |
Rounded equality | Boolean | |
?= |
Arithmetic equality | Boolean | |
?: |
Type compatibility | Boolean |
Strings
Operator | Unary operation | Binary operation | Result type |
---|---|---|---|
+ |
Value | Concatenation | String |
- |
Case inversion | String | |
* |
Duplication | String | |
/ |
Substring count | Number | |
% |
Format | String | |
? |
Comparison | Number | |
~= |
Text case-insensitive equality | Boolean | |
?= |
Text equality | Boolean | |
?: |
Type compatibility | Boolean |
Arrays and records
Operator | Unary operation | Binary operation | Result type |
---|---|---|---|
+ |
Value | Concatenation / unification / member addition | Array or record |
- |
Member negation | Member subtraction | Array or record |
* |
Member multiplication | Array or record | |
/ |
Member division | Array or record | |
% |
Member modulo | Array or record | |
^ |
Member exponentiation | Array or record | |
~ |
Member rounding | Array or record | |
? |
Member signus | Member comparison | Number (unary), array or record (binary) |
~= |
Member rounded/case-insensitive equality | Boolean | |
?= |
Member equality | Boolean | |
?: |
Type compatibility | Boolean |
Member operations are operations which are invoked on each member. In binary operations, they are invoked with the corresponding counterpart of the other operand. This is either the element with the same index for arrays or the field with the name of same for records.
If a member is only found in the first operand, no operation is performed and the result will simply contain that value. If a member is only found in the second operand, the operation will be invoked unarily, and will fail if unsupported.
Each value type supports a type compatibility operator. It checks if a type passed by reference as second operand supports the value passed as first operand. It returns a boolean value and requires two operands: a value of any type and a reference to a declared or an anonymous type.
Precedences
Different operators may vary in their precedence, too. This is the kind of importance an operator has for the evaluation of an expression. The higher its precedence, the earlier it is invoked. If a chain of operands all shares the same operator or different operators with the same precedence, the expression is evaluated from left to right meaning that it starts with the first operand and uses that result as first operand for the next operation, and so on.
Precedences do not change dependent on the amount of operators. An operator will therefore always have the same precedence, independently from whether you use it unarily or binarily.
Below, you find a list of all precedences and the operators that have them. Ultra means that it is the first to be evaluated inside an expression, low means it is the last.
Precedence | Operators |
---|---|
Top | ~ ? |
High | ^ |
Medium | * / % |
Low | + - |
Bottom | ~= ?= ?: |
Null values
See also: System.Null
The Null
constant (and all objects that have Null
as value) are not compatible with any operators (unary or binary) except for the type check operator (ˋ?:ˋ). Using a different operator uppon them will cause an exception, since they do not represent any data type from MFL, but are placeholders for empty values. It is therefore important that functions should not return Null
unless they are intended to stand alone.
Considerations
These operators are not implemented, yet, but are considered for future implementation.
Operator | Description |
---|---|
?< |
Less than |
?> |
Greater than |
/= |
Not equal |