Skip to content

Commit 4ede331

Browse files
committed
Fix "Compile error (instanceof expects an object instance, constant given)"
1 parent 7bddf75 commit 4ede331

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/main/php/lang/ast/syntax/php/IsOperator.class.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
use lang\ast\MapType;
66
use lang\ast\Node;
77
use lang\ast\UnionType;
8+
use lang\ast\nodes\Assignment;
89
use lang\ast\nodes\BinaryExpression;
10+
use lang\ast\nodes\Braced;
911
use lang\ast\nodes\InstanceOfExpression;
1012
use lang\ast\nodes\InvokeExpression;
1113
use lang\ast\nodes\Literal;
14+
use lang\ast\nodes\Variable;
1215
use lang\ast\syntax\Extension;
1316

1417
class IsOperator implements Extension {
@@ -22,7 +25,7 @@ public function setup($language, $emitter) {
2225
return $node;
2326
});
2427

25-
$test= function($literal, $expr) {
28+
$test= function($literal, $expr, $temp) {
2629
static $is= [
2730
'string' => true,
2831
'int' => true,
@@ -36,9 +39,9 @@ public function setup($language, $emitter) {
3639
// PHP 7.0 compatibility, is_iterable() doesn't exist there
3740
if ('iterable' === $literal) {
3841
return new BinaryExpression(
39-
new InstanceOfExpression($expr, '\Traversable'),
42+
new InstanceOfExpression($temp ? new Braced(new Assignment($temp, '=', $expr)) : $expr, '\Traversable'),
4043
'||',
41-
new InvokeExpression(new Literal('is_array'), [$expr])
44+
new InvokeExpression(new Literal('is_array'), [$temp])
4245
);
4346
} else if (isset($is[$literal])) {
4447
return new InvokeExpression(new Literal('is_'.$literal), [$expr]);
@@ -58,14 +61,15 @@ public function setup($language, $emitter) {
5861
return new InvokeExpression(new Literal('is'), [new Literal('"'.$t->name().'"'), $node->expression]);
5962
} else {
6063
$literal= $t->literal();
64+
$temp= new Variable($codegen->symbol());
6165
if ('?' === $literal[0]) {
6266
return new BinaryExpression(
63-
new BinaryExpression(new Literal('null'), '===', $node->expression),
67+
new BinaryExpression(new Literal('null'), '===', new Braced(new Assignment($temp, '=', $node->expression))),
6468
'||',
65-
$test(substr($literal, 1), $node->expression)
69+
$test(substr($literal, 1), $temp, null)
6670
);
6771
} else {
68-
return $test($literal, $node->expression);
72+
return $test($literal, $node->expression, $temp);
6973
}
7074
}
7175
});

0 commit comments

Comments
 (0)