From 73fe866fa26cf8605073d56363e2ec0074d9769d Mon Sep 17 00:00:00 2001 From: Muqsit Date: Fri, 12 Jul 2024 18:15:18 +0000 Subject: [PATCH] fix: spaceship opcode cannot be stringified --- src/muqsit/arithmexp/token/OpcodeToken.php | 1 + tests/muqsit/arithmexp/ParserTest.php | 21 +++++++++++++++++++++ virion.yml | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/muqsit/arithmexp/token/OpcodeToken.php b/src/muqsit/arithmexp/token/OpcodeToken.php index 00c05be..b744c81 100644 --- a/src/muqsit/arithmexp/token/OpcodeToken.php +++ b/src/muqsit/arithmexp/token/OpcodeToken.php @@ -57,6 +57,7 @@ public static function opcodeToString(int $code) : string{ self::OP_BINARY_MUL => "*", self::OP_BINARY_OR_SYMBOL => "||", self::OP_BINARY_OR_TEXTUAL => "or", + self::OP_BINARY_SPACESHIP => "<=>", self::OP_BINARY_SUB, self::OP_UNARY_NVE => "-", self::OP_BINARY_XOR => "xor", self::OP_UNARY_NOT => "!" diff --git a/tests/muqsit/arithmexp/ParserTest.php b/tests/muqsit/arithmexp/ParserTest.php index 2010e26..b85f8e2 100644 --- a/tests/muqsit/arithmexp/ParserTest.php +++ b/tests/muqsit/arithmexp/ParserTest.php @@ -15,9 +15,17 @@ use muqsit\arithmexp\token\FunctionCallToken; use muqsit\arithmexp\token\IdentifierToken; use muqsit\arithmexp\token\NumericLiteralToken; +use muqsit\arithmexp\token\OpcodeToken; use muqsit\arithmexp\token\Token; use muqsit\arithmexp\token\UnaryOperatorToken; use PHPUnit\Framework\TestCase; +use ReflectionClass; +use RuntimeException; +use UnhandledMatchError; +use function array_filter; +use function str_starts_with; +use function strlen; +use const ARRAY_FILTER_USE_KEY; use const M_PI; final class ParserTest extends TestCase{ @@ -209,6 +217,19 @@ public function testOperatorNonAssociativity() : void{ TestUtil::assertExpressionsEqual($this->uo_parser->parse("(x > y) == (x < z)"), $this->uo_parser->parse("x > y == x < z")); } + public function testOpcodeTokenToString() : void{ + $codes = (new ReflectionClass(OpcodeToken::class))->getConstants(); + $codes = array_filter($codes, fn(string $key) => str_starts_with($key, "OP_"), ARRAY_FILTER_USE_KEY); + foreach($codes as $name => $code){ + try{ + $string = OpcodeToken::opcodeToString($code); + }catch(UnhandledMatchError $e){ + throw new RuntimeException("Failed to stringify opcode {$name}", $e->getCode(), $e); + } + $this->assertGreaterThan(0, strlen($string)); + } + } + public function testBadFunctionCallToUndefinedFunction() : void{ TestUtil::assertParserThrows($this->parser, "x * fn() / 3", ParseException::ERR_UNRESOLVABLE_FCALL, 4, 8); } diff --git a/virion.yml b/virion.yml index 240482f..5f97375 100644 --- a/virion.yml +++ b/virion.yml @@ -1,5 +1,5 @@ name: arithmexp antigen: muqsit\arithmexp api: [4.21.1, 5.0.0] -version: 0.1.30 +version: 1.0.1 author: Muqsit