Ticket #1521: bug.pir

File bug.pir, 1.2 KB (added by arnsholt, 12 years ago)

(Slightly) condensed code.

Line 
1.sub 'main' :main
2    # Set up the stack of continuations, representing our choice points.
3    $P0 = new 'ResizablePMCArray'
4    set_global 'ccs', $P0
5
6    # Our exit strategy. When no more options are left to backtrack over, exit.
7    $P1 = new 'Continuation'
8    set_addr $P1, done
9
10    push $P0, $P1 # Put the continuation on the stack.
11
12    $S0 = choose("foo", "bar", "blech")
13    $S0 = "main:   " . $S0
14    say $S0
15
16    'backtrack'()
17
18  done:
19    say "Done."
20.end
21
22.sub 'choose'
23    .param pmc options :slurpy
24
25    .local pmc value
26
27    if options goto got_options
28    'backtrack'() # Out of options. Backtrack.
29  got_options:
30
31    value = shift options
32
33    # Create a choice point and store it on the stack.
34    $P0 = new 'Continuation'
35    set_addr $P0, recurse
36    $P1 = get_global 'ccs'
37    push $P1, $P0
38
39    $S0 = value
40    $S0 = "choose: " . $S0
41    say $S0
42
43    .return (value)
44
45  recurse:
46    # On backtrack, recurse on the reduced list of options.
47    #.tailcall 'choose'(options :flat)
48    .tailcall 'choose'(options :flat)
49.end
50
51.sub 'backtrack'
52    $P0 = get_global 'ccs' # Get the stack.
53    $P1 = pop $P0        # Get the next continuation in line.
54    $P1()                  # Invoke the continuation.
55.end