-
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.
Allow expressions to return bool value (address #9)
- Loading branch information
Showing
15 changed files
with
169 additions
and
26 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
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
31 changes: 31 additions & 0 deletions
31
src/muqsit/arithmexp/expression/token/BooleanLiteralExpressionToken.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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace muqsit\arithmexp\expression\token; | ||
|
||
use muqsit\arithmexp\Position; | ||
|
||
final class BooleanLiteralExpressionToken implements ExpressionToken{ | ||
|
||
public function __construct( | ||
public Position $position, | ||
public bool $value | ||
){} | ||
|
||
public function getPos() : Position{ | ||
return $this->position; | ||
} | ||
|
||
public function isDeterministic() : bool{ | ||
return true; | ||
} | ||
|
||
public function equals(ExpressionToken $other) : bool{ | ||
return $other instanceof self && $other->value === $this->value; | ||
} | ||
|
||
public function __toString() : string{ | ||
return $this->value ? "true" : "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace muqsit\arithmexp\token; | ||
|
||
use muqsit\arithmexp\expression\token\BooleanLiteralExpressionToken; | ||
use muqsit\arithmexp\Position; | ||
use muqsit\arithmexp\token\builder\ExpressionTokenBuilderState; | ||
|
||
final class BooleanLiteralToken extends SimpleToken{ | ||
|
||
public function __construct( | ||
Position $position, | ||
readonly public bool $value | ||
){ | ||
parent::__construct(TokenType::BOOLEAN_LITERAL(), $position); | ||
} | ||
|
||
public function repositioned(Position $position) : self{ | ||
return new self($position, $this->value); | ||
} | ||
|
||
public function writeExpressionTokens(ExpressionTokenBuilderState $state) : void{ | ||
$state->current_group[$state->current_index] = new BooleanLiteralExpressionToken($this->position, $this->value); | ||
} | ||
|
||
public function __debugInfo() : array{ | ||
$info = parent::__debugInfo(); | ||
$info["value"] = $this->value; | ||
return $info; | ||
} | ||
|
||
public function jsonSerialize() : string{ | ||
return $this->value ? "true" : "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
44 changes: 44 additions & 0 deletions
44
src/muqsit/arithmexp/token/builder/BooleanLiteralTokenBuilder.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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace muqsit\arithmexp\token\builder; | ||
|
||
use Generator; | ||
use muqsit\arithmexp\Position; | ||
use muqsit\arithmexp\token\BooleanLiteralToken; | ||
use muqsit\arithmexp\token\NumericLiteralToken; | ||
use function rtrim; | ||
use function str_contains; | ||
use function strlen; | ||
use function substr; | ||
|
||
final class BooleanLiteralTokenBuilder implements TokenBuilder{ | ||
|
||
private const VALUES = ["true" => true, "false" => false]; | ||
private const BUFFER_LENGTH = 5; // max(len(keys(self::VALUES))) | ||
|
||
public function __construct(){ | ||
} | ||
|
||
public function build(TokenBuilderState $state) : Generator{ | ||
$buffer = ""; | ||
$offset = $state->offset; | ||
$start = $offset; | ||
$length = $state->length; | ||
$expression = $state->expression; | ||
while($offset < $length){ | ||
$char = $expression[$offset]; | ||
$buffer = substr($buffer . $char, -self::BUFFER_LENGTH); | ||
if(isset(self::VALUES[$buffer])){ | ||
$value = self::VALUES[$buffer]; | ||
yield new BooleanLiteralToken(new Position($start, $offset), $value); | ||
break; | ||
} | ||
$offset++; | ||
} | ||
} | ||
|
||
public function transform(TokenBuilderState $state) : void{ | ||
} | ||
} |