Inform: Parse_Name Variations

Here's a weirdly useful piece of code, and I say "weirdly" because it doesn't do anything:

  
parse_name [ wd num;
  wd = NextWord();
  while (WordInProperty(wd, self, name)) {
    num++;
    wd = NextWord();
  }
  return num;
],

This parse_name routine causes the object to behave exactly as if there were no parse_name routine: it matches all the words listed in the object's name property. (WordInProperty() is a library function which tests if a single word is listed in a given property of the given object.)

What good is this? None, as it stands; but you can make some interesting variations of it.

An object that sometimes accepts a particular word

This routine adds an extra test. It accepts all the usual words from name, but it additionally accepts the word 'red' if the object has the general attribute.

  
parse_name [ wd num redokay;
  wd = NextWord();
  if (self has general)
    redokay = true;
  while (WordInProperty(wd, self, name)
      || (redokay && wd == 'red')) {
    num++;
    wd = NextWord();
  }
  return num;
],

Note that 'red' would not be listed in the name property.

An object that requires a particular word

You can check for some word, and refuse to accept the phrase at all if it doesn't occur:

  
name 'record' 'album' 'jacket',
parse_name [ wd num gotit;
  wd = NextWord();
  while (WordInProperty(wd, self, name)) {
    if (wd == 'jacket')
      gotit = true;
    num++;
    wd = NextWord();
  }
  if (~~gotit)
    return 0;
  return num;
],

This accepts any of the words 'record', 'album', or 'jacket', but it also ensures that 'jacket' occurs at least once. This object can coexist peacefully with a "record album"; the parser will never be confused about which the player means.

Note that the gotit test is placed at the start of the while loop. This ensures the flag is set whether 'jacket' is returned by the first NextWord() call or one of the following ones.

Also note that 'jacket' is listed in the name property. The word has to be accepted by the usual WordInProperty() test before the gotit test is meaningful.

An object that reacts to another object's name

By just changing the self argument to a different object, you get a routine which reacts to a different object's name list.

  
parse_name [ wd num;
  wd = NextWord();
  while (WordInProperty(wd, SomethingElse, name)) {
    num++;
    wd = NextWord();
  }
  return num;
],

(You would leave off this object's name property entirely, since it would never be consulted.)

This is handy if you need two objects with the same name, but don't want to maintain two identical word lists in different parts of your code. (For example, an NPC who is represented by different objects at different stages of play.)


More Inform Tricks