Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed concatenation support of strings with double numbers #1905

Merged
merged 3 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- Fixed concatenation support of strings with double numbers
[#1893](https://github.com/phalcon/zephir/issues/1893)

## [0.12.2] - 2019-08-05
### Added
Expand Down
146 changes: 104 additions & 42 deletions Library/Operators/Other/ConcatOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function Zephir\add_slashes;
use Zephir\CompilationContext;
use Zephir\CompiledExpression;
use Zephir\Exception;
use Zephir\Exception\CompilerException;
use Zephir\Expression;
use Zephir\Operators\BaseOperator;
Expand All @@ -32,6 +33,8 @@ class ConcatOperator extends BaseOperator
* @param CompilationContext $compilationContext
*
* @return CompiledExpression
*
* @throws CompilerException
*/
public function compile($expression, CompilationContext $compilationContext)
{
Expand All @@ -58,7 +61,9 @@ public function compile($expression, CompilationContext $compilationContext)

$expected->setDynamicTypes('string');
$expectedCode = $compilationContext->backend->getVariableCode($expected);
$compilationContext->codePrinter->output('ZEPHIR_CONCAT_'.strtoupper($optimized[0]).'('.$expectedCode.', '.$optimized[1].');');
$compilationContext->codePrinter->output(
sprintf('ZEPHIR_CONCAT_%s(%s, %s);', strtoupper($optimized[0]), $expectedCode, $optimized[1])
);

return new CompiledExpression('variable', $expected->getName(), $expression);
}
Expand All @@ -67,35 +72,15 @@ public function compile($expression, CompilationContext $compilationContext)
* If the expression cannot be optimized, fall back to the standard compilation.
*/
$leftExpr = new Expression($expression['left']);
switch ($expression['left']['type']) {
case 'array-access':
case 'property-access':
$leftExpr->setReadOnly(true);
break;

default:
$leftExpr->setReadOnly($this->readOnly);
break;
}
$left = $leftExpr->compile($compilationContext);
$left = $this->compileExpression($leftExpr, $compilationContext, $expression['left']['type']);

if ('variable' == $left->getType()) {
$variableLeft = $compilationContext->symbolTable->getVariableForRead($left->getCode(), $compilationContext, $expression['right']);
$variableLeft = $compilationContext->backend->getVariableCode($variableLeft);
}

$rightExpr = new Expression($expression['right']);
switch ($expression['left']['type']) {
case 'array-access':
case 'property-access':
$rightExpr->setReadOnly(true);
break;

default:
$rightExpr->setReadOnly($this->readOnly);
break;
}
$right = $rightExpr->compile($compilationContext);
$right = $this->compileExpression($rightExpr, $compilationContext, $expression['left']['type']);

if ('variable' == $right->getType()) {
$variableRight = $compilationContext->symbolTable->getVariableForRead($right->getCode(), $compilationContext, $expression['right']);
Expand Down Expand Up @@ -128,11 +113,12 @@ public function compile($expression, CompilationContext $compilationContext)
* @param bool $isFullString
*
* @return array
*
* @throws CompilerException
*/
private function _getOptimizedConcat($expression, CompilationContext $compilationContext, &$isFullString)
{
$originalExpr = $expression;

$isFullString = true;

$parts = [];
Expand All @@ -156,21 +142,15 @@ private function _getOptimizedConcat($expression, CompilationContext $compilatio
foreach ($parts as $part) {
$expr = new Expression($part);
$expr->setStringOperation(true);
switch ($part['type']) {
case 'array-access':
case 'property-access':
$expr->setReadOnly(true);
break;
$compiledExpr = $this->compileExpression($expr, $compilationContext, $part['type']);

default:
$expr->setReadOnly($this->readOnly);
break;
}

$compiledExpr = $expr->compile($compilationContext);
switch ($compiledExpr->getType()) {
case 'variable':
$variable = $compilationContext->symbolTable->getVariableForRead($compiledExpr->getCode(), $compilationContext, $originalExpr);
$variable = $compilationContext->symbolTable->getVariableForRead(
$compiledExpr->getCode(),
$compilationContext,
$originalExpr
);
switch ($variable->getType()) {
case 'variable':
$key .= 'v';
Expand All @@ -184,15 +164,44 @@ private function _getOptimizedConcat($expression, CompilationContext $compilatio
break;

case 'int':
case 'uint':
case 'long':
case 'ulong':
$key .= 'v';
$tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignLong(
$tempVariable,
$compiledExpr->getCode(),
$compilationContext
);
$concatParts[] = $compilationContext->backend->getVariableCode($tempVariable);
break;

case 'double':
$key .= 'v';
$tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite('variable', $compilationContext, $originalExpr);
$compilationContext->backend->assignLong($tempVariable, $compiledExpr->getCode(), $compilationContext);
$tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignDouble(
$tempVariable,
$compiledExpr->getCode(),
$compilationContext
);
$concatParts[] = $compilationContext->backend->getVariableCode($tempVariable);
break;

default:
throw new CompilerException('Variable type: '.$variable->getType().' cannot be used in concat operation', $compiledExpr->getOriginal());
throw new CompilerException(
sprintf(
'Variable type: %s cannot be used in concat operation',
$variable->getType()
),
$compiledExpr->getOriginal()
);
}
break;

Expand All @@ -202,20 +211,73 @@ private function _getOptimizedConcat($expression, CompilationContext $compilatio
break;

case 'int':
case 'uint':
case 'long':
case 'ulong':
$key .= 'v';
$tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite('variable', $compilationContext, $originalExpr);
$compilationContext->codePrinter->output('ZVAL_LONG(&'.$tempVariable->getName().', '.$compiledExpr->getCode().');');
$tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->codePrinter->output(
sprintf(
'ZVAL_LONG(&%s, %s);',
$tempVariable->getName(),
$compiledExpr->getCode()
)
);
$concatParts[] = '&'.$tempVariable->getName();
break;

case 'double':
$key .= 'v';
$tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->codePrinter->output(
sprintf(
'ZVAL_DOUBLE(&%s, %s);',
$tempVariable->getName(),
$compiledExpr->getCode()
)
);
$concatParts[] = '&'.$tempVariable->getName();
break;

default:
throw new CompilerException('Variable type: '.$compiledExpr->getType().' cannot be used in concat operation', $compiledExpr->getOriginal());
throw new CompilerException(
sprintf(
'Variable type: %s cannot be used in concat operation',
$compiledExpr->getType()
),
$compiledExpr->getOriginal()
);
}
}

$compilationContext->stringsManager->addConcatKey($key);

return [$key, implode(', ', $concatParts)];
}

private function compileExpression(Expression $expression, CompilationContext $context, $type)
{
try {
switch ($type) {
case 'array-access':
case 'property-access':
$expression->setReadOnly(true);
break;

default:
$expression->setReadOnly($this->readOnly);
break;
}

return $expression->compile($context);
} catch (Exception $e) {
throw new CompilerException($e->getMessage(), $expression->getExpression());
}
}
}
2 changes: 1 addition & 1 deletion Library/Zephir.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
final class Zephir
{
const VERSION = '0.12.2-$Id$';
const VERSION = '0.12.3-$Id$';

const LOGO = <<<'ASCII'
_____ __ _
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.12.2-{build}
version: 0.12.3-{build}

environment:
matrix:
Expand Down
72 changes: 72 additions & 0 deletions ext/test/concat.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions ext/test/concat.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading