-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
539c843
commit af11e7d
Showing
10 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\Ausdruck; | ||
|
||
use Eventjet\Ausdruck\Parser\Span; | ||
|
||
use function array_map; | ||
use function implode; | ||
|
||
/** | ||
* @template T | ||
* @extends Expression<list<T>> | ||
*/ | ||
final class ListLiteral extends Expression | ||
{ | ||
/** | ||
* @param list<Expression<T>> $elements | ||
*/ | ||
public function __construct(public readonly array $elements, public readonly Span $location) | ||
{ | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return '[' . implode(', ', $this->elements) . ']'; | ||
} | ||
|
||
public function location(): Span | ||
{ | ||
return $this->location; | ||
} | ||
|
||
/** | ||
* @return list<T> | ||
*/ | ||
public function evaluate(Scope $scope): array | ||
{ | ||
return array_map( | ||
static fn(Expression $element): mixed => $element->evaluate($scope), | ||
$this->elements, | ||
); | ||
} | ||
|
||
public function equals(Expression $other): bool | ||
{ | ||
if (!$other instanceof self) { | ||
return false; | ||
} | ||
foreach ($this->elements as $i => $element) { | ||
if ($element->equals($other->elements[$i])) { | ||
continue; | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @return Type<list<T>> | ||
*/ | ||
public function getType(): Type | ||
{ | ||
$elementType = null; | ||
foreach ($this->elements as $element) { | ||
$type = $element->getType(); | ||
if ($elementType === null) { | ||
$elementType = $type; | ||
continue; | ||
} | ||
/** @psalm-suppress RedundantCondition */ | ||
if ($elementType->equals($type)) { | ||
continue; | ||
} | ||
$elementType = Type::any(); | ||
} | ||
return Type::listOf($elementType ?? Type::any()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters