Writing ruby to [LANGUAGE] "compiler"

Hi.

Say I write something in Ruby. Then I want to translate this code
into C.

I do not aim for 100% translation. 60% is sufficiently well.

For instance, think you are using only classes, and "def method"
in those classes. No metaprogramming, no blocks etc...

You use only a small subset of Ruby.

And now you want to translate this into C. (Pure C, not Ruby-C).

I can do this by hand (if given enough time), but I want to reduce
this amount spent.

The language C is just an example. You can insert any-other language
into this.

My question is - how to approach this goal?

My most simple way would be via using readlines of the .rb file
and then analyzing it. And then inserting what seems appropriate.

But I could do this without the use of a scanner.

Do I have to use a scanner though and tokenize the stream?

···

--
Posted via http://www.ruby-forum.com/.

Use a parser. There is ruby_parse gem, just perfect for the occasion.

I've once tried to create something similar:
https://github.com/MatmaRex/crude - examples included.

-- Matma Rex

see
1) ruby2c
2) ruby2cextension

···

On 12/17/2011 8:31 AM, Marc Heiler wrote:

Hi.

Say I write something in Ruby. Then I want to translate this code
into C.

I do not aim for 100% translation. 60% is sufficiently well.

For instance, think you are using only classes, and "def method"
in those classes. No metaprogramming, no blocks etc...

You use only a small subset of Ruby.

And now you want to translate this into C. (Pure C, not Ruby-C).

I can do this by hand (if given enough time), but I want to reduce
this amount spent.

The language C is just an example. You can insert any-other language
into this.

My question is - how to approach this goal?

My most simple way would be via using readlines of the .rb file
and then analyzing it. And then inserting what seems appropriate.

But I could do this without the use of a scanner.

Do I have to use a scanner though and tokenize the stream?

also see
3) RubyToC

···

On 12/17/2011 8:31 AM, Marc Heiler wrote:

Hi.

Say I write something in Ruby. Then I want to translate this code
into C.

I do not aim for 100% translation. 60% is sufficiently well.

For instance, think you are using only classes, and "def method"
in those classes. No metaprogramming, no blocks etc...

You use only a small subset of Ruby.

And now you want to translate this into C. (Pure C, not Ruby-C).

I can do this by hand (if given enough time), but I want to reduce
this amount spent.

The language C is just an example. You can insert any-other language
into this.

My question is - how to approach this goal?

My most simple way would be via using readlines of the .rb file
and then analyzing it. And then inserting what seems appropriate.

But I could do this without the use of a scanner.

Do I have to use a scanner though and tokenize the stream?

You'd probably need to build a lexer/parser that can understand this subset
of C. I've never successfully built a parser, it's hard, but check out gems
like Treetop and Citrus. They should allow you to build an abstract syntax
tree that represents the code. Then you'll probably have to analyze the AST
to do some sort of type determining (ie if I pass it a string, then I know
its argument has type of string) check out
Hindley–Milner type system - Wikipedia for that purpose. Once
you've got all of that, just write it back out, only when writing it out,
use the C syntax instead of the Ruby syntax.

Other thoughts:
* Racc might also be useful, but looked pretty formidable to me when I
tried to play with it.
* There is an ebook
http://gilesbowkett.blogspot.com/2010/07/review-create-your-own-freaking-awesome.htmlthat
might help. I wasn't able to figure out how to do anything more
complex than the provided example, though.
* This (http://www.bayfronttechnologies.com/mc_tutorial.html\) also looked
promising to me, it was recommended in a talk by Alan Kay, but I haven't
tried to go through it yet.

···

On Sat, Dec 17, 2011 at 7:31 AM, Marc Heiler <shevegen@linuxmail.org> wrote:

Hi.

Say I write something in Ruby. Then I want to translate this code
into C.

I do not aim for 100% translation. 60% is sufficiently well.

For instance, think you are using only classes, and "def method"
in those classes. No metaprogramming, no blocks etc...

You use only a small subset of Ruby.

And now you want to translate this into C. (Pure C, not Ruby-C).

I can do this by hand (if given enough time), but I want to reduce
this amount spent.

The language C is just an example. You can insert any-other language
into this.

My question is - how to approach this goal?

My most simple way would be via using readlines of the .rb file
and then analyzing it. And then inserting what seems appropriate.

But I could do this without the use of a scanner.

Do I have to use a scanner though and tokenize the stream?

--
Posted via http://www.ruby-forum.com/\.

4) cast_off

···

On Sat, Dec 17, 2011 at 6:35 PM, Reid Thompson <reid.thompson@ateb.com> wrote:

On 12/17/2011 8:31 AM, Marc Heiler wrote:

Hi.

Say I write something in Ruby. Then I want to translate this code
into C.

I do not aim for 100% translation. 60% is sufficiently well.

For instance, think you are using only classes, and "def method"
in those classes. No metaprogramming, no blocks etc...

You use only a small subset of Ruby.

And now you want to translate this into C. (Pure C, not Ruby-C).

I can do this by hand (if given enough time), but I want to reduce
this amount spent.

The language C is just an example. You can insert any-other language
into this.

My question is - how to approach this goal?

My most simple way would be via using readlines of the .rb file
and then analyzing it. And then inserting what seems appropriate.

But I could do this without the use of a scanner.

Do I have to use a scanner though and tokenize the stream?

also see
3) RubyToC