Skip to content

Commit

Permalink
Added compiling for operator assign-bitwise only "assign-type: variable"
Browse files Browse the repository at this point in the history
  • Loading branch information
KorsaR-ZN committed Oct 2, 2015
1 parent 33c19eb commit 73c6cfb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Library/Expression/Builder/Operators/AssignVariableOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
57 changes: 57 additions & 0 deletions Library/Statements/LetStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 73c6cfb

Please sign in to comment.