这段 PHP 代码的目的是什么?

发布于 2024-10-17 11:04:14 字数 607 浏览 1 评论 0原文

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

它有什么作用?

位于此处

更新

我的问题是,既然

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

总是评估为

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

那么为什么要多一行呢?

        self::$currentend = $cfp;
        self::$currentend = &$cfp->next;
        self::$basisend = $cfp;
        self::$basisend = &$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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

胡大本事 2024-10-24 11:04:14

您的 PHP 代码是 LEMONPHP 端口> 解析器生成器,其中包含以下代码:

/* 添加另一个配置到配置列表 */
结构配置 *Configlist_add(rp,点)
结构规则*rp; /* 规则 */
整数点; /* 点所在规则的 RHS 索引 */
{
  结构配置*cfp,模型;

  断言( currentend!=0 );
  模型.rp = rp;
  模型.dot = 点;
  cfp = Configtable_find(&模型);
  如果(cfp==0){
    cfp = newconfig();
    cfp->rp = rp;
    cfp->点=点;
    cfp->fws = SetNew();
    cfp->stp=0;
    cfp->fplp=cfp->bplp=0;
    cfp->下一个=0;
    cfp->bp=0;
    *当前结束= cfp;
    currentend = &cfp->下一个;
    可配置_插入(cfp);
  }
  返回cfp;
}

它在 PHP 中,因为它在原始 C 中。在原始 C 中,它通过 currentend 指针写入来替换它指向的任何内容(在其他地方分配的内存,可能包含垃圾),然后它更新currentend指针以指向cfp->next指向的struct节点(即0< /code>,这里,这就是为什么我认为其他例程稍后会为其分配内存的原因)。

换句话说,它将新的结构规则附加到结构规则列表的末尾,同时维护指向“最后一个条目”的指针。 (好吧,是超出末尾的条目。一旦存在,下一个最后条目将去往何处。所有这些都使得访问列表末尾成为 O(1) 操作,对于改进列表追加操作非常有用。 )

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.)

满天都是小星星 2024-10-24 11:04:14

我不知道那是什么,看起来很奇怪,但我可以解释:

self:: 指的是当前对象/类的类。

$currentend & $basisend 是存储变量名称的变量 - 也就是说,如果代码是这样的:

$currentend = bla1;
$currentend = bla2;

那么它本质上计算为:

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

所以无论 $currentend & 后面的值是什么$basisend,它们引用当前类中的静态变量。

的&是一个引用运算符。它基本上意味着您不想复制变量,而是“共享”其他两个变量引用的变量。实际上,为变量分配一个指针。

除此之外,我不知道那是什么或目的是什么。但看起来很有趣。

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.

も星光 2024-10-24 11:04:14

正如上面每个人所说,代码并不完整,但它看起来很像 PHP_ParserGenerator 的 Pear 配置。

  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;
}

我怀疑如果您进一步查看代码,您会发现对此类似内容的引用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文