git @ Cat's Eye Technologies Fountain / master eg / kennel-story-0.fountain
master

Tree @master (Download .tar.gz)

kennel-story-0.fountain @master — raw · history · blame

//
// Simplified "Kennel story".
// On generation, `steps` should be provided externally
// (but on parsing, it is optional and can be omitted.)
// steps=11111 will produce a 50,000 word novel.
//

Goal<steps> ::=
    <. dx = 1 .> <. x = 0 .> <. a = 0 .> <. lx = 1 .>
    {
        DogMove<x, dx>
        <. x += dx .>
        DogTurnAround<x, dx, lx>
        <. a += 1 .>
    } <. (defined steps && a > steps) || (~defined steps) .> <. x = 0 .>  // SEE NOTE BELOW
    ;
DogMove<x, dx> ::=
      <. dx > 0 .> DogForth<x> #10
    | <. dx < 0 .> DogBack<x> #10
    ;
DogForth<x> ::=
      <. x < 1 .> "The dog ran away."
    | <. x > 0 .> "The dog ran further away."
    ;
DogBack<x> ::=
      <. x < 2 .> "The dog came back home."
    | <. x > 1 .> "The dog journeyed back."
    ;
DogTurnAround<x, dx, lx> ::=
      <. x > lx .> <. dx -= 2 .> <. lx += 1 .>
    | <. x < 1 .> <. dx += 2 .>
    | <. x >= 1 .> <. x <= lx .>
    ;

// NOTE

// The postcondition on this loop was originally written as:
//
//     <. a > steps .> <. x = 0 .>
//
// However, this had the effect of requiring that the number
// of steps be specified in advance (e.g. as a parameter on
// the `fountain` command line), not only when generating from
// this grammar, but also when parsing using it.  This in turn
// meant that it was not possible for `fountain` to parse a
// kennel story whose length in steps was not known in advance.
//
// It had this effect because, while `a = steps` performs
// unification, `a > steps` doesn't.  It just fails if
// `steps` isn't defined.  `a = steps` also has the downside
// that if the given `steps` isn't an even number, the loop
// will fail to terminate.
//
// Changing the `a > steps` test to its present form:
//
//     <. (defined steps && a > steps) || (~defined steps) .>
//
// ...causes it to allow steps to be undefined (or odd).  This
// allows, for example,
//
//    ./script/roundtrip-fountain.sh --open-ended eg/kennel-story-0.fountain steps=11111
//
// to succeed; in this "open-ended" usage of the roundtrip script,
// the parameters are not passed to the parse phase.  The defined
// check on steps allows the parser to flexibly accept any number
// of steps instead of having a particular number specified that it
// must match exactly.