Syntax Overview
File Extensions
Section titled “File Extensions”| Extension | Output | Description |
|---|---|---|
.purus | .js | Standard JavaScript |
.cpurus | .cjs | CommonJS module |
.mpurus | .mjs | ES Module |
Comments
Section titled “Comments”-- This is a line comment
--- This is ablock comment ---Strings
Section titled “Strings”Strings use triple slashes ///:
const greeting be ///Hello, World///Escape sequences
Section titled “Escape sequences”| Escape | Result |
|---|---|
\n | Newline |
\t | Tab |
\\ | Backslash |
\/ | / |
\[ | [ (literal bracket) |
\] | ] (literal bracket) |
String interpolation
Section titled “String interpolation”Use [expr] inside a string to embed expressions:
const name be ///Alice///const age be 30const msg be ///Hello, [name]! You are [age] years old.///Compiles to:
const name = "Alice";const age = 30;const msg = `Hello, ${name}! You are ${age} years old.`;You can use any expression inside the brackets:
const x be 10const result be ///[x] times 2 is [x mul 2]///Compiles to:
const x = 10;const result = `${x} times 2 is ${x * 2}`;To include a literal [ or ] in a string, use the escape sequences \[ and \].
Numbers
Section titled “Numbers”const i be 42const f be 3.14Booleans and null
Section titled “Booleans and null”const a be trueconst b be falseconst c be nullconst d be nil -- alias for nullconst e be undefinedSeparators: ; vs ,
Section titled “Separators: ; vs ,”Purus has two separators with distinct roles:
| Separator | Usage | JS Output |
|---|---|---|
; | Function arguments, function parameters, destructuring | , |
, | Array elements, object properties | , |
Both compile to , in JavaScript, but they serve different purposes in Purus:
; — Arguments and Parameters
Section titled “; — Arguments and Parameters”Use ; to separate function arguments when calling functions and parameters in function declarations:
-- Function parametersfn add a; b return a add b
-- Function argumentsadd[1; 2]console.log[///hello///; ///world///]Math.max[1; 2; 3]
-- Destructuringconst [a; b; c] be arrconst object[name; age] be person, — Data Separators
Section titled “, — Data Separators”Use , to separate array elements and object properties:
-- Arraysconst arr be [1, 2, 3]
-- Objectsconst obj be [name be ///Alice///, age be 30]Why two separators?
Section titled “Why two separators?”Since Purus uses [] for both function calls and arrays, the two separators help distinguish between them:
-- `;` makes this a function call with 3 argumentsfn[a; b; c] -- fn(a, b, c)
-- `,` makes this an array with 3 elements[a, b, c] -- [a, b, c]
-- Nested calls use `;` to separate argumentsouter[inner[x]; y] -- outer(inner(x), y)Arrays
Section titled “Arrays”const arr be [1, 2, 3]const arr2 be [1; 2; 3] -- semicolons also workconst empty be []Array ranges
Section titled “Array ranges”const inclusive be [0..5] -- [0, 1, 2, 3, 4, 5]const exclusive be [0...5] -- [0, 1, 2, 3, 4]Slicing
Section titled “Slicing”Extract a portion of an array using \ prefix with .. (inclusive) or ... (exclusive):
const numbers be [0, 1, 2, 3, 4, 5, 6]const middle be numbers[\2..5] -- [2, 3, 4, 5]const partial be numbers[\2...5] -- [2, 3, 4]Compiles to:
const middle = numbers.slice(2, 5 + 1);const partial = numbers.slice(2, 5);Splicing
Section titled “Splicing”Replace a portion of an array by assigning to a slice:
numbers[\2..4] be [///a///; ///b///; ///c///]-- numbers is now [0, 1, "a", "b", "c", 5, 6]Compiles to:
numbers.splice(2, 4 - 2 + 1, "a", "b", "c");Destructuring
Section titled “Destructuring”Extract values from arrays into variables:
const weather be [///Sunny///; ///Rainy///]const [today; tomorrow] be weather
-- Swap variables[today; tomorrow] be [tomorrow; today]Compiles to:
const [today, tomorrow] = weather;[today, tomorrow] = [tomorrow, today];Objects
Section titled “Objects”const obj be [name be ///Alice///, age be 30]const empty-obj be [be] -- empty objectObject destructuring
Section titled “Object destructuring”Use object[...] to destructure properties from an object:
const person be [name be ///Alice///, age be 30]const object[name; age] be personCompiles to:
const person = { name: "Alice", age: 30 };const { name, age } = person;Brackets only
Section titled “Brackets only”Purus uses [] for everything — function calls, arrays, objects, and grouping. No () or {}.
Computed Access
Section titled “Computed Access”Use \ inside brackets to access array indices or object properties by expression:
const val be arr[\0] -- arr[0]const item be obj[\key] -- obj[key]const x be matrix[\i][\j] -- matrix[i][j]This \ prefix distinguishes computed access from function calls:
| Syntax | Meaning | JS Output |
|---|---|---|
f[x] | Function call | f(x) |
arr[\x] | Computed access | arr[x] |
arr[\2..4] | Slice | arr.slice(2,5) |
Optional Chaining
Section titled “Optional Chaining”Use \. for optional chaining (JS ?.):
const name be user\.name -- user?.nameconst val be obj\.method[1; 2] -- obj?.method(1, 2)Multi-line Brackets
Section titled “Multi-line Brackets”Brackets [...] can span multiple lines. Newlines and indentation between items are ignored:
const items be [ 1; 2; 3]
const config be [ host be ///localhost///, port be 8080]
fetch[url].then[ fn response return response.json[]].catch[ fn err console.error[err]]Indentation
Section titled “Indentation”Blocks are defined by indentation (2 spaces recommended):
if x gt 0 console.log[///positive///]else console.log[///non-positive///]Identifiers
Section titled “Identifiers”Identifiers can contain hyphens (-) and underscores (_), which are both converted to underscores in JavaScript output:
const my-variable be 42-- compiles to: const my_variable = 42;
const my_variable2 be 43-- compiles to: const my_variable2 = 43;Hyphens and underscores are interchangeable — my-var and my_var refer to the same JavaScript variable (my_var). If you need to work with JS libraries that use underscores, you can use either form in Purus.
JavaScript Comparison
Section titled “JavaScript Comparison”Purus replaces many JavaScript symbols with words. Here is a quick reference for common JavaScript patterns and their Purus equivalents:
Template literals (${})
Section titled “Template literals (${})”JavaScript uses backticks and ${} for template literals. Purus uses /// strings with []:
-- JavaScript: `Hello, ${name}! You are ${age} years old.`const msg be ///Hello, [name]! You are [age] years old.///
-- JavaScript: `${a} + ${b} = ${a + b}`const result be ///[a] + [b] = [a add b]///const msg = `Hello, ${name}! You are ${age} years old.`;const result = `${a} + ${b} = ${a + b}`;Nullish coalescing (??)
Section titled “Nullish coalescing (??)”JavaScript’s ?? operator becomes coal in Purus:
-- JavaScript: const port = config.port ?? 3000;const port be config.port coal 3000
-- JavaScript: const name = user?.name ?? "Anonymous";const name be user\.name coal ///Anonymous///const port = config.port ?? 3000;const name = user?.name ?? "Anonymous";Ternary operator (? :)
Section titled “Ternary operator (? :)”JavaScript’s ternary condition ? a : b becomes if condition then a else b:
-- JavaScript: const label = count > 0 ? "items" : "empty";const label be if count gt 0 then ///items/// else ///empty///const label = count > 0 ? "items" : "empty";Optional chaining (?.)
Section titled “Optional chaining (?.)”JavaScript’s ?. becomes \. in Purus:
-- JavaScript: user?.address?.cityuser\.address\.city
-- JavaScript: arr?.[0]arr\.[\0]user?.address?.city;arr?.[0];Strict equality (=== / !==)
Section titled “Strict equality (=== / !==)”JavaScript’s === becomes eq (or is), and !== becomes neq (or not eq):
-- JavaScript: if (x === 0) {}if x eq 0 -- ...
-- JavaScript: if (x !== null) {}if x neq null -- ...if (x === 0) {}if (x !== null) {}Arrow functions (=>)
Section titled “Arrow functions (=>)”JavaScript’s => arrow functions become fn … to (expression body) or fn with indented block:
-- JavaScript: const double = (x) => x * 2;const double be fn x to x mul 2
-- JavaScript: const greet = (name) => { console.log(name); };const greet be fn name console.log[name]const double = (x) => x * 2;const greet = (name) => { console.log(name);};