https://github.com/xp-framework/compiler
Compiles future PHP to today's PHP.
https://github.com/xp-framework/compiler
annotations arrow-functions ast compiler enums future hack-language language metaprogramming newer-php-versions php php7 php8 php8-features transpiler xp-framework
Last synced: 4 months ago
JSON representation
Compiles future PHP to today's PHP.
- Host: GitHub
- URL: https://github.com/xp-framework/compiler
- Owner: xp-framework
- Created: 2017-10-10T18:56:58.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2025-06-10T13:00:39.000Z (7 months ago)
- Last Synced: 2025-06-10T14:20:08.455Z (7 months ago)
- Topics: annotations, arrow-functions, ast, compiler, enums, future, hack-language, language, metaprogramming, newer-php-versions, php, php7, php8, php8-features, transpiler, xp-framework
- Language: PHP
- Homepage:
- Size: 1.48 MB
- Stars: 19
- Watchers: 2
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
Awesome Lists containing this project
README
XP Compiler
===========
[](https://github.com/xp-framework/compiler/actions)
[](https://github.com/xp-framework/core)
[](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[](http://php.net/)
[](http://php.net/)
[](https://packagist.org/packages/xp-framework/compiler)
Compiles future PHP to today's PHP.
Usage
-----
After adding the compiler to your project via `composer require xp-framework/compiler`, it will hook into the class loading chain and compile `.php`-files on-demand. This keeps the efficient code-save-reload/rerun development process typical for PHP.
Example
-------
The following code uses Hack language, PHP 8.4, PHP 8.3, PHP 8.2, 8.1 and 8.0 features but runs on anything >= PHP 7.4. Builtin features from newer PHP versions are translated to work with the currently executing runtime if necessary.
```php
$args): void {
$greet= fn($to, $from) => self::GREETING.' '.$to.' from '.$from;
$author= Reflection::type(self::class)->annotation(Author::class)->argument(0);
Console::writeLine(new Date()->toString(), ': ', $greet($args[0] ?? 'World', from: $author));
}
}
```
To run this code, use `xp -m /path/to/xp/reflection HelloWorld` in a terminal window.
Compilation
-----------
Compilation can also be performed explicitely by invoking the compiler:
```bash
# Compile code and write result to a class file
$ xp compile HelloWorld.php HelloWorld.class.php
# Compile standard input and write to standard output.
$ echo " []
@FileSystemCL<./vendor/xp-framework/compiler/src/main/php>
lang.ast.emit.PHP74
lang.ast.emit.PHP80
lang.ast.emit.PHP81
lang.ast.emit.PHP82
lang.ast.emit.PHP83 [*]
lang.ast.emit.PHP84
lang.ast.emit.PHP85
lang.ast.syntax.php.Using [*]
@FileSystemCL<./vendor/xp-lang/php-is-operator/src/main/php>
lang.ast.syntax.php.IsOperator
```
Implementation status
---------------------
Some features from newer PHP versions as well as Hack language are still missing. The goal, however, is to have all features implemented - with the exception of where Hack's direction conflicts with PHP! An overview can be seen [on this Wiki page](https://github.com/xp-framework/compiler/wiki/Implementation-status).
To contribute, open issues and/or pull requests.
See also
--------
* [XP Compiler design](https://github.com/xp-framework/compiler/wiki/Compiler-design)
* [XP RFC #0299: Make XP compiler the Babel of PHP](https://github.com/xp-framework/rfc/issues/299)
* [XP RFC #0327: Compile-time metaprogramming](https://github.com/xp-framework/rfc/issues/327)