Compile hack to php
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode merged Jul 18, 2018
bin merged Jul 18, 2018
example-files merged Jul 18, 2018
out merged Jul 18, 2018
src merged Jul 18, 2018
tests merged Jul 22, 2018
.gitignore fix tests Jun 9, 2018
.hhconfig init Jun 9, 2018
.travis.sh fix build Jun 9, 2018
.travis.yml install hhvm Jun 9, 2018
LICENSE Initial commit Jun 9, 2018
README.md better example Jun 10, 2018
composer.json merged Jul 18, 2018
composer.lock merged Jul 18, 2018
composer.phar init Jun 9, 2018
hh_autoload.json init Jun 9, 2018
hhast-lint.json init Jun 9, 2018
phpunit.xml init Jun 9, 2018

README.md

hack2php

Build Status

hack2php is a project which aims to implement a compiler to translate Hack files to PHP 5.4 files. This ability becomes useful when you have no control over the environment in which your code is supposed to run, but you still want to write your code in Hack. An example might be the development of WordPress plugins or themes.

E.g. this Hack code:

<?hh //strict

namespace codeneric\phmm\legacy\validate;
use codeneric\phmm\legacy\blub;

function blub(string $v): ?string {
    return null;
}

function ano(): void {
    $a = 42;
    $arr = [1, 2, 3, 42, 5, 6];
    $f = ($e) ==> {
        return \in_array($a, $arr);
    };
}

is transpiled to this PHP code:

<?php //strict
namespace codeneric\phmm\legacy\validate;
use \codeneric\phmm\legacy\blub;

function blub($v){
    return null;
}


function ano(){
    $a = 42;
    $arr = [1, 2, 3, 42, 5, 6];
    $f = function ($e)  use($a,$arr) {
        return \in_array($a, $arr);
    };
}

Disclaimer

This project is in a very early stage, so you will probably encounter problems in some cases. If so, please create a PR and an issue as described in the Contribution section!

Getting started

Clone this repository and run hhvm composer.phar install

Compile a Hack file to PHP: ./bin/hack2php <hack-file>

Tests

There is only one test (HackToPhpTest). It reads each Hack file from the example-files directory, compiles it to PHP and checks the PHP syntax for errors. If no error were found, the test succeeds. Otherwise it fails. To add a test, simply create a new Hack file in example-files. Run the test: hhvm vendor/bin/phpunit

Contributing

If you find an issue you can help to fix it. Please add a Hack file which is not compiled correctly in the example-files folder and create a PR. Or you can fix the issue directly :) But please add an example Hack file to the example-files folder nonetheless.