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

Tree @master (Download .tar.gz)

anbncn-expanded2.fountain @masterraw · history · blame

// An attempt to expand out the anbncn example, to use
// recursion instead of loops, using a lookahead assertion.
//
// This works for parsing (with known or unknown `n`) but not
// generation, because we need to decide what the lookahead
// constraints evaluate to in generation; either they evaluate
// to different things, in which case always the first branch
// will be taken (generating an infinite string), or always the
// second branch will be taken (generating the null string);
// or they evaluate to the same thing, in which case Fountain
// can't decide which branch to take.
//
// We should add additional constraints that, unlike the look-
// ahead assertions, can be seen by the generation process, to
// direct it; but these, in turn, should be invisible or other-
// wise inconsequential to the parsing process.

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