diff --git a/Library/Operators/Other/CastOperator.php b/Library/Operators/Other/CastOperator.php index 77f518e6ef..2b8a6fb3bd 100644 --- a/Library/Operators/Other/CastOperator.php +++ b/Library/Operators/Other/CastOperator.php @@ -53,6 +53,10 @@ public function compile(array $expression, CompilationContext $compilationContex case Types::T_NULL: return new CompiledExpression('int', 0, $expression); + case Types::T_CHAR: + case Types::T_UCHAR: + return new CompiledExpression('int', "'{$resolved->getCode()}'", $expression); + case Types::T_INT: return new CompiledExpression('int', $resolved->getCode(), $expression); @@ -115,6 +119,7 @@ public function compile(array $expression, CompilationContext $compilationContex switch ($symbolVariable->getType()) { case Types::T_INT: case Types::T_CHAR: + case Types::T_UCHAR: return new CompiledExpression('int', $symbolVariable->getName(), $expression); case Types::T_DOUBLE: @@ -166,6 +171,14 @@ public function compile(array $expression, CompilationContext $compilationContex case Types::T_INT: return new CompiledExpression('long', $resolved->getCode(), $expression); + case Types::T_CHAR: + case Types::T_UCHAR: + return new CompiledExpression( + 'long', + "(long) '{$resolved->getCode()}'", + $expression + ); + case Types::T_DOUBLE: return new CompiledExpression('long', '(long) '.$resolved->getCode(), $expression); @@ -196,6 +209,7 @@ public function compile(array $expression, CompilationContext $compilationContex switch ($symbolVariable->getType()) { case Types::T_INT: case Types::T_CHAR: + case Types::T_UCHAR: return new CompiledExpression('long', $symbolVariable->getName(), $expression); case Types::T_DOUBLE: @@ -247,6 +261,16 @@ public function compile(array $expression, CompilationContext $compilationContex case Types::T_BOOL: return new CompiledExpression('double', $resolved->getBooleanCode(), $expression); + case Types::T_CHAR: + case Types::T_UCHAR: + return new CompiledExpression( + 'double', + "(double) '{$resolved->getCode()}'", + $expression + ); + + case Types::T_INT: + case Types::T_LONG: case Types::T_DOUBLE: return new CompiledExpression('double', $resolved->getCode(), $expression); @@ -276,7 +300,12 @@ public function compile(array $expression, CompilationContext $compilationContex switch ($symbolVariable->getType()) { case Types::T_INT: case Types::T_CHAR: - return new CompiledExpression('double', $symbolVariable->getName(), $expression); + case Types::T_UCHAR: + return new CompiledExpression( + 'double', + "(double) {$symbolVariable->getName()}", + $expression + ); case Types::T_DOUBLE: case Types::T_BOOL: @@ -330,6 +359,14 @@ public function compile(array $expression, CompilationContext $compilationContex $expression ); + case Types::T_CHAR: + case Types::T_UCHAR: + return new CompiledExpression( + 'bool', + "(zend_bool) '{$resolved->getCode()}'", + $expression + ); + case Types::T_BOOL: return new CompiledExpression('bool', $resolved->getCode(), $expression); @@ -348,6 +385,7 @@ public function compile(array $expression, CompilationContext $compilationContex switch ($symbolVariable->getType()) { case Types::T_INT: case Types::T_CHAR: + case Types::T_UCHAR: return new CompiledExpression( 'bool', sprintf('(zend_bool) %s', $symbolVariable->getName()), @@ -388,6 +426,12 @@ public function compile(array $expression, CompilationContext $compilationContex case Types::T_CHAR: switch ($resolved->getType()) { + case Types::T_UCHAR: + return new CompiledExpression('uchar', "'{$resolved->getCode()}'", $expression); + + case Types::T_CHAR: + return new CompiledExpression('char', "'{$resolved->getCode()}'", $expression); + case Types::T_VARIABLE: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( @@ -402,6 +446,7 @@ public function compile(array $expression, CompilationContext $compilationContex ); switch ($symbolVariable->getType()) { + // TODO: uchar case Types::T_CHAR: $compilationContext->codePrinter->output( sprintf('%s = %s;', $tempVariable->getName(), $symbolVariable->getName()) @@ -433,6 +478,10 @@ public function compile(array $expression, CompilationContext $compilationContex case Types::T_STRING: switch ($resolved->getType()) { + case Types::T_UCHAR: + case Types::T_CHAR: + return new CompiledExpression('string', $resolved->getCode(), $expression); + case Types::T_VARIABLE: $compilationContext->headersManager->add('kernel/operators'); $compilationContext->symbolTable->mustGrownStack(true); diff --git a/test/cast.zep b/test/cast.zep index 8d3774a14b..064cdd62ca 100644 --- a/test/cast.zep +++ b/test/cast.zep @@ -14,8 +14,7 @@ class Cast */ public function testCharCastFromChar() -> char { - char a = 'a'; - return (char) a; + return (char) 'a'; } /** @@ -29,6 +28,14 @@ class Cast // To string cast + /** + * @see https://github.com/phalcon/zephir/issues/1988 + */ + public function testStringCastChar() -> string + { + return (string) 'z'; + } + /** * @see https://github.com/phalcon/zephir/issues/1988 */ @@ -51,6 +58,14 @@ class Cast return (int) a; } + /** + * @see https://github.com/phalcon/zephir/issues/1988 + */ + public function testIntCastFromChar() -> int + { + return (int) 'A'; + } + /** * @see https://github.com/phalcon/zephir/issues/1988 */ @@ -155,6 +170,14 @@ class Cast // To long cast + /** + * @see https://github.com/phalcon/zephir/issues/1988 + */ + public function testLongCastFromChar() -> long + { + return (long) 'a'; + } + /** * @see https://github.com/phalcon/zephir/issues/1988 */ @@ -254,6 +277,14 @@ class Cast // To double cast + /** + * @see https://github.com/phalcon/zephir/issues/1988 + */ + public function testDoubleCastFromVChar() -> double + { + return (double) 'a'; + } + /** * @see https://github.com/phalcon/zephir/issues/1988 */ @@ -305,6 +336,14 @@ class Cast return (boolean) a; } + /** + * @see https://github.com/phalcon/zephir/issues/1988 + */ + public function testBooleanCastFromChar() -> boolean + { + return (boolean) 'a'; + } + /** * @see https://github.com/phalcon/zephir/issues/1988 */ diff --git a/unit-tests/Extension/CastTest.php b/unit-tests/Extension/CastTest.php index 54a7281c1c..83f38a8e94 100644 --- a/unit-tests/Extension/CastTest.php +++ b/unit-tests/Extension/CastTest.php @@ -37,15 +37,27 @@ public function tearDown() public function testCharCast() { /* - * Variable types + * Value */ $this->assertSame(97, $this->test->testCharCastFromChar()); + + /* + * Variable types + */ + $this->assertSame(65, $this->test->testCharCastFromVariableChar()); } public function testStringCast() { + /* + * Value + */ + + // https://github.com/phalcon/zephir/issues/1988 + $this->assertSame('z', $this->test->testStringCastChar()); + /* * Variable types */ @@ -68,6 +80,7 @@ public function testIntCast() $this->assertSame(0, $this->test->testIntCastFromEmptyArray()); $this->assertSame(1, $this->test->testIntCastFromArray()); $this->assertSame(1, $this->test->testIntCastFromStdClass()); + $this->assertSame(65, $this->test->testIntCastFromChar()); /* * Variable types @@ -95,6 +108,13 @@ public function testIntCast() public function testLongCast() { + /* + * Value + */ + + // https://github.com/phalcon/zephir/issues/1988 + $this->assertSame(97, $this->test->testLongCastFromChar()); + /* * Variable types */ @@ -124,12 +144,19 @@ public function testFloatCast() public function testDoubleCast() { + /* + * Value + */ + + // https://github.com/phalcon/zephir/issues/1988 + $this->assertSame(97.0, $this->test->testDoubleCastFromVChar()); + /* * Variable types */ // https://github.com/phalcon/zephir/issues/1988 - $this->assertSame(65, $this->test->testLongCastFromVariableChar()); + $this->assertSame(65.0, $this->test->testDoubleCastFromVariableChar()); } public function testBooleanCast() @@ -142,6 +169,9 @@ public function testBooleanCast() $this->assertTrue($this->test->testBooleanCastFromIntTrue2()); $this->assertFalse($this->test->testBooleanCastFromIntFalse()); + // https://github.com/phalcon/zephir/issues/1988 + $this->assertTrue($this->test->testBooleanCastFromChar()); + /* * Variable types */