Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make XP compiler the Babel of PHP #299

Closed
22 of 27 tasks
thekid opened this issue May 14, 2015 · 6 comments
Closed
22 of 27 tasks

Make XP compiler the Babel of PHP #299

thekid opened this issue May 14, 2015 · 6 comments

Comments

@thekid
Copy link
Member

thekid commented May 14, 2015

Scope of Change

This RFC suggests changing the PHP language support in the XP Compiler to support all future PHP features (at the time of writing, PHP 7). This would mean you could already start writing PHP 7 code, and could transpile it for running on older, PHP 5.5, environments.

Rationale

Ease adoption of new language features, decouple from slowly adopting production platforms and environments.

Functionality

Design principles

  • The future, now!
  • Valid PHP passes through the compiler untouched (except alternative syntax)
  • Output is as close as possible line/offset/whitespace wise

Compilation

Compilation can either be done explicitly by calling xcc or by using the JIT compiler.

$ xcc -E php5.5 -sp src/main/php -o target/ # Translates PHP 5.6, 7.0 
$ xcc -E php5.6 -sp src/main/php -o target/ # Translates PHP 7.0 features only
$ xcc -E php7.0 -sp src/main/php -o target/ # Copy-through, more or less
# ...

Features

The following are easy:

These need more investigation:

These might not work in a fast and reliable way:

Experimental support could be added for features currently under discussion:

We could also add Hack language features

We might want to include support for PHP 5.6:

Security considerations

Speed impact

Dependencies

Related documents

Inspired by http://channel9.msdn.com/Events/Build/2015/3-644

@thekid
Copy link
Member Author

thekid commented Oct 8, 2017

<?php namespace test;

use util\cmd\Console;
use peer\http\HttpConnection;

// https://wiki.php.net/rfc/group_use_declarations
use peer\{ConnectException, SocketException};

// https://wiki.php.net/rfc/variadics
function println(... $_) {

  // https://wiki.php.net/rfc/argument_unpacking
  return Console::writeLine(...$_);
}

class Uri {
  // https://docs.hhvm.com/hack/types/annotations#class-properties
  // (https://wiki.php.net/rfc/property_type_hints)
  private HttpConnection $conn= null;

  // https://docs.hhvm.com/hack/other-features/constructor-parameter-promotion
  public function __construct(private string $uri) { }

  // https://github.com/xp-framework/rfc/issues/241
  public function __toString(): string ==> $this->uri;

  // https://docs.hhvm.com/hack/attributes/introduction
  <<deprecated('Use connection() instead')>>
  public function getConnection() ==> $this->connection();

  // https://wiki.php.net/rfc/isset_ternary
  public function connection(): HttpConnection {
    return $this->conn ?? $this->conn= new HttpConnection($this);
  }
}

// https://docs.hhvm.com/hack/operators/lambda
// (https://wiki.php.net/rfc/arrow_functions)
$get= (Uri $uri) ==> $uri->connection()->get();

try {
  $r= $get(new Uri($argv[1] ?? 'http://localhost'));
  println($r);

  $s= $r->header('Content-Length')[0];
  println('read(', $s, ') => ', strlen($r->readData($s)));
} catch (ConnectException | SocketException $e) {

  // https://wiki.php.net/rfc/multiple-catch
  println('Error ', $e->compoundMessage());
} finally {
  println('Done');
}

@thekid
Copy link
Member Author

thekid commented Oct 10, 2017

Developed as library

https://github.com/xp-framework/compiler

@thekid
Copy link
Member Author

thekid commented Oct 12, 2017

@thekid
Copy link
Member Author

thekid commented Oct 14, 2017

Static imports?

<?php namespace demo;

use function util\cmd\Console::writeLine;

class HelloWorld {

  public static function main(array $args): int {
    writeLine('Hello, World!');
    return 0;
  }
}

http://php.net/manual/en/language.namespaces.importing.php

@thekid
Copy link
Member Author

thekid commented Oct 15, 2017

Library has reached version 0.5.0 in the meantime, looking good!

@thekid
Copy link
Member Author

thekid commented Nov 4, 2017

Library has reached surpassed 1.0.0. by now and is in productive use

@thekid thekid added accepted and removed discussion labels Nov 4, 2017
@thekid thekid closed this as completed Apr 1, 2018
@thekid thekid changed the title Make XP compiler the TypeScript of PHP Make XP compiler the Babel of PHP Oct 6, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant