condtional multi-unwrap and guard clauses

This commit is contained in:
2026-06-22 20:37:37 +02:00
parent 27f42dd253
commit 663f4dc658
9 changed files with 567 additions and 96 deletions
+4 -4
View File
@@ -80,7 +80,7 @@
- function-like macros and non-literal macro expressions remain unsupported
- static inline functions (implemented)
5. control flow
5. control flow (implemented)
- boolean expressions (implemented)
- `bool` type with `true` / `false` literals
- comparison operators: `==`, `!=`, `<`, `<=`, `>`, `>=` (numeric operands widen; `bool` supports only `==` / `!=`)
@@ -93,8 +93,8 @@
- single immutable binding scoped to the then-block; `v` not visible in `else` or after the `if`
- `|` lexes as a new `Pipe` token; the `.If` reuses AST `name` / HIR `local` to carry the binding (no new statement kind)
- new `Optional_Is_Some` / `Optional_Value` IR opcodes (the `Unwrap` presence-test + extract, minus the trap)
- conditional unwrapping with guard clause: `if val |v : v >= 10| { ... } else { ... }` - unwrap `val` into `v` if it is not `none`
- multi-unwrap (see section below)
- conditional unwrapping with guard clause (implemented): `if val |v : v >= 10| { ... } else { ... }` - enter the then-block when `val` is not `none` and the guard is true
- multi-unwrap (implemented; see section below)
- while loops (implemented; operates on boolean conditions). examples:
- `while condition { ... }` - iterate while the condition is true
- `while condition : i = i + 1 { ... }` - execute the update after each completed iteration
@@ -179,7 +179,7 @@ Ranges represent a sequence of values, commonly used in for loops, and is itself
(a + 1)..(b - 1) # OK: both sides parenthesized
# 0..n + 1 # ERROR: must parenthesize complex expressions
```
This rule keeps the grammar simple and forces clarity at the call site — no precedence rules to remember. Also, being a value type, ranges can be assigned to variables and passed around like any other value. Range bounds are evaluated once, must have compatible concrete integer types, and descending ranges are empty.
For-loop captures are immutable and scoped to the loop body. Sequence index captures are `usize`. Pointer capture uses `|@item|`; arrays must be passed by pointer (for example `&items`), while slices can be used directly. Sentinel elements are not included in iteration.