Operators And Tokens
Operators And Tokens
All supported operators and tokens
Syntax
Murex supports both expressions and statements. You can use the interchangeably with your code and the Murex parser will decide whether to run that code as an expression or statement.
Expressions
Expressions are patterns formed like equations (eg $val = 1 + 2
).
Strings must be quoted in expressions.
Statements
Statements are traditional shell command calls (eg command parameters...
).
Quoting strings is optional in statements.
Not all operators are supported in statements.
Order Of Operations
Expressions and statements are split by pipes and terminators. Each statement and expression is executed from left to right, with the statement or expression parsed by the following rules of operation
Order of operations:
- expression or statement discovery
- sub-shells / sub-expressions
- multiplication / division (expressions only)
- addition / subtraction (expressions only)
- immutable merge
- comparisons, eg greater than (expressions only)
- logical and (sub-expressions only)
- logical or (sub-expressions only)
- elvis (expressions only)
- assign (expressions only)
- left to right
Expression Or Statement Discovery
First a command is read as an expression. Because the rules of parsing expressions are stricter than statements, everything is assumed to be an expression unless the expression parser fails, which then it is assumed to be a statement.
Operators And Tokens
Terminology
- left: this is the value to the left hand side of the operator
- right: this is the value to the right hand side of the operator
Example: left operator
right
Modifiers
All modifiers replace the left, operator and right with the returned value of the modifier.
All returns will be num
data type (or their original type if strict types is enabled).
Modifiers are only supported in expressions.
Operator | Name | Operation |
---|---|---|
* | Multiplication | Multiply left by right |
/ | Divide | Divide left by right |
+ | Addition | Add left with right |
- | Subtraction | Subtract left by right |
Read more:
Immutable Merge
Returns the result of merging right into left.
immutable merge does not modify the contents of either left nor right.
The direction of the arrow indicates that the result returned is a new value rather than an updated assignment.
Left can be a statement or expression, whereas right can only be an expression. However you can still use a sub-shell as part of, or the entirety, of, that expression.
Operator | Name | Operation |
---|---|---|
~> | Immutable Merge | Returns merged value of right into left |
Comparators
All comparators replace the left, operator and right with the returned value of the comparator.
All returns will be bool
data type, either true
or false
.
Comparators are only supported in expressions.
Operator | Name | Operation |
---|---|---|
> | Greater Than | true if left is greater than right |
>= | Greater Or Equal To | true if left is greater or equal to right |
< | Less Than | true if left is less than right |
<= | Less Or Equal To | true if left is less or equal to right |
== | Equal To | true if left is equal to right |
!= | Not Equal To | false if left is equal to right |
~~ | Like | true if left string is like right string |
!! | Not Like | false if left string is like right string |
=~ | Matches Regexp | true if left matches regexp pattern on right |
!~ | Does Not Match Regexp | false if left matches regexp pattern on right |
Read more:
- Data types: bool
Assignment
Assignment returns null
if successful.
Assignment is only supported in expressions.
Operator | Name | Operation |
---|---|---|
= | Assign (overwrite) | Assign right to left |
:= | Assign (retain) | EXPERIMENTAL |
<~ | Assign Or Merge | Merge right (array / object) into left |
+= | Assign And Add | Add right to left and assign to left |
-= | Assign And Subtract | Subtract right from left and assign to left |
*= | Assign And Multiply | Multiply right with left and assign to left |
/= | Assign And Divide | Divide right with left and assign to left |
++ | Add one to variable | Adds one to right and reassigns |
-- | Subtract one from var | Subtracts one from right and reassigns |
Read more:
Conditionals
Conditionals replace left, operator and right with the value defined in operation.
These conditionals are only supported in expressions.
Operator | Name | Operation |
---|---|---|
?? | Null Coalescence | Returns left if not null , otherwise right |
?: | Elvis | Returns left if truthy, otherwise right |
Read more:
Sigils
Sigils are special prefixes that provide hints to the parser.
Sigils are supported in both expressions and statements.
Token | Name | Operation |
---|---|---|
$ | Scalar | Expand value as a string |
@ | Array | Expand value as an array |
~ | Home | Expand value as the persons home directory |
% | Builder | Create an array, map or nestable string |
Constants
Constants are supported in both expressions and statements. However null
, true
, false
and number will all be interpreted as strings in statements.
Token | Name | Operation |
---|---|---|
null | Null | null (null / nil / void) type |
true | True | bool (boolean) true |
false | False | bool (boolean) false |
number | Number | num (numeric) value |
' string' | String Literal | str (string) literal value |
" string" | Infix String | str (string) value, supports escaping & infixing |
{ code } | Code Block | str (string) value, surrounded by curly braces |
%( string ) | String Builder | Creates a nestable str (string) |
%[ array ] | Array Builder | Creates a json (JSON) array (list) |
%{ map } | Object Builder | Creates a json (JSON) object (map / dictionary) |
Read more:
Sub-shells
Sub-shells are a way of inlining expressions or statements into an existing expression or statement. Because of this they are supported in both.
Syntax | Name | Operation |
---|---|---|
command( parameters... ) | C-Style Functions | Inline a command as a function |
${ command parameters...} | Sub-shell (scalar) | Inline a command line as a string |
@{ command parameters...} | Sub-shell (array) | expand a command line as an array |
( expression) | Sub-expression | Inline an expression (statement) |
( expression) | Sub-expression | Order of evaluation (expression) |
[{ code block }] | Lambda | Run operations across lists / maps |
Read more:
Boolean Operations
Boolean operators behave like pipes.
They are supported in both expressions and statements.
Operator | Name | Operation |
---|---|---|
&& | And | Evaluates right if left is truthy |
|| | Or | Evaluates right if left is falsy |
Pipes
Pipes always flow from left to right.
They are supported in both expressions and statements.
Operator | Name | Operation |
---|---|---|
| | POSIX Pipe | POSIX compatibility |
-> | Arrow Pipe | Context aware pipe |
=> | Generic Pipe | Convert stdout to * (generic) then pipe |
|> | Truncate File | Write stdout to file, overwriting contents |
>> | Append File | Write stdout to file, appending contents |
Terminators
"LF" refers to the life feed character, which is a new line.
Token | Name | Operation |
---|---|---|
; | Semi-Colon | End of statement or expression (optional) |
LF | Line Feed | End of statement or expression (new line) |
Escape Codes
Any character can be escaped via \
to signal it isn't a token. However some characters have special meanings when escaped.
"LF" refers to the life feed character, which is a new line.
Token | Name | Operation |
---|---|---|
\s | Space | Same as a space character |
\t | Tab | Same as a tab character |
\r | Carriage Return | Carriage Return (CR) sometimes precedes LF |
\n | Line Feed | Line Feed (LF), typically a new line |
\ LF | Escaped Line Feed | Statement continues on next line |
See Also
- Expressions (
expr
): Expressions: mathematical, string comparisons, logical operators - Language Tour: Getting started with Murex: a quick tour of the next generation of shell scripting
This document was generated from gen/user-guide/operators-tokens_doc.yaml.