Skip to content

Commit

Permalink
Merge pull request #2215 from organization/feat/assign-bitwise-operators
Browse files Browse the repository at this point in the history
#1103 - Added support syntax assign-bitwise operators
  • Loading branch information
Jeckerson authored Apr 21, 2021
2 parents cc2e91a + c38d4b3 commit 9378772
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org).
- Fixed nullable array [#1094](https://github.com/zephir-lang/zephir/issues/1094)
- Fixed default value detection with Reflection (only PHP 8.0) [#1134](https://github.com/zephir-lang/zephir/issues/1134)

### Added
- Added support syntax assign-bitwise operators [#1103](https://github.com/zephir-lang/zephir/issues/1103)

## [0.13.2] - 2021-04-10
### Fixed
- Fixed default value of nullable string parameter [#2180](https://github.com/zephir-lang/zephir/issues/2180)
Expand Down
17 changes: 17 additions & 0 deletions Library/Expression/Builder/Operators/AssignVariableOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ 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;
private $expression;
Expand Down
57 changes: 57 additions & 0 deletions Library/Statements/LetStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
use Zephir\Statements\Let\StaticPropertySub as LetStaticPropertySub;
use Zephir\Statements\Let\Variable as LetVariable;
use Zephir\Statements\Let\VariableAppend as LetVariableAppend;
use Zephir\Expression\Builder\BuilderFactory;
use Zephir\Expression\Builder\Operators\AssignVariableOperator;
use Zephir\Expression\Builder\Operators\BinaryOperator;

/**
* LetStatement.
Expand Down Expand Up @@ -88,6 +91,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 @@ -256,4 +265,52 @@ public function compile(CompilationContext $compilationContext)
}
}
}

/**
* @param array $assignment
* @return array
* @throws CompilerException
*/
protected function replaceAssignBitwiseOnDirect(array $assignment): array
{
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;
}
}
52 changes: 41 additions & 11 deletions stub/assign.zep
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,36 @@ class Assign
return this->myArray;
}

public function testAssignBitwiseX(int a, int b)
{
var op, i;
var result = [];

for op in ["or", "and", "xor", "shiftleft", "shiftright"] {
let i = a;
switch op {
case "or":
let i |= b;
break;
case "and":
let i &= b;
break;
case "xor":
let i ^= b;
break;
case "shiftleft":
let i <<= b;
break;
case "shiftright":
let i >>= b;
break;
}
let result[op] = i;
}

return result;
}

/**
* @link https://github.com/zephir-lang/zephir/issues/725
*/
Expand Down Expand Up @@ -942,18 +972,18 @@ class Assign

public function issue597()
{
if isset _POST["a"] {
if isset _GET["r"] {
// Nothing here
}
}
if isset _POST["a"] {
if isset _GET["r"] {
// Nothing here
}
}

if isset _GET["s"] {
var s;
let s = _GET["s"] * 5;
let _GET["s"] = s;
if isset _GET["s"] {
var s;
let s = _GET["s"] * 5;
let _GET["s"] = s;

return s;
}
return s;
}
}
}
1 change: 1 addition & 0 deletions tests/Extension/AssignTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public function propertyAssignProvider(): array
[$arry, 'testStaticPropertyArrayMulti4'],
[['test', 1, 1.5, false, []], 'testStaticPropertyArrayAppend'],
[['a' => true, 'b' => false], 'testArrayBoolExpressionAssign'],
[['or' => 901, 'and' => 4, 'xor' => 897, 'shiftleft' => 28800, 'shiftright' => 28], 'testAssignBitwiseX', [900, 5]],
];
}

Expand Down

0 comments on commit 9378772

Please sign in to comment.