1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Compiler\Expression\BinaryOp; |
7
|
|
|
|
8
|
|
|
use PHPSA\CompiledExpression; |
9
|
|
|
use PHPSA\Compiler\Expression\AbstractExpressionCompiler; |
10
|
|
|
use PHPSA\Context; |
11
|
|
|
|
12
|
|
|
class SpaceShip extends AbstractExpressionCompiler |
13
|
|
|
{ |
14
|
|
|
protected $name = 'PhpParser\Node\Expr\BinaryOp\Spaceship'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* It's used in conditions |
18
|
|
|
* {left-expr} <=> {right-expr} |
19
|
|
|
* |
20
|
|
|
* @param \PhpParser\Node\Expr\BinaryOp\Spaceship $expr |
21
|
|
|
* @param Context $context |
22
|
|
|
* @return CompiledExpression |
23
|
|
|
*/ |
24
|
11 |
|
protected function compile($expr, Context $context) |
25
|
|
|
{ |
26
|
11 |
|
$left = $context->getExpressionCompiler()->compile($expr->left); |
27
|
11 |
|
$right = $context->getExpressionCompiler()->compile($expr->right); |
28
|
|
|
|
29
|
11 |
|
switch ($left->getType()) { |
30
|
11 |
|
case CompiledExpression::INTEGER: |
31
|
11 |
|
case CompiledExpression::DOUBLE: |
32
|
11 |
|
case CompiledExpression::BOOLEAN: |
33
|
11 |
|
case CompiledExpression::NUMBER: |
34
|
11 |
|
case CompiledExpression::NULL: |
35
|
11 |
|
case CompiledExpression::ARR: |
36
|
11 |
|
case CompiledExpression::OBJECT: |
37
|
11 |
|
case CompiledExpression::STRING: |
38
|
10 |
|
switch ($right->getType()) { |
39
|
10 |
|
case CompiledExpression::INTEGER: |
40
|
10 |
|
case CompiledExpression::DOUBLE: |
41
|
10 |
|
case CompiledExpression::BOOLEAN: |
42
|
10 |
|
case CompiledExpression::NUMBER: |
43
|
10 |
|
case CompiledExpression::NULL: |
44
|
10 |
|
case CompiledExpression::ARR: |
45
|
10 |
|
case CompiledExpression::OBJECT: |
46
|
10 |
|
case CompiledExpression::STRING: |
47
|
9 |
|
if ($left->getValue() == $right->getValue()) { |
48
|
2 |
|
return new CompiledExpression(CompiledExpression::INTEGER, 0); |
49
|
7 |
|
} elseif ($left->getValue() < $right->getValue()) { |
50
|
3 |
|
return new CompiledExpression(CompiledExpression::INTEGER, -1); |
51
|
4 |
|
} elseif ($left->getValue() > $right->getValue()) { |
52
|
4 |
|
return new CompiledExpression(CompiledExpression::INTEGER, 1); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
2 |
|
} |
56
|
|
|
|
57
|
2 |
|
return new CompiledExpression(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|