使用 C API 编译 Ruby
如果我
puts "Hello World!"
使用Ruby的C API重写
#include "ruby.h"
int main() {
ruby_init();
rb_funcall(Qnil, rb_intern("puts"), 1, rb_str_new2("Hello World!"));
ruby_finalize();
return 0;
}
一段Ruby代码并编译它,这是一种编译Ruby代码的方法吗?
如果我创建一个使用 Ripper 解析 Ruby 代码并将其重写为 C 的程序,我可以将其称为“Ruby 编译器”吗?有一些 Ruby 代码不能用这种方式用 Ruby 重写吗?以前有人尝试过编写这种“编译器”吗?
If I take a ruby code
puts "Hello World!"
and rewrite it using the C API of Ruby
#include "ruby.h"
int main() {
ruby_init();
rb_funcall(Qnil, rb_intern("puts"), 1, rb_str_new2("Hello World!"));
ruby_finalize();
return 0;
}
and compile it, is this a way to compile Ruby code?
If I create a program that uses Ripper
to parse the Ruby code and rewrite it as C, can I call it as a "Ruby compiler"? There're some ruby code that can't be rewrited in Ruby in this way? Did someone tried to write this kind of "compiler" before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于此主题的几篇好文章:
您还听说过 Crystal 吗?虽然不是真正的 Ruby,但它看起来很有趣:
关于它:有人尝试过 Crystal 编程语言(机器代码编译的 Ruby)吗?
另一个具有相同目的但主要针对嵌入式开发的(商业)项目:http://foundry-lang.org/
A couple of good articles on this topic:
Also have you heard of Crystal? While not truly Ruby, it looks interesting:
about it on SO: Anybody tried the Crystal Programming Language (machine-code compiled Ruby)?
And another (commercial) project with the same purposes but mainly targeted at embedded development: http://foundry-lang.org/
是的,这就是“c 化”的 ruby 代码。
最接近“ruby to c”的是 http://ruby2cext.rubyforge.org,rubinius 及其 JIT编译器和 ruby2c
http://betterlogic.com/roger /2009/08/how-to-use-the-ruby2c-gem
另一种选择是为 1.9 的字节码编写 JIT 编译器,这可能会加快速度。
另请参阅 mirah 语言,它类似于静态、编译时 ruby。
理论上应该是可以的。
yes that is "c-ified" ruby code, as it were.
The closest things to "ruby to c" have been http://ruby2cext.rubyforge.org, rubinius with its JIT compiler, and ruby2c
http://betterlogic.com/roger/2009/08/how-to-use-the-ruby2c-gem
Another option would be to write a JIT compiler for 1.9's bytecode, that might speed things up a bit.
Also see the mirah language, which is like static, compile time ruby.
Theoretically it should be possible.