Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operators & Expressions

CXL provides arithmetic, comparison, boolean, null coalescing, and string operators. Boolean logic uses keywords (and, or, not), not symbols.

Arithmetic operators

OperatorDescriptionExample
+Addition (or string concatenation)2 + 3
-Subtraction10 - 4
*Multiplication3 * 5
/Division10 / 3
%Modulo (remainder)10 % 3
$ cxl eval -e 'emit result = 2 + 3 * 4'
{
  "result": 14
}

Multiplication binds tighter than addition, so 2 + 3 * 4 is 2 + (3 * 4) = 14, not (2 + 3) * 4 = 20.

$ cxl eval -e 'emit result = 10 % 3'
{
  "result": 1
}

Comparison operators

OperatorDescriptionExample
==Equalx == 0
!=Not equalx != 0
>Greater thanx > 10
<Less thanx < 10
>=Greater than or equalx >= 10
<=Less than or equalx <= 10
$ cxl eval -e 'emit result = 5 > 3' --field dummy=1
{
  "result": true
}

Boolean operators

CXL uses keywords for boolean logic. The symbols &&, ||, and ! are not valid CXL syntax.

OperatorDescriptionExample
andLogical ANDa and b
orLogical ORa or b
notLogical NOT (unary)not a
$ cxl eval -e 'emit result = true and not false'
{
  "result": true
}
$ cxl eval -e 'emit result = 5 > 3 or 10 < 2'
{
  "result": true
}

Null coalesce operator

The ?? operator returns its left operand if non-null, otherwise its right operand.

$ cxl eval -e 'emit result = null ?? "default"'
{
  "result": "default"
}
$ cxl eval -e 'emit result = "present" ?? "default"'
{
  "result": "present"
}

String concatenation

The + operator concatenates strings when both operands are strings.

$ cxl eval -e 'emit result = "hello" + " " + "world"'
{
  "result": "hello world"
}

Unary operators

OperatorDescriptionExample
-Numeric negation-x
notBoolean negationnot done
$ cxl eval -e 'emit result = -42'
{
  "result": -42
}

Method calls

Methods are called on a receiver using dot notation:

$ cxl eval -e 'emit result = "hello".upper()'
{
  "result": "HELLO"
}

Methods can be chained:

$ cxl eval -e 'emit result = "  hello  ".trim().upper()'
{
  "result": "HELLO"
}

Field references

Bare identifiers reference fields from the input record:

$ cxl eval -e 'emit result = price * qty' \
    --field price=10 \
    --field qty=3
{
  "result": 30
}

Qualified field references use dot notation for multi-source pipelines: source.field.

Operator precedence

From highest (binds tightest) to lowest:

PrecedenceOperatorsAssociativity
1 (highest). (method calls, field access)Left
2- (unary), notPrefix
3* / %Left
4+ -Left
5== != > < >= <=Left
6andLeft
7orLeft
8 (lowest)??Right

Use parentheses to override precedence:

$ cxl eval -e 'emit result = (2 + 3) * 4'
{
  "result": 20
}

Comments

Line comments start with # (when not followed by a digit – digit-prefixed # starts a date literal):

# This is a comment
emit total = price * qty  # inline comment
emit deadline = #2024-12-31#  # this is a date literal, not a comment