如何将 CV 分配给包含操作码列表的包?
我一直在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会看看 eval 内部做了什么,因为这本质上就是它所做的。特别是,op.c 和 Perl_newPROG( OP *o) 可能是一个很好的起点:
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: