vile / scheme / capture.v
28 lines · 26 sloc · 891 bytes · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3struct CaptureVisit {
4 function &Function
5 finish bool
6}
7
8// Direct lexical addresses establish each function's outer depth. A nested
9// child can also require a path through its parent's environment. Propagate
10// those needs after all bodies are built: let* constructs its child chain
11// before compiling the innermost body, and that chain can be very long.
12fn finalize_captures(root &Function) {
13 mut pending := [CaptureVisit{root, false}]
14 for pending.len > 0 {
15 visit := pending.pop()
16 mut function := visit.function
17 if !visit.finish {
18 pending << CaptureVisit{function, true}
19 for child in function.children { pending << CaptureVisit{child, false} }
20 } else {
21 for child in function.children {
22 depth := child.capture_depth - 1
23 if depth > function.capture_depth { function.capture_depth = depth }
24 }
25 function.prepare_integer_tail_loop()
26 }
27 }
28}
29