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

Implement operators as functions #21

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions src/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __construct(
public readonly Type $type,
public readonly array $arguments,
Span $location,
public readonly CallType $callType = CallType::Method,
) {
$this->location = $location;
}
Expand All @@ -58,8 +59,12 @@ private static function compareArguments(array $a, array $b): bool

public function __toString(): string
{
/** @psalm-suppress ImplicitToStringCast */
return sprintf('%s.%s:%s(%s)', $this->target, $this->name, $this->type, implode(', ', $this->arguments));
return match ($this->callType) {
CallType::Infix => sprintf('%s %s %s', $this->target, $this->name, $this->arguments[0]),
CallType::Prefix => sprintf('%s%s', $this->name, $this->target),
/** @psalm-suppress ImplicitToStringCast */
CallType::Method => sprintf('%s.%s:%s(%s)', $this->target, $this->name, $this->type, implode(', ', $this->arguments)),
};
}

public function evaluate(Scope $scope): mixed
Expand Down
12 changes: 12 additions & 0 deletions src/CallType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Eventjet\Ausdruck;

enum CallType
{
case Method;
case Infix;
case Prefix;
}
53 changes: 0 additions & 53 deletions src/Eq.php
rieschl marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

36 changes: 23 additions & 13 deletions src/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ private function __construct()
* @template T
* @param Expression<T> $left
* @param Expression<T> $right
* @return Expression<bool>
*/
public static function eq(Expression $left, Expression $right): Eq
public static function eq(Expression $left, Expression $right): Expression
{
return new Eq($left, $right);
return new Call($left, '===', Type::bool(), [$right], $left->location()->to($right->location()), CallType::Infix);
}

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

/**
Expand All @@ -103,32 +105,40 @@ public static function lambda(Expression $body, array $parameters = [], Span|nul
* @template T of int | float
* @param Expression<T> $minuend
* @param Expression<T> $subtrahend
* @return Subtract<T>
* @return Expression<T>
*/
public static function subtract(Expression $minuend, Expression $subtrahend): Subtract
public static function subtract(Expression $minuend, Expression $subtrahend): Expression
{
return new Subtract($minuend, $subtrahend);
return new Call(
$minuend,
'-',
$minuend->getType(),
[$subtrahend],
$minuend->location()->to($subtrahend->location()),
CallType::Infix,
);
}

/**
* @template T of int | float
* @param Expression<T> $left
* @param Expression<T> $right
* @return Gt<T>
* @return Expression<bool>
*/
public static function gt(Expression $left, Expression $right): Gt
public static function gt(Expression $left, Expression $right): Expression
{
return new Gt($left, $right);
return new Call($left, '>', Type::bool(), [$right], $left->location()->to($right->location()), CallType::Infix);
}

/**
* @template T of int | float
* @param Expression<T> $expression
* @return Negative<T>
* @return Expression<T>
*/
public static function negative(Expression $expression, Span|null $location = null): Negative
public static function negative(Expression $expression, Span|null $location = null): Expression
{
return new Negative($expression, $location ?? self::dummySpan());
$location ??= self::dummySpan();
return new Call($expression, '-', $expression->getType(), [], $location, CallType::Prefix);
}

private static function dummySpan(): Span
Expand Down
14 changes: 8 additions & 6 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ abstract class Expression implements Stringable
{
/**
* @param self<mixed> $other
* @return Expression<bool>
*/
public function eq(self $other): Eq
public function eq(self $other): self
{
return Expr::eq($this, $other);
}

/**
* @template U of int | float
* @param Expression<U> $subtrahend
* @return Subtract<U>
* @return Expression<U>
*/
public function subtract(self $subtrahend): Subtract
public function subtract(self $subtrahend): self
{
/** @var self<U> $self */
$self = $this;
Expand All @@ -36,9 +37,9 @@ public function subtract(self $subtrahend): Subtract
/**
* @template U of int | float
* @param Expression<U> $right
* @return Gt<U>
* @return Expression<bool>
*/
public function gt(self $right): Gt
public function gt(self $right): self
{
/** @var self<U> $self */
$self = $this;
Expand All @@ -47,8 +48,9 @@ public function gt(self $right): Gt

/**
* @param self<bool> $other
* @return Expression<bool>
*/
public function or_(self $other): Or_
public function or_(self $other): self
{
/** @var self<bool> $self */
$self = $this;
Expand Down
54 changes: 0 additions & 54 deletions src/Gt.php

This file was deleted.

51 changes: 0 additions & 51 deletions src/Negative.php

This file was deleted.

53 changes: 0 additions & 53 deletions src/Or_.php

This file was deleted.

Loading
Loading