Skip to content

Commit 8f5ed9b

Browse files
committed
Refresh control-flow and error docs for loop semantics
1 parent f9b4c38 commit 8f5ed9b

3 files changed

Lines changed: 89 additions & 17 deletions

File tree

ROADMAP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Goal: improve language ergonomics for complex script logic and recovery behavior
232232

233233
- [x] Add parser/runtime tests for each new control flow construct.
234234
- [x] Add nested control flow tests for edge cases.
235-
- [ ] Add docs updates in `docs/control-flow.md` and `docs/errors.md`.
235+
- [x] Add docs updates in `docs/control-flow.md` and `docs/errors.md`.
236236
- [ ] Add examples under `examples/control_flow/` for each new feature.
237237

238238
### v0.16.0 Definition of Done

docs/control-flow.md

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,74 @@
11
# Control Flow and Ranges
22

3-
VibeScript supports familiar Ruby control structures:
3+
VibeScript supports these control-flow forms:
44

5-
- `if/elsif/else` expressions
6-
- `for` loops iterating arrays or ranges
7-
- Numeric ranges via `start..finish`
5+
- `if` / `elsif` / `else`
6+
- `for` loops over arrays and ranges
7+
- `while` and `until` loops
8+
- loop control with `break` and `next`
9+
- numeric ranges via `start..finish`
810

9-
Example range usage:
11+
## `for` loops
1012

1113
```vibe
12-
def fizzbuzz(limit)
13-
for n in 1..limit
14-
if n % 15 == 0
15-
puts("FizzBuzz")
16-
elsif n % 3 == 0
17-
puts("Fizz")
18-
elsif n % 5 == 0
19-
puts("Buzz")
20-
else
21-
puts(n)
14+
def sum_first_five()
15+
total = 0
16+
for n in 1..5
17+
total = total + n
18+
end
19+
total
20+
end
21+
```
22+
23+
## `while` and `until`
24+
25+
```vibe
26+
def countdown(n)
27+
out = []
28+
while n > 0
29+
out = out + [n]
30+
n = n - 1
31+
end
32+
out
33+
end
34+
35+
def count_up(limit)
36+
out = []
37+
n = 0
38+
until n >= limit
39+
out = out + [n]
40+
n = n + 1
41+
end
42+
out
43+
end
44+
```
45+
46+
## `break` and `next`
47+
48+
```vibe
49+
def odds_under_five()
50+
out = []
51+
for n in [1, 2, 3, 4, 5]
52+
if n == 5
53+
break
2254
end
55+
if n % 2 == 0
56+
next
57+
end
58+
out = out + [n]
2359
end
60+
out
2461
end
2562
```
2663

27-
See `examples/loops/` and `examples/ranges/` for runnable scripts.
64+
Semantics:
65+
66+
- In nested loops, `break` and `next` target the nearest active loop.
67+
- `break`/`next` used outside any loop raise runtime errors.
68+
- `break`/`next` cannot cross call boundaries (for example from block callbacks back into outer loops).
69+
70+
## Quotas
71+
72+
Loop execution participates in step and memory quotas. Infinite loops will terminate with quota errors when limits are exceeded.
73+
74+
See `examples/control_flow/`, `examples/loops/`, and `examples/ranges/` for runnable scripts.

docs/errors.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ division by zero
3737
at calculate (7:7)
3838
```
3939

40+
## Type Errors
41+
42+
Typed argument and return checks include:
43+
44+
- parameter or function context
45+
- expected type
46+
- actual runtime type
47+
48+
```text
49+
argument payload expected { id: string, score: int }, got { id: string, score: string }
50+
```
51+
52+
For composite values, actual types include shape/element detail (`array<int | string>`, `{ id: string, ... }`) to make fixes local and explicit.
53+
54+
## Loop Control Errors
55+
56+
Loop control diagnostics are explicit:
57+
58+
- `break used outside of loop`
59+
- `next used outside of loop`
60+
- `break cannot cross call boundary`
61+
- `next cannot cross call boundary`
62+
63+
These boundary errors happen when `break`/`next` are raised inside called blocks/functions and attempt to escape into an outer loop.
64+
4065
## REPL Debugging
4166

4267
The REPL stores the previous failure. Use:

0 commit comments

Comments
 (0)