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

Add support for components type literals #8

Merged
merged 4 commits into from
Oct 1, 2023
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
48 changes: 47 additions & 1 deletion src/main/php/lang/ast/syntax/php/Generics.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
InvokeExpression,
Literal,
ScopeExpression,
TernaryExpression
TernaryExpression,
Variable
};
use lang\ast\syntax\Extension;
use lang\ast\types\{IsArray, IsFunction, IsGeneric, IsMap, IsUnion, IsNullable, IsValue};
Expand Down Expand Up @@ -233,6 +234,51 @@ public function setup($language, $emitter) {
$node->arguments
);
}

// Rewrite `new T(...)` -> `$T->newInstance(...)` if T is a component
if (
$node->type instanceof IsValue &&
$generic= self::generic($node->type, $codegen->scope[0]->type->name->components())
) {
return new InvokeExpression(
new InstanceExpression(new Variable($generic), new Literal('newInstance')),
$node->arguments
);
}

return $node;
});

$emitter->transform('instanceof', function($codegen, $node) {

// Rewrite `... instanceof T` -> `$T->isInstance(...)` if T is a component
if (
is_string($node->type) &&
$generic= self::generic(new IsValue($node->type), $codegen->scope[0]->type->name->components())
) {
return new InvokeExpression(
new InstanceExpression(new Variable($generic), new Literal('isInstance')),
[$node->expression]
);
}

return $node;
});

$emitter->transform('scope', function($codegen, $node) {

// Rewrite `T::class` to `$T->literal()` if T is a component
if (
$node->member instanceof Literal &&
'class' === $node->member->expression &&
$generic= self::generic(new IsValue($node->type), $codegen->scope[0]->type->name->components())
) {
return new InvokeExpression(
new InstanceExpression(new Variable($generic), new Literal('literal')),
[]
);
}

return $node;
});

Expand Down
30 changes: 28 additions & 2 deletions src/test/php/lang/ast/syntax/php/unittest/GenericsTest.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace lang\ast\syntax\php\unittest;

use lang\ast\unittest\emit\EmittingTest;
use lang\{ArrayType, MapType, Primitive, Reflection};
use lang\{ArrayType, MapType, Primitive, Reflection, XPClass};
use test\{Assert, Test, Values};

class GenericsTest extends EmittingTest {
Expand Down Expand Up @@ -89,7 +89,7 @@ public function run() {
public function new_component() {
$t= $this->type('class %T<E> {
public static function fixture($arg) {
return $E->newInstance($arg);
return new E($arg);
}
}');
Assert::equals(6100, Reflection::type($t->newGenericType([Primitive::$INT]))
Expand All @@ -98,6 +98,32 @@ public static function fixture($arg) {
);
}

#[Test]
public function instanceof_component() {
$t= $this->type('class %T<E> {
public static function fixture($arg) {
return $arg instanceof E;
}
}');
Assert::equals(true, Reflection::type($t->newGenericType([new XPClass(self::class)]))
->method('fixture')
->invoke(null, [$this])
);
}

#[Test]
public function component_class() {
$t= $this->type('class %T<E> {
public static function fixture() {
return E::class;
}
}');
Assert::equals(self::class, Reflection::type($t->newGenericType([new XPClass(self::class)]))
->method('fixture')
->invoke(null, [])
);
}

#[Test]
public function string_queue() {
$r= $this->run('class %T<E> {
Expand Down
Loading