Skip to content
HackLang for PHP Transpiler
Branch: master
Clone or download
sgolemon Refactor Generics placeholder grammar
Generics subtypes used in concrete types are more specialized than
generics subtypes used in class/function definitions.
Don't share their parser definitions.
Latest commit 61d79a1 Sep 29, 2016

Phack: HackLang to PHP Transpiler

This project is EXTREMELY EXPERIMENTAL and not ready for prime-time.

Only a few HackLang features are supported at this time, and they aren't fully tested.

What is this?

This project is an attempt to make HackLang code run "out of the box" on a regular PHP runtime without the need for a custom extension (it's all PHP code!). By supporting all of HackLang's rich typing system, we'll be able to make use of the static typechecker to perform robust analysis while still executing it on the stable and trusted PHP runtime.

Any attempt to use HackLang files which have not passed hh_client is a terrible mistake, and you should feel bad.

How does it work?

Phack extends PHP-Parser by amending the PHP 7 parsing rules and overriding the Lexer's pre/post processor hooks. This produces a usable AST, which can then be "PrettyPrinted" into normal PHP. The following table shows our roadmap:

Feature State Notes
Short Lambdas Alpha Needs more tests
Generics Alpha Partial type erasure for PHP compat (base type preserved)
XHP TBD XHP-1.x support available through for now, XHP-2.x coming with this library
Enums Alpha Need to actually implement \InvariantException for full HackLang compat...
Pipe Op Alpha Limited to one use of lhs in rhs (unlike HackLang)
Type Aliasing TBD
Constructor Parameter Promotion Alpha
Prop/Const Typing Alpha Props: Yes, Consts: No
Callable typing Alpha
Soft/Nullable typing Alpha Full erasure on soft/nullables. Need logging for soft, and need psuedo statements for nullable checking (or PHP 7.1 nullable type support)
User Attributes Alpha UA erasure, need to preserve somewhere for reflection.
Shapes TBD
Collections TBD Static initialization is... complicated...
Async Unlikely PHP's Engine just doesn't support this concept well
Trailing Commas Alpha Allow trailing comma in arg/param lists

How do I use it?

To include a HackLang file manually, use \PhpLang\Phack\includeFile() as you would use include, similarly you can use \PhpLang\Phack\evalString as you would eval.

To have composer handle HackLang files transparently, you can try out the EXPERIMENTAL replacement ClassLoader by invoking, at the top level of your entrypoint: \PhpLang\Phack\ClassLoader::hijack();. After this point, any class autoloaded via composer will be preprocessed from HackLang into runable PHP.

To see how HackLang files transpile to PHP, you can run vendor/bin/phack which will invoke PhpLang\Phack\transpileString for you and provide the output.

You can’t perform that action at this time.