Skip to content

Expressions ​

Implementation status: Implemented (Expr enum, complete precedence table); custom operators via operator func GA (three-back-end parity); optional chaining / nil coalescing / force unwrap Implemented

Expressions produce values. The table below is ordered from highest to lowest precedence (matching how the parser climbs).


Precedence (Complete Table) ​

LevelPrecedenceFormOperator / description
1 (highest)—Postfix. [] () ?. ! (postfix) await (postfix)
2—Unary prefix- ! ~ try & (borrow) await (prefix)
3100Multiplicative* / % << >> (shifts mix into this level)
490Additive+ -
580Bit shift<< >> (Partially implemented)
670Range..< ...
770Ordered comparison< <= > >=
860Equality== != === !==
950Bitwise AND&
1040Bitwise XOR^
1130Bitwise OR|
1220Logical AND&&
1310Logical OR||
145Nil coalescing??
1510Compound assignment+= -= *= /= %=
16 (lowest)5Assignment=

Parentheses win: () may be added anywhere to force precedence, with no side effects. Assignment expressions have Void semantics (do not depend on chaining).

Literal Expressions ​

literal ::= int-literal | float-literal | string-literal | bool-literal | nil-literal
          | array-literal | dict-literal | object-literal | regex-literal
swift
42
3.14f32
"text"
true
nil
[1, 2, 3]
["k": "v"]
{ "name": "SYX" }
/\d+/

Identifiers ​

identifier-expr ::= identifier
swift
count
MyType
self                                        // Current instance
super                                       // Superclass

Tuple Expressions ​

tuple-expr ::= '(' expr (',' expr)+ ')'
            |  '(' expr ',' ')'              // One-element tuple
swift
let t = (1, "a", true)                      // (Int, String, Bool)
let (x, y) = (1, 2)                         // Destructuring

Unary and Binary ​

swift
-a                                          // Unary minus
!flag                                       // Logical NOT
~bits                                       // Bitwise NOT
a + b
a * b
a % b
a == b
a < b
a && b
a || b
expr?                                       // try chain: convert error to nil / propagate

Calls ​

call-expr ::= expr '(' arg-list? ')'
arg-list  ::= (label ':')? expr (',' (label ':')? expr)*
swift
foo()
foo(x: 1, y: 2)                             // Named arguments
obj.method(arg)
arr.map { x in x * 2 }                      // Trailing closure

Named arguments: the caller must write label: value (there is no _ elision syntax).

Member Access ​

member-expr ::= expr '.' identifier
swift
obj.field
obj.length
arr.map
self.name

Subscripts ​

index-expr ::= expr '[' expr ']'
            |  expr '[' expr (',' expr)* ']'
swift
arr[i]
dict["key"]                                 // Dictionary
s[0]                                        // String (single scalar)
m[0, 1]                                     // Matrix multi-parameter
obj["field"]                                // Dynamic Object

Implementation status:

  • arr[i] / dict["key"] / s[i] Implemented
  • s[0..<3] slicing Not implemented (use substring)
  • Custom subscript Partially implemented (Phase 40/41)

Optional Chaining and Force Unwrap ​

optional-chain ::= expr '?' '.' identifier
                |  expr '?' '[' expr ']'
                |  expr '?' '(' arg-list? ')'
force-unwrap   ::= expr '!'
swift
maybe?.length                               // Optional chain
maybe?.foo(1, 2)
maybe!                                       // Force unwrap (nil → R007)

Diagnostic: R007 (runtime nil force unwrap)

await ​

await-expr ::= 'await' expr
swift
await asyncFunc()
await ch.receive()                          // Asynchronous receive

Legal only inside an async function body; await outside async → S020.

Closure Expressions ​

closure-expr ::= '{' closure-params? closure-body? '}'
closure-params ::= ('|' (identifier (',' identifier)* '|') | (identifier (',' identifier)*))
                |  '(' (identifier ':' type (',' identifier ':' type)*)? ')'
closure-body ::= 'in' expr | 'in' block
swift
{ x in x + 1 }                              // Single expression
{ (a, b) in a + b }                         // Multiple parameters
{ print("hi") }                             // No parameters
{ (x: Int) -> Int in x * 2 }               // Explicit types
{ |x| x + x }                               // Pipe parameters (**partially supported**)

Array Literals ​

array-literal ::= '[' (expr (',' expr)*)? ']'
swift
[10, 20, 30]
[]                                          // Empty array (needs a type annotation)
[1, 2, 3].map { $0 * 2 }                    // Immediate call

Dictionary Literals ​

dict-literal ::= '[' dict-entry (',' dict-entry)* ']'
dict-entry   ::= expr ':' expr
object-literal ::= '{' object-entry (',' object-entry)* '}'
object-entry  ::= string-literal ':' expr
swift
["a": 1, "b": 2]                            // Dictionary
{ "name": "SYX", "version": 110 }           // Object

Task Blocks ​

task-expr ::= 'Task' ('.' 'detached')? closure-expr
swift
Task { … }
Task.detached { … }

Borrow Expressions ​

borrow-expr ::= '&' 'mut'? expr
swift
&value
&mut counter

Ranges (Half-Open / Closed) ​

range-expr ::= expr '..<' expr
            |  expr '...' expr
            |  '..<' expr
            |  expr '..<'
swift
0..<10                                      // Half-open
1...5                                       // Closed range (**Partially implemented**)

Used in for-in and Range type contexts.

Pattern Expressions (switch / if case) ​

pattern-expr ::= pattern '=' expr
             |  'if' 'case' pattern '=' expr block
             |  'switch' '(' expr ')' '{' case-clause* '}'
swift
if case .ok(let x) = result { … }
switch (result) {
case .ok(let v) { … }
default { … }
}

See Patterns.

Assignment Expressions ​

assign-expr ::= lvalue '=' expr
            |  lvalue ('+=' | '-=' | '*=' | '/=' | '%=') expr
swift
x = 1
x += 2
obj.field = v
arr[i] = v

Assignment expressions have Void semantics (do not depend on chaining). a = b = c is Not implemented.

Custom Operators ​

operator-decl ::= 'prefix'|'infix'|'postfix' 'operator' identifier
                  '{' 'precedence' ':' int ',' 'associativity' ':' ('left'|'right'|'none') '}'
swift
infix operator |> { precedence: 50, associativity: left }
func |> <T>(value: T, f: (T) -> T) -> T { return f(value) }

The complete grammar is in Chapter 30, Advanced Operators.

Expression Classification ​

CategoryExample
Literal42, "x", [1,2,3]
Identifiercount, self, super
Pathobj.field, arr[i]
Callf(), obj.method(x)
Unary-x, !flag, &mut x
Binarya + b, a < b
Range0..<n, 1...5
Optionalopt?.x, opt!
Closure{ x in x + 1 }
Asyncawait future
trytry? risky()
Control-flow expressionif (x) { a } else { b }
Keywordnil, true, false
String interpolation"\(name)"

Expression Type Checking ​

RuleDescription
Both sides same typeRequired for arithmetic and comparison (S004 if incompatible)
Integer + floatwidening (Int → Double)
Signed + unsignedrejected with S014
Optional + TT? offers nil coalescing / chaining / force unwrap
Function + functionCalls / HOF
Tuple + tupleElement types matched individually
nil literalInferred as T?

Diagnostics: S004 / S008 / S010 / S014 / S020 / R007

Usage examples → Language Guide: Operators · Advanced Operators

See also → Statements · Types · Patterns · Grammar Summary

SYX · Apache-2.0