It starts by tokenizing the PHP code into tokens, which it then uses to build an AST tree. Once the tree has been constructed, the script compiles it into JavaScript that can be interpreted by the VM and then executes it.
What you've done there is you've explained the frontend processing that every compiler does, (tokenizing, building an AST) and missed out the part that is entirely specific to this particular one.
While most compilers today build an AST, it's by no means true that all do. Some very influential compilers through history explicitly does not.
E.g. Niklaus Wirth's languages, such as Pascal and Oberon are explicitly designed so that they can easily be compiled with single pass compilers that call the code generator to emit code from the parser instead of going via an AST, because he as more concerned with small easily understandable compilers than complex optimizations.
It's not nearly as common as it was, but it was a fairly widespread practice in compilers for languages where it is possible back when memory and disk space was still in short supply.
It's also not true that all compilers tokenize by any meaningful interpretation. While e.g. Wirth's Pascal-P compiler arguably tokenize, compact recursive descent compilers for languages with as small grammar as Wirth's languages can and often do blur the lines significantly, or have no tokenization as a separate step from the parser at all.
Also, the 'T' in "AST" stands for tree. Not all intermediate-represenatations are trees: some are linear, like stack and register operations, others are graphs, etc.
But you were right, that description is .. PHPish.
The AST is typically the first generated intermediate representation and by definition it relates to the textual form of the language. All textual forms can be viewed as trees.
I can think of a couple of practical uses, the biggest one that comes to mind is helping places like CodeSchool do in browser PHP tutorials with the code actually executing. So definite practical uses :)
Assuming you have strong convictions about using php instead of JavaScript, it could be very useful.
I'm actually using a similar tool (js_of_ocaml [1]) at work. It's great: we can take our product and package it for node or let people use it in the browser directly. And we can also have a native compiled version if performance is needed.
So if you have an almost religiously php-oriented shop working on something that isn't a web app, I could certainly see this being useful. That's exactly what the company I'm at is, except with OCaml instead of php. Of course, I can't actually imagine any company like that in php :P. (How many people use php for something that isn't server-side code?)
But the core idea is useful and awesome. And it's certainly very cool for a personal side-project!
Actually, I stumbled on an article recently noting that you can use the V8 pecl extension for running computationally-heavy code as JS from within PHP, rather than PHP code itself, to shave off some cycles.
A tool like this, with enough support, could allow you to easily convert large chunks of slower, PHP code (like object graph hydration) into JS, run in V8 for speed, and use the result in PHP without manually converting libraries.
you should waste your time solving more useful puzzles.
Head into Machine Learning for example, or if you are into VMs, why not try creating a Java VM on top of Cocoa for Android developers to invade iOS, that'll have more impact and you will have to learn some serious compiler theory.
One of the problems I have with that project is that none of the functions are namespaced or sandboxed (i.e. all global); but then again, it is emulating PHP, so there you go :)
I don't think they are comparable. The one linked by toni[1] takes PHP code and compiles it for running in Javascript; the one linked by kaolinite[2] is a translation of some PHP methods into pure Javascript.
Hm, this opens the door to integrating server and client side code more closely.
For instance now we can 'share' validation rules in Forms with client side PHP scripts (the argument some people use to recommend server side JavaScript).
I would really like that.
Actually I think it does have a practical use: if I want to try out a PHP technique, maybe I'm porting a small code snippet, or I want to help a friend debug their code, but don't have a PHP environment available - this is ideal.
Personally, I like JavaScript; but I realise that for some people this is like saying that you have solved the problem of dog poo by creating a machine that turns it into cat poo.
Like a 3 clawed hammer, there simply is no reason for this :) j/k
It's really cool. And really impressive. And if you can make it perform well in the browser (big if I bet) I can think of several valid use cases. Nice work
Given all the compile to JavaScript projects and even video decoders running in JS, I wonder why there is still no chip capable of executing JS on pure Silicon. Since the standard is a very slow moving target, that should speed up all Web applications a lot making it a considerable business case.
We'll eventually see chips optimized for JS execution. It just takes a while. It took a long time for H.264 (video) decoding/encoding to be built in to the hardware.
JS is also a general purpose language, but there's still plenty of potential for hardware optimization.
Implementing script interpreters / miscelleneous applications in JavaScript is quickly becoming the next evolution of esoteric programming languages such as Brainfuck and lolcode: useless, unless you're in college dealing with a TA that likes to be a jerk while grading your code. Heheheh.
I'm a bit skeptic but after trying it I must say that I love it.
I'd like to be able to do server-side compilation (PHP source file to JS file) and include the phpjs min library to run that PHP-to-JS code. With additional libraries and integration we could write client-side code in PHP as well…
Which PHP version does this implement? Doesn't seem to have lambda support :(
call_user_func(function($what) { echo "hello $what\n"; }, "world");
Parse error: Object [object Object] has no method 'Node_Expr_Closure' in /console.htm on line undefined
var_dump([1,2,3]);
works fine though so that must be php 5.4, no? But lack of lambdas makes me sad.
'Any additional unconverted code that gets executed within the VM, such as eval or lambda functions will go through the same process before being executed.'
I was expecting to see something about phpjs.org, which is a great way to get some of the domain-specific language benefits of PHP from within Javascript/node. This project is impressive and quite interesting, but I don't see many practical use cases.
It starts by tokenizing the PHP code into tokens, which it then uses to build an AST tree. Once the tree has been constructed, the script compiles it into JavaScript that can be interpreted by the VM and then executes it.
What you've done there is you've explained the frontend processing that every compiler does, (tokenizing, building an AST) and missed out the part that is entirely specific to this particular one.