Statements
Implementation status: Implemented (Stmt enum); control flow / exceptions / pattern matching / async are fully covered; labels Partially implemented; fallthrough Rejected
Statements control the flow of execution and do not directly serve as expression values. print is a language-level global Host (signature in packages/core/builtins/special/globals.sy) whose call syntax looks like a function.
Classification
stmt ::= var-decl-stmt
| expr-stmt
| return-stmt
| if-stmt
| while-stmt
| repeat-while-stmt
| for-in-stmt
| break-stmt
| continue-stmt
| block-stmt
| try-stmt
| throw-stmt
| switch-stmt
| if-case-stmt
| if-let-stmt
| guard-let-stmt
| print-stmt
| macro-invoke-stmtVariable Declarations
var-decl ::= ('let' | 'var') pattern (':' type)? ('=' expr)?
| 'async' 'let' pattern '=' expr
| 'async' 'var' pattern '=' exprlet name = "SYX"
var count = 0
let x: Int = 10
async let result = await fetch() // async let
async var lazyRes: Int? = nil| Keyword | Mutability | Reassignment |
|---|---|---|
let | Immutable | ❌ S003 |
var | Mutable | ✅ |
async let | Immutable | ❌ |
async var | Mutable | ✅ |
Type inference: a
lettype is frozen after its first assignment; a later incompatible assignment → S004.
Diagnostics: S003 (assignment to let); S002 (undefined)
Expression Statements
expr-stmt ::= exprfoo() // Function call
x = 1 // Assignment
arr.append(2) // Method callAn assignment expression is a legal statement; it returns no value usable for chaining.
return
return-stmt ::= 'return' expr?return
return 42
return resultWith no
expr, this is equivalent toreturn ()(Void); a non-Void function mustreturn expr.
if
if-stmt ::= 'if' expr block ('else' (if-stmt | block))?if condition {
…
} else if other {
…
} else {
…
}Condition parentheses are optional: both
if exprandif (expr)are legal; omitting them is recommended (consistent with Swift). Condition type must beBool; anything else → S004. if expression (Phase 100+): when used as an expression, both branches must exist and have the same type.
while
while-stmt ::= 'while' expr blockwhile x > 0 {
x = x - 1
}Condition parentheses are optional.
repeat while
repeat-while-stmt ::= 'repeat' block 'while' exprrepeat {
n = n + 1
} while n < 3The loop body executes at least once; the condition is checked after each iteration. Condition parentheses are optional.
for-in
for-in-stmt ::= 'for' 'await'? pattern 'in' expr blockfor item in items { … }
for c in "abc" { … }
for i in 0..<10 { … }
for await x in asyncSeq { … }Iterable:
| Type | Element type |
|---|---|
Array<T> | T |
String | String (single scalar) |
Set<T> | T |
Dictionary<K, V> | (K, V) tuple |
0..<n | Int |
Custom Iterator | String |
Object | (String, Any) |
break / continue
break-stmt ::= 'break' label?
continue-stmt ::= 'continue' label?while true {
if done { break }
if skip { continue }
}
// Labels
outer: for x in items {
inner: for y in x.subs {
if y.shouldStop { break outer } // ⚠️ Labels are partially implemented
}
}Labels / cross-level
break/continueare Partially implemented.
Blocks
block-stmt ::= '{' stmt* '}'{
let local = 1
print(local)
}try / catch
try-stmt ::= 'try' block 'catch' '(' identifier (':' type)? ')' block
('catch' '(' identifier (':' type)? ')' block)*Note: a
tryblock uses{ };catchbindings must carry(), because thecatch errform is ambiguous with statements (catch is a keyword plus an identifier, which without parentheses collides withlet/binding).
do {
throw "error"
print("no catch")
} catch (err) {
print(err) // err: String (default when the type is omitted)
}
do {
throw "typed"
} catch (err: String) {
print(err) // Explicit type is optional (Java-like)
}Multiple catch clauses: several
catchclauses pluswhereguards are supported (Partially implemented). Catch binding type defaults toStringwhen omitted;catch (e: T)may be written; typed throws (throws T) are Partially implemented.
switch
switch-stmt ::= 'switch' expr '{' case-clause* '}'
case-clause ::= 'case' pattern ('where' expr)? block
| 'default' (':' | block)switch value {
case 1 {
print("one")
}
case 2, 3, 5, 7 {
print("small prime")
}
case 0..<10 {
print("single digit")
}
case .ok(let v) {
print(v)
}
case .err(let e) where e.length > 0 {
print(e)
}
default {
print("other")
}
}Condition parentheses are optional; both
switch exprandswitch (expr)are legal.
caseblock syntax: a SYXcasemust be followed by a{ }block (notcase 1:plus indentation).default: may be omitted for anenum/ closed domain (the compiler checks full case coverage → S007); an open domain needsdefault.fallthrough: Rejected (compile error).
if case / if let / guard let
if-case-stmt ::= 'if' 'case' pattern '=' expr block ('else' block)?
if-let-stmt ::= 'if' 'let' pattern '=' expr block ('else' block)?
guard-stmt ::= 'guard' 'let' pattern '=' expr 'else' blockif case .ok(let x) = result {
print(x)
} else {
print("not ok")
}
if let x = optional {
print("got \(x)")
}
guard let y = optional else {
return // Early exit required
}
print("y is \(y)")
guard letelse must exit (return/throw/break/continue/ an infinite loop) — otherwise S020. Multiple patterns:if let x = a, let y = b, cond { … }(chaining + guards) — legal.
print
print-stmt ::= 'print' '(' arg-list? ')'print(expr)
print("Hello", name, count)A built-in output statement (a dedicated statement, not a function). The call form
print(x)looks like a function; underneath it is a statement node.
throw
throw-stmt ::= 'throw' exprthrow "message"
throw errLegal only inside a
throwsfunction body; in a non-throwsfunction → S020.
Macro Invocation Statements
macro-invoke-stmt ::= '#' identifier '(' arg-list? ')'#assert(condition)
#for(3, print("x"))
#if(DEBUG) print("debug mode")See Attributes.
empty statement
; // An empty statement is allowed (uncommon)SYX does not require semicolons; they may be omitted.
Labels
label-stmt ::= identifier ':' stmtouter: for x in items {
for y in x.subs {
if y.bad { break outer } // ⚠️ Label support is still evolving
}
}Status: basic label support; recognition across functions / closures is still evolving.
Diagnostics: S003 / S004 / S007 / S010 / S017 / S020 / R007 / P001
Usage examples → Language Guide: Control Flow · Error Handling · Concurrency
See also → Declarations · Patterns · Expressions · Grammar Summary