如何将 CV 分配给包含操作码列表的包?

发布于 2024-10-09 17:03:40 字数 1264 浏览 3 评论 0原文

我一直在使用 bleadperl 中的新 parse_block 功能,

我可以将多个语句解析为一个 listop,这将生成下面的树:

LISTOP (0x1002a00c0) leave [1] 
    OP (0x1002d6220) enter 
    COP (0x1002a0b80) nextstate 
    OP (0x10028c0f0) null 
    LISTOP (0x1002a0170) print 
        OP (0x1002b1a90) pushmark 
        SVOP (0x100327ee0) const  PV (0x100826ec0) "hello\n" 
    COP (0x1002a0c50) nextstate 
    LISTOP (0x100324ee0) print 
        OP (0x100327880) pushmark 
        SVOP (0x100324eb0) const  PV (0x100897688) "world\n"

我需要从我的关键字插件返回一个指向 optree 结构的指针,该结构目前只包含简单的操作列表。我想将这些操作包装在子例程中,并将其分配给存储中的符号。

所以我想我想做这样的事情:

$ perl  -MO=Terse -e "*foo = sub { print 'my listops here' }"

LISTOP (0x10022b5e0) leave [1] 
    OP (0x10022b620) enter 
    COP (0x10022b590) nextstate 
    BINOP (0x100202090) sassign 
        UNOP (0x1002083d0) refgen 
            UNOP (0x100208360) null [146] 
                OP (0x1002083a0) pushmark 
                SVOP (0x100208330) anoncode [1] CV (0x100826d40) 
        UNOP (0x1002085a0) rv2gv 
            SVOP (0x100208550) gv  GV (0x100826d28) *foo 

大概我需要在列表的开头和结尾添加entersub、leavesub,但我不确定如何在XS中构建它?我也不知道如何将生成的 opttree 转换为 CV?

我可以找到为 xsub 生成简历的示例,但不能从 opttree 找到。

感谢您的帮助。

I have been playing with the new parse_block feature in bleadperl,

I can parse several statements into a listop, which would generate the tree below:

LISTOP (0x1002a00c0) leave [1] 
    OP (0x1002d6220) enter 
    COP (0x1002a0b80) nextstate 
    OP (0x10028c0f0) null 
    LISTOP (0x1002a0170) print 
        OP (0x1002b1a90) pushmark 
        SVOP (0x100327ee0) const  PV (0x100826ec0) "hello\n" 
    COP (0x1002a0c50) nextstate 
    LISTOP (0x100324ee0) print 
        OP (0x100327880) pushmark 
        SVOP (0x100324eb0) const  PV (0x100897688) "world\n"

I need to return a pointer to an optree structure from my keyword plugin, which at the moment just contains the bare list of ops. I want to wrap these ops inside a subroutine and assign it to a symbol within a stash.

So I guess I want to do something like this:

$ perl  -MO=Terse -e "*foo = sub { print 'my listops here' }"

LISTOP (0x10022b5e0) leave [1] 
    OP (0x10022b620) enter 
    COP (0x10022b590) nextstate 
    BINOP (0x100202090) sassign 
        UNOP (0x1002083d0) refgen 
            UNOP (0x100208360) null [146] 
                OP (0x1002083a0) pushmark 
                SVOP (0x100208330) anoncode [1] CV (0x100826d40) 
        UNOP (0x1002085a0) rv2gv 
            SVOP (0x100208550) gv  GV (0x100826d28) *foo 

Presumably I need to add entersub, leavesub at the beginning and end of my listops but I am not sure how I would go about constructing this in XS? Nor do I know how to turn the resulting optree in to a CV?

I can find examples of generating a CV for xsubs but not from optrees.

Thanks for your help.

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

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

发布评论

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

评论(1

脸赞 2024-10-16 17:03:40

我会看看 eval 内部做了什么,因为这本质上就是它所做的。特别是,op.c 和 Perl_newPROG( OP *o) 可能是一个很好的起点:

void
Perl_newPROG(pTHX_ OP *o)
{
    ...
    PL_eval_root = newUNOP(OP_LEAVEEVAL,
                   ((PL_in_eval & EVAL_KEEPERR)
                ? OPf_SPECIAL : 0), o);
    PL_eval_start = op_linklist(PL_eval_root);
    PL_eval_root->op_private |= OPpREFCOUNTED;
    OpREFCNT_set(PL_eval_root, 1);
    PL_eval_root->op_next = 0;
    CALL_PEEP(PL_eval_start);
    ...

I'd have a look at what eval does internally, as this is essentially what it does. In particular, op.c and Perl_newPROG( OP *o) might be a good place to start:

void
Perl_newPROG(pTHX_ OP *o)
{
    ...
    PL_eval_root = newUNOP(OP_LEAVEEVAL,
                   ((PL_in_eval & EVAL_KEEPERR)
                ? OPf_SPECIAL : 0), o);
    PL_eval_start = op_linklist(PL_eval_root);
    PL_eval_root->op_private |= OPpREFCOUNTED;
    OpREFCNT_set(PL_eval_root, 1);
    PL_eval_root->op_next = 0;
    CALL_PEEP(PL_eval_start);
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文