Interpreter Tree Search (commands) [by Angus]
Snippet Posted Wednesday, August 12th @ 11:29:23 PM, by George Greer in the Utils dept.
Added Dec 8, 1997. Click the link below to read it or download it.

From: angus@EDGIL.CCMAIL.COMPUSERVE.COM
Subject: Interpreter tree search

Here is a correct version:

/*
       <stuff from the has discussion>
       Quote ------>
       n > o > r > t > h
       V
       e > a > s > t
       V
       s > o > u > t > h
       V
       w > e > s > t
       V
       a > t
       V   V
       V   d > v > a > n > c > e
       V   V
       V   l > i > a > s
       V   V
       V   s > s > i > t
       V   V   V
       V   V   k
       V   V
       V   u > t > o > e > x > i > t
       V
       e > a > t
       <------Quote

       n (index letter) -> (next in word ptr) -> o (index letter)
       |
       V
       (next in list ptr)
       |
       V
       e (index letter)

       so, what type of data structure would you us for this type of thing?
       something like this:?
       */
struct xCommandList
{
   char   cIndexLetter;   /* (a,b,c,d) the actual letter you are checking */
   struct xCommandList *pxNextInWord; /* the next letter in this command */
   struct xCommandList *pxNextInList; /* the next command with this letter */
   struct xCommandStruct *pxThisCommand; /*the struct for this command */
};

struct xCommandStruct
{
   char *pztCommand;
   byte iMinPosition;
   void (*CommandPointer) (struct char_data *ch, char * argument, int cmd,
            int subcmd);
   sh_int iMinLevel;
   int iSubCmd;
};




pxTempCmdLst=pxHeadOfList; /*a global pointer to the top of the tree */
iPosition =0;
CheckCommand("test",NULL, pxTempCmdList, iPosition);



/* this is  where we check the command against the struct, we have already
   checked the SPEC */
int CheckCommand(char *pztCommand,struct xCommandStruct *pxCommand,
       struct xCommandList *pxCmdLst, int iPosition)
{
   assert(pztCommand);
   assert(pztCommand[0]!='\0');



   /* end of this chain, no match */
   if(!pxCmdLst)
      return(FALSE);

   /* if item matches */
   if(pztCommand[iPosition]==pxCmdList->cIndexLetter)
      {
      /* end of command string, we match */
      if(pztCommand[iPosition+1]=='\0')
          {
          pxCommand=pxCmdLst->pxThisCommand;
          return(TRUE);
          }

      /* check the next letter */
      if(pxCmdLst->pxNextInWord!=NULL)/* for the 'qui' 'quit' scenario */
          {
          if(CheckCommand(pztCommand,pxCommand,pxCmdLst->pxNextInWord,
                iPosition++)==TRUE)
             {
             return(TRUE);
             }
       else
          {
          return(FALSE);
          }
       }
      else
       {
       if(CheckCommand(pztCommand,pxCommand,pxCmdLst->pxNextInList,
          iPosition++)==TRUE)
          {
          return(TRUE);
          }
       else
          {
          return(FALSE);
          }
       }
      }/* END: if(pztCommand[iPosition]==pxCmdList.cIndexLetter) */


   /* else check the next item in the list */
   if(CheckCommand(pztCommand,pxCommand,pxCmdLst->pxNextInList,
         iPosition++)==TRUE)
      {
      return(TRUE);
      }
   else
      {
      return(FALSE);
      }

   return(ERROR); /* SHOULD NEVER GET HERE */
}

/*
       Should be able to build this list by reading in to cmd_info array and,
       using the array's order, construct the pointers.  Probably some sort of
       recursive build.  You will want to strip out all relation between cmd
       number and the dirs array. Use the SCMD_ instead for this.  It is bad
       form to use the same variable for multiple purposes.  Just look at all
       the problems getc causes by returning an int for errors, and a char for
       success.  I personally prefer sending results through pointers, and
       errors through return values.  Mileage may vary.
       */



<< Interposing Hand [by JTP94] | Reply | View as text | Threaded | Invisible Object Special [by Mike Carpenter] >>

 


Related Links
  download
Related Articles
More by greerga
 
 

CircleMUD Snippets
 
Note: Not all of these snippets will work perfectly with your version of code, so be prepared to fix one or two bugs that may arise, and please let me know what you needed to do to fix it. Sending a corrected version is always welcome.
Finally, if you wish to use any of the snippets from this page, you are more than welcome, just mention the authors in your credits. If you wish to release any of these snippets to the public on another site, contact me FIRST.