xp-framework / compiler
XP Compiler
Installs: 17 968
Dependents: 8
Suggesters: 0
Security: 0
Stars: 19
Watchers: 3
Forks: 0
Open Issues: 10
Requires
- php: >=7.4.0
- xp-framework/ast: ^11.3
- xp-framework/core: ^12.0 | ^11.6 | ^10.16
- xp-framework/reflection: ^3.2 | ^2.15
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.5
- dev-master
- v9.3.2
- v9.3.1
- v9.3.0
- v9.2.0
- v9.1.1
- v9.1.0
- v9.0.0
- v8.17.2
- v8.17.1
- v8.17.0
- v8.16.0
- v8.15.1
- v8.15.0
- v8.14.0
- v8.13.0
- v8.12.0
- v8.11.0
- v8.10.0
- v8.9.0
- v8.8.5
- v8.8.4
- v8.8.3
- v8.8.2
- v8.8.1
- v8.8.0
- v8.7.0
- v8.6.0
- v8.5.1
- v8.5.0
- v8.4.0
- v8.3.0
- v8.2.0
- v8.1.0
- v8.0.0
- v7.3.0
- v7.2.1
- v7.2.0
- v7.1.0
- v7.0.0
- v6.11.0
- v6.10.0
- v6.9.0
- v6.8.3
- v6.8.2
- v6.8.1
- v6.8.0
- v6.7.0
- v6.6.0
- v6.5.0
- v6.4.0
- v6.3.2
- v6.3.1
- v6.3.0
- v6.2.0
- v6.1.1
- v6.1.0
- v6.0.0
- v5.7.0
- v5.6.0
- v5.5.0
- v5.4.1
- v5.4.0
- v5.3.0
- v5.2.1
- v5.2.0
- v5.1.3
- v5.1.2
- v5.1.1
- v5.1.0
- v5.0.0
- v4.3.1
- v4.3.0
- v4.2.1
- v4.2.0
- v4.1.0
- v4.0.0
- v3.0.0
- v2.13.0
- v2.12.0
- v2.11.1
- v2.11.0
- v2.10.1
- v2.10.0
- v2.9.0
- v2.8.0
- v2.7.0
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.1
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- v0.9.1
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.0
- dev-feature/pipelines
- dev-feature/interface-defaults
- dev-feature/instance-method-callable
- dev-feature/unchecked-types
- dev-feature/namespace-constant
- dev-four
- dev-three
- dev-two
This package is auto-updated.
Last update: 2024-12-02 16:37:31 UTC
README
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 // In a file "HelloWorld.php" use lang\Reflection; use util\Date; use util\cmd\Console; #[Author('Timm Friebe')] #[Permissions(0o777)] class HelloWorld { private const string GREETING= 'Hello'; public static function main(array<string> $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:
# 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 "<?php ..." | xp compile - # Compile src/main/php and src/test/php to the dist folder. $ xp compile -o dist src/main/php/ src/test/php/ # Compile src/main/php to the dist.xar archive $ xp compile -o dist.xar src/main/php/ # Compile src/main/php, do not write output $ xp compile -n src/main/php/ # Target PHP 7.4 (default target is current PHP version) $ xp compile -t php:7.4 HelloWorld.php HelloWorld.class.php # Emit XP meta information (includes lang.ast.emit.php.XpMeta): $ xp compile -t php:7.4 -a php:xp-meta -o dist src/main/php
The -o and -n options accept multiple input sources following them. The -q option suppresses all diagnostic output except for errors.
Features supported
XP Compiler supports features such as annotations, arrow functions, enums, property type-hints, the null-safe instance operator as well as all PHP 7 and PHP 8 syntax additions. A complete list including examples can be found in our Wiki.
More features
Additional syntax like an is
operator, generics or record types can be added by installing compiler plugins from here:
$ composer require xp-lang/php-is-operator # ... $ xp compile Usage: xp compile <in> [<out>] @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.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.
To contribute, open issues and/or pull requests.