Appendix A. Operators and precedence
This appendix is a reference sheet: every Sprig operator in one place, with its precedence, its associativity and its real behavior. Every program comes from a snippet in the repository, and the compiler produced the outputs.
A.1 The operators at a glance
Assignment (only at the start of a statement, with an expression on the right):
| Operator | Example | Meaning |
|---|---|---|
= | total = 0 | assignment; the target must be a var variable, a var field or an element of a mutable collection |
+= | total += 1 | add, then assign |
-= | total -= 1 | subtract, then assign |
*= | total *= 2 | multiply, then assign |
/= | ratio /= 2.0 | divide, then assign; integer / is still not allowed |
Logic: and, or, not; the operands must be Bool.
Comparison: ==, !=, <, <=, >, >=, in.
Arithmetic: binary +, -, *, /, %, and prefix +, -.
Postfix: .member, (arguments), [index], and [TypeArguments] for generics.
Other symbols: : (blocks, type annotations, map literals), ,, -> (function result), => (lambda body), ? (nullable type suffix), # (comment).
A.2 Precedence and associativity
From loosest (evaluated last) to tightest:
| Level | Operators | Associativity | Example and result |
|---|---|---|---|
| 1 | or | left | true or false and false → true |
| 2 | and | left | |
| 3 | not (prefix) | right | not 1 == 2 → not (1 == 2) → true |
| 4 | == != < <= > >= in | no chaining | 1 + 1 in [2] → (1 + 1) in [2] → true |
| 5 | + - (binary) | left | 10 - 2 - 3 → 5 |
| 6 | * / % | left | 1 + 2 * 3 → 7 |
| 7 | + - (unary) | prefix | -2 * 3 → -6 |
| 8 | .member (arguments) [index] | left | items.size(), text[0] |
| 9 | literals, (...), [...], {...} | — | (true or false) and false → false |
Assignment is not an expression but a whole statement, so it does not chain:
# Assignment is a statement; a = b = 3 does not chain.
var a = 1
var b = 2
a = b = 3SPR-SYNTAX-ERROR [SYNTAX] main.spr:4:7: Expected the end of the line, found '='
1 error(s); run 'sprig explain <code>' for details on a diagnostic code.Comparisons cannot chain either (the full error is in the "Deliberate mistakes" section below).
A.3 Precedence in action
# Precedence demo: each line's result comes from a rule other than parentheses.
# Multiplication and division bind before addition and subtraction.
print(1 + 2 * 3)
print(2 * 3 + 1)
# Same level evaluates left to right.
print(10 - 2 - 3)
print(2 * 3 % 4)
# The minus sign is a prefix and applies to the value right after it.
print(-2 * 3)
print(- -3)
# not applies to the comparison result; and binds tighter than or.
print(not 1 == 2)
print(true or false and false)
print((true or false) and false)
# in has the comparison level; with + it evaluates the addition first.
print(1 + 1 in [2])
print("a" in "abc")
print("a" in {"a": 1})7
7
5
2
-6
3
true
true
false
true
true
trueLine by line:
1 + 2 * 3is1 + (2 * 3);2 * 3 + 1shows that multiplication and division bind tighter than addition and subtraction.10 - 2 - 3is(10 - 2) - 3: one level evaluates left to right.-2 * 3negates first, then multiplies;- -3is 3.- In
not 1 == 2,notbinds looser than==, so the comparison happens first and its result is negated. - In
true or false and false,andbinds tighter thanor; with parentheses,(true or false) and falseisfalse. - In
1 + 1 in [2],+binds tighter thanin;"a" in "abc"finds a substring in a string, and"a" in {"a": 1}finds a key in a map.
A.4 How the operators behave
# How the operators behave.
# and/or short-circuit: the right-hand function is called only when needed.
func loud_true() -> Bool:
print("called true")
return true
func loud_false() -> Bool:
print("called false")
return false
print(false and loud_true())
print(true or loud_false())
print(true and loud_true())
# The sign of % follows the dividend, as in Java and unlike Python.
print(-7 % 3)
print(7 % -3)
# + chains left to right: the first two Ints add before the text joins.
print(1 + 2 + " then " + 1 + 2)
# Compound assignment works on var and mutable collection elements, like =.
var total = 7
total += 3
total -= 1
total *= 2
print(total)
var ratio = 7.0
ratio /= 2.0
print(ratio)false
true
called true
true
-1
1
3 then 12
18
3.5Line by line:
false and loud_true()andtrue or loud_false()print nocalled ..., soand/orshort-circuit: the right side is not called when it is not needed.true and loud_true()does call the right side.-7 % 3is-1and7 % -3is1: the sign of the result of%follows the dividend, as in Java and unlike Python.1 + 2 + " then " + 1 + 2prints3 then 12: the first twoInts add, and joining with text begins at the first string.total += 3,-=and*=lead to 18;ratio /= 2.0gives3.5(/=works for floating-point values).- Only
varcan be assigned;letbinds once (Chapter 3).
Details by type:
| Operator | Operands | Behavior |
|---|---|---|
+ | either side is String | turns the other side into its text and joins, with no need to call toString() first |
+ - * | two numbers of the same family | Int, Int32, Float, Float32, Decimal, BigInt all work; the result stays in the same family |
% | two numbers of the same family | Int, Int32, Float, Float32, BigInt; Decimal is not allowed |
/ | two Float or two Float32 | fractional division |
/ | two Int/Int32/BigInt | not allowed, see "Deliberate mistakes" |
/ | two Decimal | not allowed; use a.divide(b, scale, roundingMode) |
== != | numbers, Bool, String, enum, variant, lists, maps | compare by value |
== != | class objects | compare by identity: only the same object is equal, even when fields match (Chapter 11) |
< <= > >= | numbers, String | String compares by UTF-16 code units; numbers never mix Int and Float implicitly |
in | List, Map, String | finds an element in a list, a key in a map, or a substring in a string; uses == |
and or not | Bool | short-circuit; there is no "nonzero is true" |
+ - | one number | positive or negative sign |
Two nullable scalars can be compared directly with ==, and two nulls are equal; but + cannot join a value that may be null, so check it first (Chapter 13).
A.5 Operators that do not exist
| What you might write | What Sprig actually does | What to write instead |
|---|---|---|
x++, x-- | syntax error | x += 1, x -= 1 |
a ** b | no power operator | Float.sqrt (or Java's Math.pow, Chapter 21) |
<< >> & | ^ ~ | no bitwise operators | floor_div and friends from @std/math; bitwise work goes through Java (Chapter 21) |
&& || ! | syntax errors | and, or, not |
a ? b : c, a if c else b | no ternary expression | an if expression (Chapter 5) |
x ?? y, x?.y | syntax errors | if x != null:, or or_else/require from @std/nulls (Chapter 13) |
1 < x < 3 | comparisons do not chain | 1 < x and x < 3 |
===, !== | syntax errors | ==, != (they are already strict: there are no implicit conversions) |
A.6 Deliberate mistakes
Chain two comparisons and the compiler stops at the second:
# Comparisons do not chain: there is no 1 < x < 3.
print(1 < 2 < 3)SPR-SYNTAX-ERROR [SYNTAX] main.spr:2:13: missing ')' (unexpected '<')
1 error(s); run 'sprig explain <code>' for details on a diagnostic code.Write ++ and the error hands you the replacement:
# Sprig has no ++ or --.
var count = 1
count++
print(count)SPR-SYNTAX-ERROR [SYNTAX] main.spr:3:8: Sprig has no ++ or -- operator
hint: Write 'count += 1' or 'count -= 1'.
1 error(s); run 'sprig explain <code>' for details on a diagnostic code.Integer division is rejected even when the quotient is a whole number, because / must mean "real division":
# Integer division must state its intent: / is only for Float and Float32.
print(7 / 2)SPR-NUM-DIVISION [TYPE] main.spr:2:7: Integer / would truncate; use a.divTrunc(b), or convert both operands explicitly (expected explicit division, actual Int / Int)
hint: Write 7.divTrunc(2) to drop the remainder on purpose, or 7.toFloatExact() / 2.toFloatExact() for a Float result. text.fixed(value, decimals) from @std/text.spr prints a Float with that many decimals.
1 error(s); run 'sprig explain <code>' for details on a diagnostic code.Related chapters
- Chapter 3: Values, variables and arithmetic:
let/var, the number families,Bool. - Chapter 4: Text:
+joining and string methods. - Chapter 5: Making decisions: if: conditions and if expressions.
- Chapter 13: Values that may be missing:
== nulland narrowing. - Chapter 15: Functions as values:
=>and function types. - Appendix B: Error codes and where to read about them: the error codes on this page.
