Control Flow
If / Elif / Else
Section titled “If / Elif / Else”Purus uses indentation to define blocks — no curly braces {} needed.
if x lt 0 console.log[///negative///]elif x eq 0 console.log[///zero///]else console.log[///positive///]if (x < 0) { console.log("negative");} else if (x === 0) { console.log("zero");} else { console.log("positive");}Multi-line blocks
Section titled “Multi-line blocks”Any statements indented under if, elif, or else belong to that block:
if logged-in const user be get-user[] console.log[///Welcome, [user.name]///] show-dashboard[]else console.log[///Please log in///] redirect[///login///]if (logged_in) { const user = get_user(); console.log(`Welcome, ${user.name}`); show_dashboard();} else { console.log("Please log in"); redirect("login");}Unless
Section titled “Unless”Negated if:
unless done keep-going[]if (!(done)) { keep_going();}Inline if (ternary)
Section titled “Inline if (ternary)”const result be if condition then 1 else 2const result = condition ? 1 : 2;Postfix modifiers
Section titled “Postfix modifiers”Write short conditionals and loops on a single line:
console.log[///debug///] if verboseconsole.log[///skip///] unless doneconsole.log[item] for item in listif (verbose) { console.log("debug");}if (!(done)) { console.log("skip");}for (const item of list) { console.log(item);}While / Until
Section titled “While / Until”while i lt 10 i be i add 1
until finished do-work[]while (i < 10) { i = i + 1;}while (!(finished)) { do_work();}Do-While
Section titled “Do-While”Execute a block at least once, then repeat while the condition is true:
do process-item[]while has-more[]do { processItem();} while (hasMore());For-in
Section titled “For-in”for item in items console.log[item]for (const item of items) { console.log(item);}With index:
for i; item in items console.log[i; item]for (let [i, item] of items.entries()) { console.log(i, item);}For-range
Section titled “For-range”for i in range 0; 10 console.log[i]for (let i = 0; i < 10; i++) { console.log(i);}For (JS-style)
Section titled “For (JS-style)”A C-style for loop with init, condition, and update clauses:
for let i be 0; i lt 10; i add be 1 console.log[i]for (let i = 0; i < 10; i += 1) { console.log(i);}Works with any compound assignment operator:
for let x be 1; x lt 100; x mul be 2 console.log[x]for (let x = 1; x < 100; x *= 2) { console.log(x);}Plain assignment in the update clause:
for let i be 0; i lt 5; i be i add 1 console.log[i]for (let i = 0; i < 5; i = i + 1) { console.log(i);}Using postfix/prefix increment with \add and add\ (no spaces needed):
for let i be 0; i lt 10; i\add console.log[i]for (let i = 0; i < 10; i++) { console.log(i);}Counting down with \sub:
for let i be 9; i ge 0; i\sub console.log[i]for (let i = 9; i >= 0; i--) { console.log(i);}Switch / Case / Default
Section titled “Switch / Case / Default”switch x case 1 then ///one/// case 2 then ///two/// default ///other///if (x === 1) { "one";} else if (x === 2) { "two";} else { "other";}With guards:
In case n if ..., the variable n is bound to the switch subject (value), allowing the guard condition to reference it.
switch value case n if n gt 0 console.log[///positive///] default console.log[///non-positive///]{ const n = value; if (n > 0) { console.log("positive"); } else { console.log("non-positive"); }}Match / When
Section titled “Match / When”match x when 1 then ///one/// when 2 then ///two/// else ///other///if (x === 1) { "one";} else if (x === 2) { "two";} else { "other";}With guards:
In when n if ..., the variable n is bound to the match subject (value), allowing the guard condition to reference it.
match value when n if n gt 0 console.log[///positive///] else console.log[///non-positive///]{ const n = value; if (n > 0) { console.log("positive"); } else { console.log("non-positive"); }}try / catch / finally
Section titled “try / catch / finally”try risky[]catch e console.log[e]finally cleanup[]try { risky();} catch (e) { console.log(e);} finally { cleanup();}Try as expression
Section titled “Try as expression”const result be try risky[]catch e default-valuelet result;try { result = risky();} catch (e) { result = defaultValue;}throw new Error[///something went wrong///]throw err if conditionthrow new Error("something went wrong");if (condition) { throw err;}