Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class HelloWorld {
$author= Reflection::type(self::class)->annotation(Author::class)->argument(0);

Console::writeLine(new Date()->toString(), ': ');
$greet($args[0] ?? 'World', from: $author) |> Console::writeLine(?);
$hello= $greet(?, from: $author);
$hello($args[0] ?? 'World') |> Console::writeLine(?);
}
}
```
Expand Down Expand Up @@ -93,9 +94,10 @@ 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.PHP83
lang.ast.emit.PHP84 [*]
lang.ast.emit.PHP85
lang.ast.emit.PHP86
lang.ast.syntax.php.Using [*]

@FileSystemCL<./vendor/xp-lang/php-is-operator/src/main/php>
Expand Down
15 changes: 14 additions & 1 deletion src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
InstanceExpression,
Literal,
NewExpression,
Placeholder,
Property,
ScopeExpression,
UnpackExpression,
Expand Down Expand Up @@ -1123,6 +1124,16 @@ protected function emitClone($result, $clone) {
$result->out->write(')');
}

protected function emitPlaceholder($result, $placeholder) {
if (Placeholder::$VARIADIC === $placeholder) {
$result->out->write('...');
} else if (Placeholder::$ARGUMENT === $placeholder) {
$result->out->write('?');
} else {
$this->raise('Unsupportet placeholder '.$placeholder->kind());
}
}

protected function emitCallable($result, $callable) {

// Disambiguate the following:
Expand All @@ -1132,7 +1143,9 @@ protected function emitCallable($result, $callable) {
$callable->expression->line= -1;

$this->emitOne($result, $callable->expression);
$result->out->write('(...)');
$result->out->write('(');
$this->emitArguments($result, $callable->arguments);
$result->out->write(')');
}

protected function emitCallableNew($result, $callable) {
Expand Down
57 changes: 57 additions & 0 deletions src/main/php/lang/ast/emit/PHP86.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php namespace lang\ast\emit;

use lang\ast\types\{
IsArray,
IsFunction,
IsGeneric,
IsIntersection,
IsLiteral,
IsMap,
IsNullable,
IsUnion,
IsValue
};

/**
* PHP 8.6 syntax
*
* @test lang.ast.unittest.emit.PHP86Test
* @see https://wiki.php.net/rfc#php_86
*/
class PHP86 extends PHP {
use RewriteBlockLambdaExpressions;

public $targetVersion= 80600;

/** Sets up type => literal mappings */
public function __construct() {
$this->literals= [
IsArray::class => function($t) { return 'array'; },
IsMap::class => function($t) { return 'array'; },
IsFunction::class => function($t) { return 'callable'; },
IsValue::class => function($t) { return $t->literal(); },
IsNullable::class => function($t) {
if (null === ($l= $this->literal($t->element))) return null;
return $t->element instanceof IsUnion ? $l.'|null' : '?'.$l;
},
IsIntersection::class => function($t) {
$i= '';
foreach ($t->components as $component) {
if (null === ($l= $this->literal($component))) return null;
$i.= '&'.$l;
}
return substr($i, 1);
},
IsUnion::class => function($t) {
$u= '';
foreach ($t->components as $component) {
if (null === ($l= $this->literal($component))) return null;
$u.= '|'.$l;
}
return substr($u, 1);
},
IsLiteral::class => function($t) { return $t->literal(); },
IsGeneric::class => function($t) { return null; }
];
}
}
14 changes: 1 addition & 13 deletions src/test/php/lang/ast/unittest/emit/CallableSyntaxTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public function run() {
Assert::equals(1, $count);
}

#[Test, Runtime(php: '>=8.6.0')]
#[Test, Runtime(php: '>=8.6.0-dev')]
public function partial_function_application_with_named() {
$r= $this->run('class %T {

Expand All @@ -435,18 +435,6 @@ public function run() {
Assert::equals('ok.', $r);
}

#[Test, Runtime(php: '>=8.6.0')]
public function partial_function_application_variadic_before_named() {
$r= $this->run('class %T {

public function run() {
$f= str_replace("test", ..., subject: ?);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This raises a compile error: Variadic placeholder must be last

return $f("ok", "test.");
}
}');
Assert::equals('ok.', $r);
}

#[Test]
public function partial_function_application_omit_optional() {
$f= $this->run('class %T {
Expand Down
26 changes: 26 additions & 0 deletions src/test/php/lang/ast/unittest/emit/PHP86Test.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php namespace lang\ast\unittest\emit;

use lang\ast\emit\Type;
use test\{Assert, Test};

class PHP86Test extends EmittingTest {

/** @return string */
protected function runtime() { return 'php:8.6.0'; }

#[Test]
public function partial_function_application_argument() {
Assert::equals(
'str_replace("test","ok",?);',
$this->emit('str_replace("test", "ok", ?)')
);
}

#[Test]
public function partial_function_application_variadic() {
Assert::equals(
'str_replace("test","ok",...);',
$this->emit('str_replace("test", "ok", ...)')
);
}
}