-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement comparison operators (address #9)
- Loading branch information
Showing
13 changed files
with
256 additions
and
41 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
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
71 changes: 71 additions & 0 deletions
71
src/muqsit/arithmexp/operator/assignment/NonAssociativeOperatorAssignment.php
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace muqsit\arithmexp\operator\assignment; | ||
|
||
use Generator; | ||
use muqsit\arithmexp\operator\OperatorList; | ||
use muqsit\arithmexp\ParseException; | ||
use muqsit\arithmexp\token\BinaryOperatorToken; | ||
use function count; | ||
use function is_array; | ||
|
||
final class NonAssociativeOperatorAssignment implements OperatorAssignment{ | ||
|
||
public static function instance() : self{ | ||
static $instance = null; | ||
return $instance ??= new self(); | ||
} | ||
|
||
private function __construct(){ | ||
} | ||
|
||
public function getType() : int{ | ||
return self::TYPE_NON_ASSOCIATIVE; | ||
} | ||
|
||
private function getPairPrecedence(OperatorList $list, mixed $entry) : ?int{ | ||
if(!is_array($entry) || count($entry) !== 3 || !($entry[1] instanceof BinaryOperatorToken)){ | ||
return null; | ||
} | ||
if(!isset($list->binary[$entry[1]->operator])){ | ||
return null; | ||
} | ||
return $list->binary[$entry[1]->operator]->getPrecedence(); | ||
} | ||
|
||
public function traverse(OperatorList $list, string $expression, array &$tokens) : Generator{ | ||
$state = new OperatorAssignmentTraverserState($tokens); | ||
$index = -1; | ||
$count = count($tokens); | ||
while(++$index < $count){ | ||
$value = $tokens[$index]; | ||
if($value instanceof BinaryOperatorToken && isset($list->binary[$value->operator])){ | ||
$precedence = $list->binary[$value->operator]->getPrecedence(); | ||
if($index > 0){ | ||
$lvalue = $tokens[$index - 1]; | ||
$lvalue_precedence = $this->getPairPrecedence($list, $lvalue); | ||
if($lvalue_precedence === $precedence){ | ||
throw ParseException::undefinedOperatorAssociativity($expression, $value->getPos()); | ||
} | ||
} | ||
if($index + 1 < $count){ | ||
$rvalue = $tokens[$index + 1]; | ||
$rvalue_precedence = $this->getPairPrecedence($list, $rvalue); | ||
if($rvalue_precedence === $precedence){ | ||
throw ParseException::undefinedOperatorAssociativity($expression, $rvalue[1]->getPos()); | ||
} | ||
} | ||
$state->index = $index; | ||
$state->value = $value; | ||
yield $state; | ||
if($state->changed){ | ||
$index = -1; | ||
$count = count($tokens); | ||
$state->changed = false; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.