Feat: Clarify Block unification & add if-statement example

This commit is contained in:
2025-09-22 09:30:07 -07:00
parent a408b4024c
commit 936de0e3eb

View File

@@ -103,12 +103,12 @@ version: 1.0 // Declaration and initialization
version = 1.1 // Assignment version = 1.1 // Assignment
\`\`\` \`\`\`
## The Block: A Unified Structure ## The Block: A Unified Structure for Objects and Functions
The \`{}\` syntax creates a **Block**, the single most foundational structure in Hi. A Block is a sequence of expressions that can represent data structures (like objects), executable code (like functions), or both simultaneously. 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 a Data Structure ### As a Data Structure (Object)
When a Block contains named declarations (\`key: value\`), it behaves like an object. A \`#\` prefix denotes a private property. To support code golfing, expression separators are flexible: When a Block contains primarily named declarations (\`key: value\`), it behaves like a traditional object or dictionary. A \`#\` prefix denotes a private property. To support code golfing, expression separators are flexible:
- **Newlines** act as separators. Commas or semicolons are optional. - **Newlines** act as separators. Commas or semicolons are optional.
- **Commas (\`,\`) or semicolons (\`;\`)** must be used to separate expressions on the same line. - **Commas (\`,\`) or semicolons (\`;\`)** must be used to separate expressions on the same line.
@@ -125,8 +125,8 @@ player: { name: "Orion", #hp: 100 }
point: { x: 10; y: 20 } point: { x: 10; y: 20 }
\`\`\` \`\`\`
### As Executable Code ### As Executable Code (Function)
A Block containing executable statements can be invoked like a function. Parameters are defined using \`()\` before the block. When a Block is defined with parameters (\`()\`) or invoked, it behaves like a function. It executes its sequence of expressions.
\`\`\`javascript \`\`\`javascript
sayHi: { _("Hi") } sayHi: { _("Hi") }
@@ -137,7 +137,7 @@ greet("Orion")
\`\`\` \`\`\`
### Unification: Data and Behavior ### Unification: Data and Behavior
Since a Block is just a sequence of expressions, data and behavior can be combined seamlessly. This provides natural encapsulation without classes. Inner blocks inherit the parent's scope, allowing access to its state. Because data declarations and executable statements can coexist in the same Block, Hi achieves a seamless fusion of state and behavior. This provides natural encapsulation without the need for traditional classes. Any "method" within a block can access its sibling "properties" directly.
\`\`\`javascript \`\`\`javascript
counter: { counter: {
@@ -187,17 +187,17 @@ All conditional logic is handled by a single ternary expression structure, which
// if / else // if / else
result: (1 > 2) ? { "A" } : { "B" } // result is "B" result: (1 > 2) ? { "A" } : { "B" } // result is "B"
// A missing 'else' on a false condition results in -0
value: (1 > 2) ? { "A" } // value is -0
// if / else if / else // if / else if / else
score: 75 score: 75
grade: (score >= 90) ? { "A" } grade: (score >= 90) ? { "A" }
: (score >= 80) ? { "B" } : (score >= 80) ? { "B" }
: (score >= 70) ? { "C" } : (score >= 70) ? { "C" }
: { "D" } // The final else case : { "D" }
_(grade) // Prints "C" _(grade) // Prints "C"
// Use for side-effects, like a traditional 'if'.
// The return value is discarded.
(grade == "C") ? { _("Passable") }
\`\`\` \`\`\`
## Data Structures ## Data Structures