Skip to content

Commit

Permalink
Use named constructors for infix and prefix function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
MidnightDesign committed Mar 25, 2024
1 parent 926287d commit ddff1b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
23 changes: 23 additions & 0 deletions src/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ public function __construct(
$this->location = $location;
}

/**
* @template U
* @param Expression<mixed> $left
* @param Expression<mixed> $right
* @param Type<U> $type
* @return self<U>
*/
public static function infix(string $name, Expression $left, Expression $right, Type $type): self
{
return new self($left, $name, $type, [$right], $left->location()->to($right->location()), CallType::Infix);
}

/**
* @template U
* @param Expression<mixed> $argument
* @param Type<U> $type
* @return self<U>
*/
public static function prefix(string $name, Expression $argument, Type $type, Span $location): self
{
return new self($argument, $name, $type, [], $location, CallType::Prefix);
}

/**
* @param list<Expression<mixed>> $a
* @param list<Expression<mixed>> $b
Expand Down
18 changes: 5 additions & 13 deletions src/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private function __construct()
*/
public static function eq(Expression $left, Expression $right): Expression
{
return new Call($left, '===', Type::bool(), [$right], $left->location()->to($right->location()), CallType::Infix);
return Call::infix('===', $left, $right, Type::bool());
}

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ public static function call(Expression $target, string $name, Type $type, array
*/
public static function or_(Expression $left, Expression $right): Expression
{
return new Call($left, '||', Type::bool(), [$right], $left->location()->to($right->location()), CallType::Infix);
return Call::infix('||', $left, $right, Type::bool());
}

/**
Expand All @@ -109,14 +109,7 @@ public static function lambda(Expression $body, array $parameters = [], Span|nul
*/
public static function subtract(Expression $minuend, Expression $subtrahend): Expression
{
return new Call(
$minuend,
'-',
$minuend->getType(),
[$subtrahend],
$minuend->location()->to($subtrahend->location()),
CallType::Infix,
);
return Call::infix('-', $minuend, $subtrahend, $minuend->getType());
}

/**
Expand All @@ -127,7 +120,7 @@ public static function subtract(Expression $minuend, Expression $subtrahend): Ex
*/
public static function gt(Expression $left, Expression $right): Expression
{
return new Call($left, '>', Type::bool(), [$right], $left->location()->to($right->location()), CallType::Infix);
return Call::infix('>', $left, $right, Type::bool());
}

/**
Expand All @@ -137,8 +130,7 @@ public static function gt(Expression $left, Expression $right): Expression
*/
public static function negative(Expression $expression, Span|null $location = null): Expression
{
$location ??= self::dummySpan();
return new Call($expression, '-', $expression->getType(), [], $location, CallType::Prefix);
return Call::prefix('-', $expression, $expression->getType(), $location ?? self::dummySpan());
}

private static function dummySpan(): Span
Expand Down

0 comments on commit ddff1b4

Please sign in to comment.