Operators And Tokens
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 |
Other Reference Material
Language Guides
Language Tour, which is an introduction into the Murex language.
Rosetta Stone, which is a reference table comparing Bash syntax to Murex's.
Builtins, for docs on the core builtins.
Murex's Source Code
The parser is located Murex's source under the lang/expressions/
path of the project files.
Pages
- ( expression ): Inline expressions
- C-style functions: Inlined commands for expressions and statements
- Filter By Range
[ ..Range ]
: Outputs a ranged subset of data from stdin - Get Item (
[ Index ]
): Outputs an element from an array, map or table - Get Nested Element (
[[ Element ]]
): Outputs an element from a nested structure - Read / Write To A Named Pipe (
<pipe>
): Reads from a Murex named pipe - Read From Stdin (
<stdin>
): Read the stdin belonging to the parent code block - Truncate File (
>
): Writes stdin to disk - overwriting contents if file already exists "Double Quote"
: Initiates or terminates a string (variables expanded)$Scalar
Sigil (eg variables): Expand values as a scalar%(Brace Quote)
: Initiates or terminates a string (variables expanded)%[]
Array Builder: Quickly generate arrays%{}
Object Builder: Quickly generate objects (dictionaries / maps)&&
And Logical Operator: Continues next operation if previous operation passes'Single Quote'
: Initiates or terminates a string (variables not expanded)(brace quote)
: Write a string to the stdout without new line (deprecated)*=
Multiply By Operator: Multiplies a variable by the right hand value (expression)*
Multiplication Operator: Multiplies one numeric value with another (expression)+=
Add With Operator: Adds the right hand value to a variable (expression)+
Addition Operator: Adds two numeric values together (expression)-=
Subtract By Operator: Subtracts a variable by the right hand value (expression)->
Arrow Pipe: Pipes stdout from the left hand command to stdin of the right hand command-
Subtraction Operator: Subtracts one numeric value from another (expression)/=
Divide By Operator: Divides a variable by the right hand value (expression)/
Division Operator: Divides one numeric value from another (expression)<~
Assign Or Merge: Merges the right hand value to a variable on the left hand side (expression)=>
Generic Pipe: Pipes a reformatted stdout stream from the left hand command to stdin of the right hand command=
(arithmetic evaluation): Evaluate a mathematical function (deprecated)>>
Append File: Writes stdin to disk - appending contents if file already exists?:
Elvis Operator: Returns the right operand if the left operand is falsy (expression)??
Null Coalescing Operator: Returns the right operand if the left operand is empty / undefined (expression)?
stderr Pipe: Pipes stderr from the left hand command to stdin of the right hand command (DEPRECATED)@Array
Sigil: Expand values as an array[{ Lambda }]
: Iterate through structured data{ Curly Brace }
: Initiates or terminates a code block|
POSIX Pipe: Pipes stdout from the left hand command to stdin of the right hand command||
Or Logical Operator: Continues next operation only if previous operation fails~
Home Sigil: Home directory path variable