diff --git a/index.html b/index.html index 5c5064e..c6c99b0 100644 --- a/index.html +++ b/index.html @@ -106,48 +106,65 @@ version: 1.0 // Declaration and initialization version = 1.1 // Assignment \`\`\` -## The Block: Unified Data and Code +## The Block: Unifying Objects and Functions -In Hi, objects and functions are unified into a single structure: the **Block** (\`{}\`). A Block is a sequence of expressions that holds data, executes code, or both. It is invoked with \`()\`. +In Hi, the distinction between objects and functions is eliminated. Both concepts are unified into a single, foundational structure: the **Block** (\`{}\`). A Block is a sequence of expressions that can hold data, executable code, or both, making it a powerful tool for building complex structures. -### As Data (Object) -A Block with \`key: value\` declarations acts as an object. The \`#\` prefix denotes a private property. +### As a Data Structure (Object) +When a Block contains primarily named declarations (\`key: value\`), it behaves like a traditional object or dictionary. A \`#\` prefix denotes a private property, making it inaccessible from outside the block. \`\`\`javascript player: { - name: "Orion", + name: "Orion" #hp: 100 } _(player.name) // "Orion" -_(player.hp) // -0 (null) +_(player.hp) // -0 (null) because #hp is private \`\`\` -### As Code (Function) -A Block can be invoked like a function. Parameters are defined in parentheses before the Block. +### As Executable Code (Function) +When a Block is invoked via \`()\`, its sequence of expressions is executed. \`\`\`javascript +sayHi: { _("Hi") } +sayHi() // Invokes the block, prints "Hi" + greet: (name) { _("Hi, " + name) } greet("Orion") // Prints "Hi, Orion" \`\`\` -### As a Factory -A Block can mix data and code. When invoked, its expressions execute in order. Declarations are re-initialized on each call, making Blocks natural factories. The value of the last expression is implicitly returned. +### The Hybrid Block: Data and Logic Combined +The true power of Hi's design emerges when a Block contains both data declarations and executable statements. This creates a hybrid structure that can act as a stateful object, a function, or a factory for creating other objects. + +When a Block is invoked (\`()\`), its expressions are executed in sequence. Any data declarations inside are re-initialized with each call, making Blocks natural factories. The value of the Block's *last expression* is implicitly returned. \`\`\`javascript -// This Block configures and returns a new Block. +// This block is a factory for creating player objects. createPlayer: (name) { - name: name, hp: 100 // Data, initialized on call - _("Created " + name) // Action - @ // Returns the new Block context + // 1. Data declarations are initialized on each call + name: name, + hp: 100 + + // 2. An action is performed + _("Created " + name) + + // 3. The last expression returns the context (@), + // which is the newly configured block. + @ } +// Invoking the block executes its logic player1: createPlayer("Orion") + +// Accessing the data from the returned block _(player1.name) // "Orion" +_(player1.hp) // 100 \`\`\` +In this example, \`createPlayer\` is a Block that, when called, configures and returns a new Block. Each call to \`createPlayer\` produces a distinct player object with its own initial state. ### Expression Separators -To support code golfing, expression separators are flexible. Newlines act as implicit separators, while commas (\`,\`) or semicolons (\`;\`) are required for expressions on the same line. +To support code golfing, expression separators within a Block are flexible. Newlines act as implicit separators, while commas (\`,\`) or semicolons (\`;\`) are required to separate expressions on the same line. \`\`\`javascript // Multi-line style (separators optional) @@ -158,6 +175,7 @@ point: { // Single-line style (separators required) point: { x: 10, y: 20 } +point: { x: 10; y: 20 } \`\`\` ## Arrow Expressions @@ -480,3 +498,4 @@ To create the initial Hi-to-JS transpiler, the following primitives and built-in +