
Fructose: compile Ruby into PHP - cygwin98
http://www.fructoselang.org/
======
jameskilton
What makes this project even more incredible is the fact that it's written in
C# under .NET 4.0 using IronRuby for the parser.

Yes it says Mono is supported, but why? Windows is the least run environment
for _either_ of these languages (Ruby / PHP). Were I to guess, I'd say this
was a dare that it couldn't be done, and if that's the case, then hats off to
the owner for doing it.

------
aphyr
Upvoting for sheer perversity.

~~~
VladRussian
seeing the PHP monstrosity Ruby has as an equivalent straightens my feelings
of what an ugly language Ruby in it intrinsic nature is

~~~
jessedhillon
You realize that this logic can be applied, in reverse, to make a similar
conclusion about PHP, right? Seeing the monstrous PHP code that equates to
simple Ruby statements confirms my feelings about what a poorly designed
language PHP is. (For example)

~~~
dpkendal
This is an unavoidable part of compilation; there's no way a compiler could
write code as well as a human. It applies to all compilers; a human programmer
writing assembly language would be hard-pressed to understand the output of a
C compiler.

------
mnutt
Somewhat related: One RubyConf, zenspider and tenderlove hacked together Phuby
on Phails, an abomination that allowed you to use PHP within Rails. It even
translated instance variables from Rails controllers to PHP views.

<http://www.youtube.com/watch?v=lsWKjS6Vufw>

------
bonquesha99
Projects like these are awesome learning experiences. I've been working on
something similar (rubyisms in php) but in pure php -
<https://github.com/shuber/phuby>

    
    
      * mixins
      * classes are objects
      * eigenclasses
      * method_missing, respond_to, respond_to_missing, send, super
      * splat
      * extended, included, inherited callbacks
    

Definitely made me appreciate ruby more after working on it.

------
elliottcarlson
Besides being a fun side project and having a chance to play with lexical
parsing - does this have any point?

Converting programming languages really doesn't serve any purpose whatsoever -
this can't be used as a learning tool (a rubyist wanting to learn php) nor
should this output ever be trusted in a production environment as you are
going to count on the converted code being as efficient as your original code.

If you want to code in PHP, use PHP - if you want to code in Ruby, use Ruby -
this just seems silly.

~~~
pkulak
C++ started out as a precompiled language, and I love CoffeeScript, which
compiles to JavaScript. Java and C# don't compile all the way down to assembly
either. There are plenty of reasons to compile one language to another.

~~~
viraptor
Both Java and C# can be compiled to native binaries - there's nothing stopping
that. The standard runtimes for them just don't do that.

Java can be compiled via gcj ("It [GCJ] can compile Java source code to Java
bytecode (class files) or directly to native machine code, and Java bytecode
to native machine code." - <http://gcc.gnu.org/java/>). C# can be compiled
down to native code using various tools. MonoTouch uses that to run C# on
iPhones for example.

------
drm237
The only reason this seems at all useful would be to take advantage of cheap
shared hosting that can only run php. But that's hardly a good reason given
that you can run a ruby (rack-based) app on heroku for free!

~~~
mburney
I've had consulting gigs where I was forced to write PHP apps because of
management decisions. Something like this would have been helpful.

~~~
mdasen
I'm guessing that your clients wouldn't be pleased with the PHP the compiler
produces. It wouldn't really be maintainable PHP code for them.

~~~
Terretta
Just tell them it's _really good_ PHP.

------
stanmancan
Thats a pretty crappy example to put on the homepage: 13 lines of Ruby
compiled into 67 lines of PHP. You could write some PHP more or less identical
to the Ruby in the same original 13 lines.

~~~
fosk
Actually most of the code initializes Fructose, in PHP I can write the same
code like (6 lines, 7 including blank line):

    
    
      class Greeter {
          public static function greet($text) {
              echo $text;
          }
      }
    
      Greeter::greet("Hello, World!\n");

~~~
electrum
That code isn't the same as the original. The instance variable is being
initialized via a block -- your function needs to accept a callback.

------
hardboiled
A call of action to all PHP developers out there: We must defend our language
against this misrepresentation that PHP is ugly and verbose. It is not a fair
comparison of teh power of PHP as it is now. I suggest we start a similar
project called "Sucralose" in which we compile PHP into Ruby 1.8 written in
Objective-C using MacRuby. I will host it on PEAR. All hail Zend!

------
compay
Tip of the cap for the fun hack factor. People asking if this is practical are
missing the point, I think.

~~~
bigiain
+1

I like the implications of the name. Fructose - it's kinda sweet but not quite
real sugar, and there's a suspicion of some deep and not-quite-right
subterfuge going on underneath the entire industry that supports it for no
real reason...

------
mitchellh
Although I'm sure this was a fun weekend project, which is very cool, I can
say that I could actually see something like this being useful IF Ruby could
be compiled to PHP which could then be compiled by HipHop to C++.

But still, fun project to see.

~~~
jonursenbach
How on earth could that be useful? Compiling a piece of Ruby three times down
into C, you're going to end up a with an absolute pile of shit.

~~~
amadeus
Sarcasm is lost on this one :)

~~~
amadeus
He's sitting right across the table from me :)

------
erik_p
has anyone created the opposite? Compile PHP into Ruby?

(Did I just feel all of the HN rubyists shudder at once?)

~~~
StavrosK
Hell, I'm a Pythonista and I shuddered nonetheless.

------
rauljara
Is this some sort of snide commentary on how much more limited of a language
php is? It doesn't seem like it could possibly be meant for production. I
guess I could see it being a fun learning project, tho.

~~~
healthyhippo
I just sent this out to a friend who was trying to figure out whether to build
his simple app in ruby vs. php. Think it made up his mind.

~~~
mfjordvald
That's a pretty stupid reason, though. The two samples of code are no where
near identical...

~~~
jasonlotito
> That's a pretty stupid reason, though.

Yes. So, do you really want him writing PHP?

~~~
eropple
I don't want him writing Ruby, either. Keep him away from the sharp knives and
stupid decisions and give him Java/Play or something.

------
TamDenholm
Is there anything that can do the reverse?

------
mduvall
I'm curious - would it be inefficient to use something that could efficiently
compile Ruby into PHP such as Fructose, and then use HipHop for PHP from
Facebook to compile the PHP into C++? So far I don't see a lot of tools that
utilize the Ruby->C++ direction, and much work has gone into HipHop for PHP.

~~~
lautis
It won't work.

First, HipHop doesn't support create_function, which is used in the example.

Second, feature mismatch between Ruby and PHP will make efficient compilation
quite hard and most likely be far from optimal. The languages doesn't seem to
be very easily optimizable either.

HipHop isn't a silver bullet either: sometimes it seems to be faster than
cached PHP bytecode, but not always.

------
ashchristopher
<https://github.com/charliesome/Fructose/pull/9>

Active community participation too =).

------
alexg0
Would be cool if it was self-hosted, written in Ruby, but ran in complied to
PHP form.

