-
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.
Opcode token operations are implemented inlined and execute much faster than closure-backed fcalls
- Loading branch information
Showing
4 changed files
with
148 additions
and
3 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
42 changes: 42 additions & 0 deletions
42
src/muqsit/arithmexp/expression/token/OpcodeExpressionToken.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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace muqsit\arithmexp\expression\token; | ||
|
||
use muqsit\arithmexp\expression\Expression; | ||
use muqsit\arithmexp\Position; | ||
use muqsit\arithmexp\token\OpcodeToken; | ||
use RuntimeException; | ||
|
||
final class OpcodeExpressionToken implements ExpressionToken{ | ||
|
||
/** | ||
* @param Position $position | ||
* @param OpcodeToken::OP_* $code | ||
*/ | ||
public function __construct( | ||
public Position $position, | ||
public int $code | ||
){} | ||
|
||
public function getPos() : Position{ | ||
return $this->position; | ||
} | ||
|
||
public function isDeterministic() : bool{ | ||
return true; | ||
} | ||
|
||
public function retrieveValue(Expression $expression, array $variables) : int|float{ | ||
throw new RuntimeException("Don't know how to retrieve value of " . self::class); | ||
} | ||
|
||
public function equals(ExpressionToken $other) : bool{ | ||
return $other instanceof self && $other->code === $this->code; | ||
} | ||
|
||
public function __toString() : string{ | ||
return OpcodeToken::opcodeToString($this->code); | ||
} | ||
} |
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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace muqsit\arithmexp\token; | ||
|
||
use muqsit\arithmexp\expression\token\OpcodeExpressionToken; | ||
use muqsit\arithmexp\Position; | ||
use muqsit\arithmexp\token\builder\ExpressionTokenBuilderState; | ||
|
||
final class OpcodeToken extends SimpleToken{ | ||
|
||
public const OP_BINARY_ADD = 0; | ||
public const OP_BINARY_DIV = 1; | ||
public const OP_BINARY_EXP = 2; | ||
public const OP_BINARY_MOD = 3; | ||
public const OP_BINARY_MUL = 4; | ||
public const OP_BINARY_SUB = 5; | ||
public const OP_UNARY_NVE = 6; | ||
public const OP_UNARY_PVE = 7; | ||
|
||
/** | ||
* @param self::OP_* $code | ||
* @return string | ||
*/ | ||
public static function opcodeToString(int $code) : string{ | ||
return match($code){ | ||
self::OP_BINARY_ADD, self::OP_UNARY_PVE => "+", | ||
self::OP_BINARY_DIV => "/", | ||
self::OP_BINARY_EXP => "**", | ||
self::OP_BINARY_MOD => "%", | ||
self::OP_BINARY_MUL => "*", | ||
self::OP_BINARY_SUB, self::OP_UNARY_NVE => "-" | ||
}; | ||
} | ||
|
||
/** | ||
* @param Position $position | ||
* @param self::OP_* $code | ||
*/ | ||
public function __construct( | ||
Position $position, | ||
private int $code | ||
){ | ||
parent::__construct(TokenType::OPCODE(), $position); | ||
} | ||
|
||
/** | ||
* @return self::OP_* | ||
*/ | ||
public function getCode() : int{ | ||
return $this->code; | ||
} | ||
|
||
public function repositioned(Position $position) : self{ | ||
return new self($position, $this->code); | ||
} | ||
|
||
public function writeExpressionTokens(ExpressionTokenBuilderState $state) : void{ | ||
$state->current_group[$state->current_index] = new OpcodeExpressionToken($this->position, $this->code); | ||
} | ||
|
||
public function __debugInfo() : array{ | ||
$info = parent::__debugInfo(); | ||
$info["code"] = $this->code; | ||
return $info; | ||
} | ||
|
||
public function jsonSerialize() : string{ | ||
return self::opcodeToString($this->code); | ||
} | ||
} |
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