From: Robert Klemme Date: 2008-10-28T06:29:41+09:00 Subject: Re: automatic code conversion from Ruby to C ? On 27.10.2008 20:44, Axel Etzold wrote: > I am looking for a (semi-)automatic conversion of a Ruby script to C or C++, so > that readable source code for a human C/C++ programmer is produced. Hm, automatic conversion tends to produce not so readable code. > The program should read in text from a file as a string, split and search it using regular expressions, > convert some numbers into floats and perform some calculations on them > (the latter part is done using some existing C code). > > The text file to be read has this structure: > > -------------------- > P1 > P2 23 > P4 27 > -------------------- > P2 > P3 1 > P5 457 > P6 3 > P377 56 > -------------------- > > Thus, P1 is a starting point and the distance to point P2 is 23 etc. > There is a varying number of entries in each of the parts separated by > dashes. > Is there some gem that would allow me to write a nice short > Ruby script doing the above-mentioned manipulations and translate it for me into > readable C/C++, such that I'd have to specify as few as possible size and variable > type declarations ? So you basically want a script like this converted: graph = [] p = nil ARGF.each do |line| line.chomp! case line when /^-+$/ p = nil when /^P(\d+)$/ p = $1.to_i graph[p] = {} when /^P(\d+)\s+(\d+)$/ graph[p][$1.to_i] = $2.to_i else raise "Cannot parse: #{line.inspect}" end end This should be similar easy with regular expressions in C/C++ or even parsing by hand. > I have about a hundred thousand points and relative distances, so speed could matter here ... > and I'd have a hard time defending the use of a scripting language with the people > I am collaborating here. What do you want to do with the data afterwards? Maybe you can just add the missing code and benchmark (your writing time as well as runtime). Kind regards robert