Feat: Add destructuring and apply one-liners

This commit is contained in:
2025-09-15 16:37:02 -07:00
parent b48baa557b
commit 1b41b443a7

View File

@@ -93,14 +93,13 @@ _("Hi world")
## Declaration and Assignment
Variable lifecycle is split into two distinct symbolic operations.
Variable lifecycle is split into two distinct symbolic operations. A semicolon \`;\` can be used to separate expressions on a single line.
- \`:\` **Declaration**: Binds a name in the current scope.
- \`=\` **Assignment**: Reassigns the value of an existing name.
\`\`\`javascript
version: 1.0 // Declaration and initialization
version = 1.1 // Assignment
version: 1.0; version = 1.1
\`\`\`
## The Block: A Unified Structure
@@ -111,9 +110,7 @@ The \`{}\` syntax creates a **Block**, the foundational structure in Hi. A Block
A Block with only executable code is a function.
\`\`\`javascript
sayHi: {
_("Hi")
}
sayHi: { _("Hi") }
sayHi() // Invokes the block
\`\`\`
@@ -121,10 +118,8 @@ sayHi() // Invokes the block
A Block with named properties is an object. A \`#\` prefix denotes a private property, inaccessible from outside the block's scope.
\`\`\`javascript
player: {
name: "Orion" // Public property
#hp: 100 // Private property
}
// A Block can be defined on one line using ';'
player: { name: "Orion"; #hp: 100 }
_(player.name) // "Orion"
\`\`\`
@@ -134,14 +129,8 @@ Blocks can contain both state and methods. Inner blocks lexically inherit the sc
\`\`\`javascript
counter: {
#value: 0
inc: {
value = value + 1 // Mutates parent's private state
}
get: {
value // The last expression is implicitly returned
}
inc: { value = value + 1 } // Concise one-liner methods
get: { value } // Last expression is implicitly returned
}
counter.inc()
@@ -151,9 +140,7 @@ _(counter.get()) // Prints 1
### Blocks with Parameters
\`\`\`javascript
greet: (name) {
_("Hi, " + name)
}
greet: (name) { _("Hi, " + name) }
greet("Orion")
\`\`\`
@@ -214,11 +201,36 @@ Arrays are 0-indexed lists of values.
\`\`\`javascript
primes: [2, 3, 5, 7]
firstPrime: primes[0] // Access
primes[0] = 1 // Mutation
_(primes) // [1, 3, 5, 7]
\`\`\`
## Destructuring
Destructuring provides an expressive way to extract data from Blocks and Arrays. The extraction operator \`->\` is used to unpack values, mirroring its use in module imports.
- \`source -> { pattern }\`: Extracts properties from a Block.
- \`source -> [ pattern ]\`: Extracts elements from an Array.
- \`prop -> alias\`: The same operator is used within the pattern for aliasing.
\`\`\`javascript
// Setup
user: { name: "Zeta"; role: "Admin"; id: 101 }
coords: [10, -5, 8]
// Extract 'name' and 'role' from the user block
user -> { name, role }
_(name) // "Zeta"
// Extract 'id' and alias it to 'userID'
user -> { id -> userID }
_(userID) // 101
// Extract first two elements from the array
coords -> [x, y]
_(y) // -5
\`\`\`
## Repetition
Loops are initiated with the \`*\` operator following a standard three-part condition or a simple boolean expression.
@@ -235,8 +247,7 @@ Loops are initiated with the \`*\` operator following a standard three-part cond
// while-style loop
active: !0
(active) * {
_("Looping...")
active = 0 // Condition becomes false
_("Looping..."); active = 0
}
// Loop Control
@@ -253,7 +264,7 @@ Modules are imported using the \`+\` and \`->\` operators. This allows for destr
\`\`\`javascript
// Import 'block1' and 'block2' (as 'alias2') from a module
+ "npm://hi-lang@0.1/path/file.hi" -> { block1, block2: alias2 }
+ "npm://hi-lang@0.1/path/file.hi" -> { block1, block2 -> alias2 }
block1()
alias2("some value")
@@ -266,11 +277,9 @@ A Block's return behavior is designed for fluency. It implicitly returns the val
\`\`\`javascript
calculator: {
#total: 0
// Last expression is an assignment, so 'this' is returned
add: (n) { total = total + n }
sub: (n) { total = total - n }
// Last expression is a value, which is returned
get: { ^ total }
add: (n) { total = total + n } // Implicitly returns 'this'
sub: (n) { total = total - n } // Implicitly returns 'this'
get: { ^ total } // Explicitly returns the total
}
// .add() and .sub() return the calculator, allowing the chain