简体   繁体   English

如何从C级代码访问Ruby AST?

[英]How do I access the Ruby AST from C level code?

I understand that the Ruby 1.8 AST is traversed at runtime using a big switch statement, and many things like calling a method in a class or parent module involve the interpreter looking up and down the tree as it goes. 我了解到Ruby 1.8 AST是在运行时使用大的switch语句遍历的,并且诸如在类或父模块中调用方法之类的许多事情都涉及到解释器在树的运行过程中上下移动。 Is there a straightforward way of accessing this AST in a Ruby C extension? 是否有直接的方法可以在Ruby C扩展中访问此AST Does it involve the Ruby extension API, or necessitate hacking the internal data structures directly? 它涉及Ruby扩展API,还是必须直接入侵内部数据结构?

一个不错的起点可能是阅读ParseTree库的源代码,该库可以让您了解并使用ruby的AST。

Thanks for the tip. 谢谢你的提示。 You're right - ParseTree seems to be the only code out there with any manipulation of the AST going on, except that it's actually written in RubyInline. 没错ParseTree似乎是唯一可以对AST进行任何操作的代码,除了它实际上是用RubyInline编写的。

So, it's a strange mixture between Ruby and C code. 因此,这是Ruby和C代码之间的奇怪混合。 Very interesting reading, though. 不过,这很有趣。

The other reference of course is eval.c from Ruby itself. 当然,另一个参考是Ruby本身的eval.c。

It's going to take a fair bit of reading of both, to get my head around it. 阅读这两者都需要花点时间,才能让我有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM