Arc Forumnew | comments | leaders | submitlogin
Languages that compile to PHP (stackoverflow.com)
2 points by rocketnia 4262 days ago | 1 comment


2 points by rocketnia 4262 days ago | link

What fate befall ye, StackOverflow netizens? How do you think in such backward ways?

---

At one time or another, I've found myself stuck with PHP. It's the most hassle-free server-side platform on my cheap hosting service, I worked in PHP for my college's website, and I've helped family members muck around with WordPress.

The last time I tried to use PHP, I realized its semantics were extremely surprising--at least to someone like me who likes the scoping rules of JavaScript and Scheme. But more recently than that, I realized these semantic surprises weren't utter dealbreakers:

- Assigning to an array element may be sugar for assigning a whole new array to the variable, and the most convenient syntax for function arguments may cause them to be deeply copied, but PHP's classical object system corrects these flaws somewhat.

- PHP's anonymous function syntax may force you to declare the function's closed-over variables, but hey, at least it's an anonymous function syntax, and at least this way it can plausibly avoid capturing the entire lexical scope. (Since PHP supports eval in local scopes, it's hard to determine free variables in the general case. That hasn't stopped JavaScript implementations from optimizing the eval-free case when possible, but still.)

I wouldn't shy away from coding in PHP again if I had to, but I'd much rather compile to it. The right source language could compile all my data structures to objects, and it could infer all my anonymous functions' lexical closures.

---

The StackOverflow link I posted doesn't help much--I just found it amusing--but there are a few helpful options:

- Snowscript (https://github.com/runekaagaard/snowscript) is a CoffeeScript-like syntax sugar layer for PHP. It doesn't address any of my semantic problems with PHP, but it's something. (The OP of the StackOverflow thread linked to Snowscript in a comment.)

- Fructose (https://github.com/charliesome/Fructose) is a language which approximates Ruby and compiles to PHP. The main website is down, the GitHub project hasn't been updated for a year, and I don't see much documentation. But wait! The compiler code does manipulate lexical closures in some way, and the runtime library has object wrappers for arrays and other primitives, so I expect it to be pretty nice to work with.

- Pharen (http://scriptor.github.com/pharen/reference.html) is a lisp which compiles to PHP. Probably the most promising of all of these, not only does it have implicit lexical closures and PHP-object-system-related features, but it also has macros, and the language reference explicitly talks about tail call elimination.[1] And hey, the compiler is written in PHP itself, so it should be possible to import the compiler and do eval at run time (whether or not Pharen is set up to do that out of the box).

[1] Pharen probably doesn't support TCE across multiple procedures, since it seems Pharen procedures can be passed directly to existing PHP utilities, which indicates they're implemented as (TCE-unfriendly) PHP procedures.

-----