开发者

What is the purpose of this PHP code?

        self::$currentend = $cfp;
        self::$currentend = &$cfp->next;
        self::$basisend = $cfp;
        self::$b开发者_如何学Goasisend = &$cfp->bp;

What does it do?

Found here.

UPDATE

My question is since

        self::$currentend = $cfp;
        self::$currentend = &$cfp->next;

always evaluates to

        self::$currentend = &$cfp->next;

So why the extra line?


Your PHP code is a C->PHP port of the LEMON parser generator, which includes this code:

/* Add another configuration to the configuration list */
struct config *Configlist_add(rp,dot)
struct rule *rp;    /* The rule */
int dot;            /* Index into the RHS of the rule where the dot goes */
{
  struct config *cfp, model;

  assert( currentend!=0 );
  model.rp = rp;
  model.dot = dot;
  cfp = Configtable_find(&model);
  if( cfp==0 ){
    cfp = newconfig();
    cfp->rp = rp;
    cfp->dot = dot;
    cfp->fws = SetNew();
    cfp->stp = 0;
    cfp->fplp = cfp->bplp = 0;
    cfp->next = 0;
    cfp->bp = 0;
    *currentend = cfp;
    currentend = &cfp->next;
    Configtable_insert(cfp);
  }
  return cfp;
}

It's in the PHP because it was in the original C. In the original C, it writes through the currentend pointer to replace the contents of whatever it is pointing at (memory allocated elsewhere, probably contains garbage), and then it updates the currentend pointer to point to the struct node pointed to by cfp->next (which is 0, here, which is why I think some other routine will allocate the memory for it later).

In other words, it appends the new struct rule to the end of a list of struct rules while maintaining a pointer to the "last entry". (Well, an entry beyond the end. Where the next last-entry will go, once it exists. All of which makes accessing the end-of-the-list an O(1) operation, superb for improving list-append operations.)


I have no idea what that is from, it looks weird, but I can explain:

self:: refers to the class of the current object/class.

$currentend & $basisend are variables storing variable names - that is, if the code were like this:

$currentend = bla1;
$currentend = bla2;

then it essentially evaluates to:

self::bla1 = $cfp;
self::bla1 =& $cfp->next;
self::bla2 = $cfp;
self::bla2 =& $cfp->bp;

So whatever the value behind $currentend & $basisend, they are refering to static variables within the current class.

The & is a reference operator. It basically means that you do not want to copy the variable, but "share" the variable referenced by both of the other variables. Actually, to assign a pointer to the variable.

Other than that, I have no idea from what that is or what the purpose is. But it looks funny.


The code is incomplete as everyone has stated above, however it looks suspiciously like the Pear Config for PHP_ParserGenerator.

  static function Configlist_add($rp, $dot)
{
    $model = new PHP_ParserGenerator_Config;
    $model->rp = $rp;
    $model->dot = $dot;
    $cfp = self::Configtable_find($model);
    if ($cfp === 0) {
        $cfp = self::newconfig();
        $cfp->rp = $rp;
        $cfp->dot = $dot;
        $cfp->fws = array();
        $cfp->stp = 0;
        $cfp->fplp = $cfp->bplp = 0;
        $cfp->next = 0;
        $cfp->bp = 0;
        self::$currentend = $cfp;
        self::$currentend = &$cfp->next;
        self::Configtable_insert($cfp);
    }
    return $cfp;
}

I would suspect if you look further into the code you will find a reference to this of something similar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜