Expressions¶
StxScript supports familiar infix operators that compile to Clarity's prefix S-expressions.
Arithmetic Operators¶
| Operator | Description | Clarity |
|---|---|---|
+ |
Addition | (+ a b) |
- |
Subtraction | (- a b) |
* |
Multiplication | (* a b) |
/ |
Integer division | (/ a b) |
% |
Modulo | (mod a b) |
let sum = a + b;
let diff = a - b;
let product = a * b;
let quotient = a / b;
let remainder = a % b;
Generated Clarity:
Note: Division is integer division (truncates toward zero). There is no floating-point arithmetic in Clarity.
Type Requirements¶
Arithmetic operators require operands of the same numeric type. You cannot mix int and uint:
Comparison Operators¶
| Operator | Description | Clarity |
|---|---|---|
== |
Equal | (is-eq a b) |
!= |
Not equal | (not (is-eq a b)) |
< |
Less than | (< a b) |
<= |
Less or equal | (<= a b) |
> |
Greater than | (> a b) |
>= |
Greater or equal | (>= a b) |
let isEqual = a == b;
let isNotEqual = a != b;
let isLess = a < b;
let isLessOrEqual = a <= b;
let isGreater = a > b;
let isGreaterOrEqual = a >= b;
Generated Clarity:
Comparisons return bool and work with numeric types and principals.
Logical Operators¶
| Operator | Description | Clarity |
|---|---|---|
&& |
Logical AND | (and a b) |
\|\| |
Logical OR | (or a b) |
! |
Logical NOT | (not a) |
let both = condition1 && condition2;
let either = condition1 || condition2;
let opposite = !condition;
Generated Clarity:
Logical operators work on bool values only. They short-circuit: && stops on the first false, || stops on the first true.
Bitwise Operators¶
| Operator | Description | Clarity |
|---|---|---|
& |
Bitwise AND | (bit-and a b) |
\| |
Bitwise OR | (bit-or a b) |
^ |
Bitwise XOR | (bit-xor a b) |
~ |
Bitwise NOT | (bit-not a) |
<< |
Left shift | (bit-shift-left a n) |
>> |
Right shift | (bit-shift-right a n) |
let andResult = a & b;
let orResult = a | b;
let xorResult = a ^ b;
let notResult = ~a;
let leftShift = a << 2;
let rightShift = a >> 2;
Generated Clarity:
Bitwise operators work on integer types (int and uint).
Operator Precedence¶
From highest to lowest:
!,~(unary)*,/,%+,-<<,>><,<=,>,>===,!=&^|&&||
Use parentheses to override precedence:
Function Calls¶
Call functions with parentheses:
Generated Clarity:
Next Steps¶
- Control Flow - Using expressions in conditions
- Functions - Expressions in function bodies
- Data Structures - Expressions with collections