From: Brian Candler Date: 2008-10-30T18:48:11+09:00 Subject: Re: automatic code conversion from Ruby to C ? Axel Etzold wrote: > I am not looking for swig or an extension of Ruby by C, but a way of > producing > C/C++ code from a Ruby script containing some magical part that saves > me the trouble of declaring all the variables, assigning memory etc and > all > these things that make writing C/C++ so tedious in comparison with Ruby That's one thing that makes Ruby fundamentally different from C/C++. It has its own big library of useful data structures, and its own typing system. Ruby's highly dynamic typing and method dispatch makes it poorly suited to automatic code analysis (e.g. type inferencing) which you could use with, say, Haskell. Real-life Ruby programs make heavy use of this. Look at ActiveRecord; it adds all sorts of methods dynamically to a class, depending on the structure of the database at the time the program is run. What would the generated C code for this look like? Talking about Sexps ignores the distinction between syntax and semantics. Of course it's easy to get to s(:block, s(:lasgn, :str, s(:str, "hello")), s(:call, s(:lvar, :str), :upcase!, s(:arglist))) But turning that into idiomatic C begs fundamental questions like "what's a String"? (Remember that Ruby strings are 8-bit clean, i.e. can contain nulls, and are dynamically sized) Once you decide that strlen, strcat and friends are unsuitable for the general case, then you have to make a new data structure for strings, and implement it in C. Then you move onto Array where the problem gets worse: in many cases Ruby arrays are heterogeneous collections, so your generated C code would have to be able to hold objects of any Ruby type. Sooner or later it becomes an array of VALUE pointers, which means you are using the Ruby core library anyway. -- Posted via http://www.ruby-forum.com/.