From eb7b9d585e5832a691e84df7cc9a3e9e0277b5e8 Mon Sep 17 00:00:00 2001 From: Stanislav Kiryukhin Date: Fri, 2 Oct 2015 20:46:01 +0300 Subject: [PATCH] Added compiling for operator assign-bitwise only "assign-type: variable" --- .../Operators/AssignVariableOperator.php | 16 ++++++ Library/Statements/LetStatement.php | 57 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/Library/Expression/Builder/Operators/AssignVariableOperator.php b/Library/Expression/Builder/Operators/AssignVariableOperator.php index 40c80a2e94..daba3f7367 100644 --- a/Library/Expression/Builder/Operators/AssignVariableOperator.php +++ b/Library/Expression/Builder/Operators/AssignVariableOperator.php @@ -46,6 +46,22 @@ class AssignVariableOperator extends AbstractOperator // %= const OPERATOR_MOD = 'mod-assign'; + // &= + const OPERATOR_BITWISE_AND = 'bitwise-and-assign'; + + // |= + const OPERATOR_BITWISE_OR = 'bitwise-or-assign'; + + // ^= + const OPERATOR_BITWISE_XOR = 'bitwise-xor-assign'; + + // <<= + const OPERATOR_BITWISE_SHIFTLEFT = 'bitwise-shiftleft-assign'; + + // >>= + const OPERATOR_BITWISE_SHIFTRIGHT = 'bitwise-shiftright-assign'; + + private $variable; private $operator = self::OPERATOR_ASSIGN; diff --git a/Library/Statements/LetStatement.php b/Library/Statements/LetStatement.php index 4a72254861..d590bdc416 100644 --- a/Library/Statements/LetStatement.php +++ b/Library/Statements/LetStatement.php @@ -48,6 +48,9 @@ use Zephir\Statements\Let\Incr as LetIncr; use Zephir\Statements\Let\ExportSymbol as LetExportSymbol; use Zephir\Statements\Let\ExportSymbolString as LetExportSymbolString; +use Zephir\Expression\Builder\BuilderFactory; +use Zephir\Expression\Builder\Operators\AssignVariableOperator; +use Zephir\Expression\Builder\Operators\BinaryOperator; /** * LetStatement @@ -98,6 +101,12 @@ public function compile(CompilationContext $compilationContext) * Incr/Decr assignments don't require an expression */ if (isset($assignment['expr'])) { + /** + * Replace on direct-assignment if this bitwise-assignment + * @Todo: Replace on supported native bitwise-assignment + */ + $assignment = $this->replaceAssignBitwiseOnDirect($assignment); + $expr = new Expression($assignment['expr']); switch ($assignment['assign-type']) { @@ -249,4 +258,52 @@ public function compile(CompilationContext $compilationContext) } } } + + /** + * @param $assignment + * @return mixed + * @throws CompilerException + */ + protected function replaceAssignBitwiseOnDirect($assignment) + { + switch ($assignment['operator']) { + case AssignVariableOperator::OPERATOR_BITWISE_AND: + $operator = BinaryOperator::OPERATOR_BITWISE_AND; + break; + + case AssignVariableOperator::OPERATOR_BITWISE_OR: + $operator = BinaryOperator::OPERATOR_BITWISE_OR; + break; + + case AssignVariableOperator::OPERATOR_BITWISE_XOR: + $operator = BinaryOperator::OPERATOR_BITWISE_XOR; + break; + + case AssignVariableOperator::OPERATOR_BITWISE_SHIFTLEFT: + $operator = BinaryOperator::OPERATOR_BITWISE_SHIFT_LEFT; + break; + + case AssignVariableOperator::OPERATOR_BITWISE_SHIFTRIGHT: + $operator = BinaryOperator::OPERATOR_BITWISE_SHIFT_RIGHT; + break; + + default: + return $assignment; + } + + if ($assignment['assign-type'] != 'variable') { + throw new CompilerException("Operator '" . $assignment['operator'] . "' is not supported assign-type: " . $assignment['assign-type']); + } + + $builderExpr = BuilderFactory::getInstance(); + + $leftExpression = $builderExpr->variable($assignment['variable']); + + $assignment['expr'] = $builderExpr->operators() + ->binary($operator, $leftExpression, $builderExpr->raw($assignment['expr'])) + ->build(); + + $assignment['operator'] = AssignVariableOperator::OPERATOR_ASSIGN; + return $assignment; + } }