git @ Cat's Eye Technologies Fountain / master eg / anbncn-expanded3.fountain
master

Tree @master (Download .tar.gz)

anbncn-expanded3.fountain @masterraw · history · blame

// Expand out the anbncn example, to use recursion instead of
// loops, using lookahead and generating constraints.
//
// This works for parsing, with known or unknown `n`, and also
// for generation (which requires a known `n` regardless),
// because we split into seperate parsing and generating
// cases; in the parsing case we exploit lookahead, while
// in the generating case we exploit the fact that `n` must
// already be known.
//
// This trasformation avoids some of the pitfalls of the other
// attempts to convert iteration into recursion, and it has been
// implemented in the reference implementation of Fountain.

Goal<n> ::= <. a = 0 .> Loop0<a, n>
            <. b = 0 .> Loop1<b, n>
            <. c = 0 .> Loop2<c, n>
            ;
Loop0<a, n> ::= <. (~generating && ?"a")  || (generating && ~a = n) .>  "a" <. a += 1 .> Loop0<a, n>
              | <. (~generating && ~?"a") || (generating && a = n) .>   <. a = n .>;
Loop1<b, n> ::= <. (~generating && ?"b")  || (generating && ~b = n) .>  "b" <. b += 1 .> Loop1<b, n>
              | <. (~generating && ~?"b") || (generating && b = n) .>   <. b = n .>;
Loop2<c, n> ::= <. (~generating && ?"c")  || (generating && ~c = n) .>  "c" <. c += 1 .> Loop2<c, n>
              | <. (~generating && ~?"c") || (generating && c = n) .>   <. c = n .>;