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 ## 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. - \`:\` **Declaration**: Binds a name in the current scope.
- \`=\` **Assignment**: Reassigns the value of an existing name. - \`=\` **Assignment**: Reassigns the value of an existing name.
\`\`\`javascript \`\`\`javascript
version: 1.0 // Declaration and initialization version: 1.0; version = 1.1
version = 1.1 // Assignment
\`\`\` \`\`\`
## The Block: A Unified Structure ## 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. A Block with only executable code is a function.
\`\`\`javascript \`\`\`javascript
sayHi: { sayHi: { _("Hi") }
_("Hi")
}
sayHi() // Invokes the block 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. A Block with named properties is an object. A \`#\` prefix denotes a private property, inaccessible from outside the block's scope.
\`\`\`javascript \`\`\`javascript
player: { // A Block can be defined on one line using ';'
name: "Orion" // Public property player: { name: "Orion"; #hp: 100 }
#hp: 100 // Private property
}
_(player.name) // "Orion" _(player.name) // "Orion"
\`\`\` \`\`\`
@@ -134,14 +129,8 @@ Blocks can contain both state and methods. Inner blocks lexically inherit the sc
\`\`\`javascript \`\`\`javascript
counter: { counter: {
#value: 0 #value: 0
inc: { value = value + 1 } // Concise one-liner methods
inc: { get: { value } // Last expression is implicitly returned
value = value + 1 // Mutates parent's private state
}
get: {
value // The last expression is implicitly returned
}
} }
counter.inc() counter.inc()
@@ -151,9 +140,7 @@ _(counter.get()) // Prints 1
### Blocks with Parameters ### Blocks with Parameters
\`\`\`javascript \`\`\`javascript
greet: (name) { greet: (name) { _("Hi, " + name) }
_("Hi, " + name)
}
greet("Orion") greet("Orion")
\`\`\` \`\`\`
@@ -214,11 +201,36 @@ Arrays are 0-indexed lists of values.
\`\`\`javascript \`\`\`javascript
primes: [2, 3, 5, 7] primes: [2, 3, 5, 7]
firstPrime: primes[0] // Access
primes[0] = 1 // Mutation primes[0] = 1 // Mutation
_(primes) // [1, 3, 5, 7] _(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 ## Repetition
Loops are initiated with the \`*\` operator following a standard three-part condition or a simple boolean expression. 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 // while-style loop
active: !0 active: !0
(active) * { (active) * {
_("Looping...") _("Looping..."); active = 0
active = 0 // Condition becomes false
} }
// Loop Control // Loop Control
@@ -253,7 +264,7 @@ Modules are imported using the \`+\` and \`->\` operators. This allows for destr
\`\`\`javascript \`\`\`javascript
// Import 'block1' and 'block2' (as 'alias2') from a module // 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() block1()
alias2("some value") alias2("some value")
@@ -266,11 +277,9 @@ A Block's return behavior is designed for fluency. It implicitly returns the val
\`\`\`javascript \`\`\`javascript
calculator: { calculator: {
#total: 0 #total: 0
// Last expression is an assignment, so 'this' is returned add: (n) { total = total + n } // Implicitly returns 'this'
add: (n) { total = total + n } sub: (n) { total = total - n } // Implicitly returns 'this'
sub: (n) { total = total - n } get: { ^ total } // Explicitly returns the total
// Last expression is a value, which is returned
get: { ^ total }
} }
// .add() and .sub() return the calculator, allowing the chain // .add() and .sub() return the calculator, allowing the chain