Skip to content

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):

OperatorExampleMeaning
=total = 0assignment; the target must be a var variable, a var field or an element of a mutable collection
+=total += 1add, then assign
-=total -= 1subtract, then assign
*=total *= 2multiply, then assign
/=ratio /= 2.0divide, 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:

LevelOperatorsAssociativityExample and result
1orlefttrue or false and false → true
2andleft
3not (prefix)rightnot 1 == 2 → not (1 == 2) → true
4== != < <= > >= inno chaining1 + 1 in [2] → (1 + 1) in [2] → true
5+ - (binary)left10 - 2 - 3 → 5
6* / %left1 + 2 * 3 → 7
7+ - (unary)prefix-2 * 3 → -6
8.member (arguments) [index]leftitems.size(), text[0]
9literals, (...), [...], {...}—(true or false) and false → false

Assignment is not an expression but a whole statement, so it does not chain:

spr
# Assignment is a statement; a = b = 3 does not chain.
var a = 1
var b = 2
a = b = 3
text
SPR-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 ​

spr
# 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})
text
7
7
5
2
-6
3
true
true
false
true
true
true

Line by line:

  • 1 + 2 * 3 is 1 + (2 * 3); 2 * 3 + 1 shows that multiplication and division bind tighter than addition and subtraction.
  • 10 - 2 - 3 is (10 - 2) - 3: one level evaluates left to right.
  • -2 * 3 negates first, then multiplies; - -3 is 3.
  • In not 1 == 2, not binds looser than ==, so the comparison happens first and its result is negated.
  • In true or false and false, and binds tighter than or; with parentheses, (true or false) and false is false.
  • In 1 + 1 in [2], + binds tighter than in; "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 ​

spr
# 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)
text
false
true
called true
true
-1
1
3 then 12
18
3.5

Line by line:

  • false and loud_true() and true or loud_false() print no called ..., so and/or short-circuit: the right side is not called when it is not needed.
  • true and loud_true() does call the right side.
  • -7 % 3 is -1 and 7 % -3 is 1: the sign of the result of % follows the dividend, as in Java and unlike Python.
  • 1 + 2 + " then " + 1 + 2 prints 3 then 12: the first two Ints add, and joining with text begins at the first string.
  • total += 3, -= and *= lead to 18; ratio /= 2.0 gives 3.5 (/= works for floating-point values).
  • Only var can be assigned; let binds once (Chapter 3).

Details by type:

OperatorOperandsBehavior
+either side is Stringturns the other side into its text and joins, with no need to call toString() first
+ - *two numbers of the same familyInt, Int32, Float, Float32, Decimal, BigInt all work; the result stays in the same family
%two numbers of the same familyInt, Int32, Float, Float32, BigInt; Decimal is not allowed
/two Float or two Float32fractional division
/two Int/Int32/BigIntnot allowed, see "Deliberate mistakes"
/two Decimalnot allowed; use a.divide(b, scale, roundingMode)
== !=numbers, Bool, String, enum, variant, lists, mapscompare by value
== !=class objectscompare by identity: only the same object is equal, even when fields match (Chapter 11)
< <= > >=numbers, StringString compares by UTF-16 code units; numbers never mix Int and Float implicitly
inList, Map, Stringfinds an element in a list, a key in a map, or a substring in a string; uses ==
and or notBoolshort-circuit; there is no "nonzero is true"
+ -one numberpositive 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 writeWhat Sprig actually doesWhat to write instead
x++, x--syntax errorx += 1, x -= 1
a ** bno power operatorFloat.sqrt (or Java's Math.pow, Chapter 21)
<< >> & | ^ ~no bitwise operatorsfloor_div and friends from @std/math; bitwise work goes through Java (Chapter 21)
&& || !syntax errorsand, or, not
a ? b : c, a if c else bno ternary expressionan if expression (Chapter 5)
x ?? y, x?.ysyntax errorsif x != null:, or or_else/require from @std/nulls (Chapter 13)
1 < x < 3comparisons do not chain1 < 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:

spr
# Comparisons do not chain: there is no 1 < x < 3.
print(1 < 2 < 3)
text
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:

spr
# Sprig has no ++ or --.
var count = 1
count++
print(count)
text
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":

spr
# Integer division must state its intent: / is only for Float and Float32.
print(7 / 2)
text
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.

Sprig is licensed under Apache-2.0 · v0.7.1-beta.1 published as a prerelease