Inform 7 Home Page / Documentation
§11.3. Pattern matching
In this section, let's make the following new phrase:
To admire (item - an object):
say "You take a long look at [item].".
This does very little, of course, but it does allow the wording to be different each time the phrase is used:
admire the diamonds;
admire Mr Cogito;
admire the honey sandwich;
and our single definition covers all of these possibilities. The bracketed part of the definition, "(item - an object)", tells Inform to expect an object in that position, and Inform enforces this carefully. So this definition might tell Inform what "admire the barricade" means, but not what
admire "blue cheese";
admire 63;
mean. Unless some other definition sorts the matter out, Inform will reply to uses like this with a Problem message:
Problem. You wrote 'admire 63' , 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 Auction House:
admire the noun.
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 story 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; the alternative "--" means "no word at all", and thus makes it optional:
To grant (bonus - a number) point/points/--: ...
makes "grant 3" do the same as "grant 3 points".
If we need more variation than that, we should make more than one definition.
|
ExampleAhem
Writing a phrase, with several variant forms, whose function is to follow a rule several times.
|
|
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".
Here we use phrases that match individual items where possible, and the general kind otherwise:
"Ferragamo Again"
The Break Room is a room. Vanessa, Tina, and Lisa are women in the Break Room. Mark and Holman are men in the Break Room.
Understand the commands "ask" and "tell" and "answer" as something new.
Understand "talk about [any subject]" as talking about. Talking about is an action applying to one visible thing.
Understand "talk about [text]" as talking randomly about. Talking randomly about is an action applying to one topic. Carry out talking randomly about: say "Mostly you're interested in [the list of subjects]."
Carry out talking about something:
now the previous subject is the noun.
Report talking about something:
say "You chat for a while about [the noun]."
A subject is a kind of thing. Assyrian vowel sounds, designer handbags, and instant run-off voting are subjects. Understand "linguistics" and "mute" and "stop" as sounds. Understand "prada" and "tods" and "coach" and "carmen marc valvo" as designer handbags. Understand "reform" and "election" and "election fraud" and "two-party system" and "Diebold" as instant run-off voting.
To say (annoyed-person - a person) gestures in irritation:
say "[The annoyed-person] sighs heavily. [run paragraph on]"
To say (annoyed-person - Vanessa) gestures in irritation:
say "[The annoyed-person] takes off her glasses and polishes them on her sleeve. [run paragraph on]".
To say (annoyed-person - Holman) gestures in irritation:
say "Holman bobs his head. [run paragraph on]"
The previous subject is a subject that varies.
Instead of talking about something for more than one turn:
if the noun is the previous subject, say "[a random visible person who is not the player gestures in irritation]Maybe you should let this one go.[line break][paragraph break]";
otherwise continue the action.
Test me with "talk about chocolate / talk about vowel sounds / g / talk about handbags / talk about prada / talk about tods".