Chapter 11: Phrases
11.3. Pattern matching

In this section, let's take the built-in phrase "remove ... from play" as an example. As the wording suggests, this causes whatever thing is named to disappear from every room, going into a sort of off-stage limbo.

Although the wording can be different each time this is used:

remove the diamonds from play;
remove Mr Cogito from play;
remove the honey sandwich from play;

... a single definition in Inform's Standard Rules nevertheless covers all of these possibilities. It is written like so:

To remove (item - an object) from play: ...

The bracketed part of the definition tells Inform to expect an object in that position, and Inform enforces this carefully. So this definition might tell Inform what "remove the barricade from play" means, but not what

remove "blue cheese" from play;
remove 63 from play;

mean. Unless some other definition sorts the matter out, Inform will reply to uses like this with a Problem message:

Problem. You wrote 'remove 63 from play' , but '63' has the wrong kind of value: a number rather than an object.

The object does not need to be named literally, but can be anything which works out to be an object: for instance,

After dropping something in the Nebulous Mist:
    remove the noun from play.

which Inform allows because "noun", here, is a name for the object which is being acted on.

Inform decides which definition to apply in a process called "pattern matching".

The bracketed part of the example definition has the form "(name - kind)". The definition only applies if the text supplied agrees with the "kind" part - for instance, the diamonds agreed with "object", but 63 did not. If the definition does apply, then the Inform works through the rest of the phrase using "name" to mean whatever value matched. For example:

To slam shut (box - an open container):
    say "With great panache, you slam shut [the box].";
    now the box is closed.

When this phrase is followed, "box" means whatever open container the pattern-matcher found when it was called for. For example, if Inform reads

slam shut the Dutch armoire;

then it acts on this by following the definition of "slam shut ...", using the Dutch armoire object as the value of "box", so it prints:

With great panache, you slam shut the Dutch armoire.

and renders it closed.

In fact any description can be given in the definition, and that includes a single, specific value. For instance, we could define:

To grant (bonus - a number) points:
    increase the score by the bonus.

To grant (bonus - 7) points:
    say "You shiver uncontrollably."

which would withhold this unlucky bounty. That would mean that:

grant 7 points;
grant seven points;

would each produce uncontrollable shivers, because Inform uses the definition applying to the number 7; but

grant six points;

would increase the score by 6. In general Inform always follows the principle that more specific definitions take priority over more general ones. So although the definitions:

To grant (bonus - a number) points: ...
To grant (bonus - 7) points: ...

both apply to the case of "grant 7 points", Inform uses the second, because it's the more specific of the two possibilities.

Sometimes it will not be possible to tell if the value supplied meets the requirements until the game is actually playing. If, at run-time, no definition fits some phrase which has to be carried out, a run-time problem message is produced.

Finally, and more straightforwardly, we can specify variations in wording using slashes between alternative words in a "To ..." definition. For instance:

To grant (bonus - a number) point/points: ...

allows the final word to be either "point" or "points". Slashes like this can only be used with literal words, not bracketed values, and give alternative forms only of a single word at a time. (If we need more variation than that, we should make more than one definition.)


169
* Example  Ahem
Writing a phrase, with several variant forms, whose function is to follow a rule several times.

RB

As we see in the example here, it is possible to use slashed variations in more than one place in a phrase, and to offer a number of separate forms. The main rule of thumb to remember is that value inputs for the phrase should always be separated by some text; so

To do/follow (chosen rule - a rule) exactly/precisely/just/-- (N - a number) time/times:
    ....

would cause a problem when we tried to call it with

follow the throat-clearing rule 2 times.

In general, we probably don't need to make our phrase definitions quite so flexible as this, but it's a good idea to account for "a" vs. "the", and for the possibility of using singular and plural forms, especially when writing extensions or other source to be shared.

"Ahem"

To do/follow (chosen rule - a rule) exactly/precisely/just (N - a number) time/times:
    repeat with index running from 1 to N:
        follow chosen rule.

This is the throat-clearing rule:
    say "'Ahem,' says [a random visible person who is not the player]."

After waiting:
    do the throat-clearing rule just one time.

Instead of listening:
    follow the throat-clearing rule precisely three times.

Instead of smelling:
    follow the throat-clearing rule exactly 2 times.

Chateau Marmont is a room. Tom, Jack, Zsa-Zsa, and Wilma-Faye are people in the Chateau. Zsa-Zsa and Wilma-Faye are women.

Test me with "wait / smell / listen".

170
** Example  Ferragamo Again
Using the same phrase to produce different results with different characters.

RB


PreviousContentsNext