使用C API编译Ruby

时间:2021-10-10 23:42:56

If I take a ruby code

如果我采用ruby代码

puts "Hello World!"

and rewrite it using the C API of Ruby

并使用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;
}

and compile it, is this a way to compile Ruby code?

并编译它,这是一种编译Ruby代码的方法吗?

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?

如果我创建一个使用Ripper来解析Ruby代码并将其重写为C的程序,我可以将其称为“Ruby编译器”吗?有一些ruby代码不能以这种方式在Ruby中重写吗?有人曾经尝试过编写这种“编译器”吗?

2 个解决方案

#1


4  

A couple of good articles on this topic:

关于这个主题的几篇好文章:

Also have you heard of Crystal? While not truly Ruby, it looks interesting:

还有你听说过水晶吗?虽然不是真正的Ruby,但它看起来很有趣:

Crystal is a programming language with the following goals:

Crystal是一种编程语言,具有以下目标:

  • Have the same syntax as Ruby, or at least as similar as possible.
  • 使用与Ruby相同的语法,或至少尽可能相似。

  • Never have to specify the type of a variable or method argument.
  • 永远不必指定变量或方法参数的类型。

  • Be able to call C code by writing bindings to it in Crystal.
  • 能够通过在Crystal中编写绑定来调用C代码。

  • Have compile-time evaluation and generation of code, to avoid boilerplate code.
  • 有编译时评估和代码生成,以避免样板代码。

  • Compile to efficient native code.
  • 编译为高效的本机代码。

about it on SO: Anybody tried the Crystal Programming Language (machine-code compiled Ruby)?

关于SO:有人试过Crystal编程语言(机器码编译Ruby)吗?

And another (commercial) project with the same purposes but mainly targeted at embedded development: http://foundry-lang.org/

另一个(商业)项目具有相同的目的,但主要针对嵌入式开发:http://foundry-lang.org/

#2


1  

yes that is "c-ified" ruby code, as it were.

是的,这是“c-ified”ruby代码,就像它一样。

The closest things to "ruby to c" have been http://ruby2cext.rubyforge.org, rubinius with its JIT compiler, and ruby2c

最接近“ruby to c”的内容是http://ruby2cext.rubyforge.org,rubinius及其JIT编译器和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.

另一个选择是为1.9的字节码编写一个JIT编译器,这可能会加快速度。

Also see the mirah language, which is like static, compile time ruby.

另请参阅mirah语言,这就像静态,编译时间ruby。

Theoretically it should be possible.

理论上它应该是可能的。

#1


4  

A couple of good articles on this topic:

关于这个主题的几篇好文章:

Also have you heard of Crystal? While not truly Ruby, it looks interesting:

还有你听说过水晶吗?虽然不是真正的Ruby,但它看起来很有趣:

Crystal is a programming language with the following goals:

Crystal是一种编程语言,具有以下目标:

  • Have the same syntax as Ruby, or at least as similar as possible.
  • 使用与Ruby相同的语法,或至少尽可能相似。

  • Never have to specify the type of a variable or method argument.
  • 永远不必指定变量或方法参数的类型。

  • Be able to call C code by writing bindings to it in Crystal.
  • 能够通过在Crystal中编写绑定来调用C代码。

  • Have compile-time evaluation and generation of code, to avoid boilerplate code.
  • 有编译时评估和代码生成,以避免样板代码。

  • Compile to efficient native code.
  • 编译为高效的本机代码。

about it on SO: Anybody tried the Crystal Programming Language (machine-code compiled Ruby)?

关于SO:有人试过Crystal编程语言(机器码编译Ruby)吗?

And another (commercial) project with the same purposes but mainly targeted at embedded development: http://foundry-lang.org/

另一个(商业)项目具有相同的目的,但主要针对嵌入式开发:http://foundry-lang.org/

#2


1  

yes that is "c-ified" ruby code, as it were.

是的,这是“c-ified”ruby代码,就像它一样。

The closest things to "ruby to c" have been http://ruby2cext.rubyforge.org, rubinius with its JIT compiler, and ruby2c

最接近“ruby to c”的内容是http://ruby2cext.rubyforge.org,rubinius及其JIT编译器和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.

另一个选择是为1.9的字节码编写一个JIT编译器,这可能会加快速度。

Also see the mirah language, which is like static, compile time ruby.

另请参阅mirah语言,这就像静态,编译时间ruby。

Theoretically it should be possible.

理论上它应该是可能的。