Skip to content

Commit cfd747f

Browse files
committed
Emit binary operations directly instead of deferring their initialization
See https://wiki.php.net/rfc/const_scalar_exprs
1 parent c5547f9 commit cfd747f

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/main/php/lang/ast/emit/PHP.class.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace lang\ast\emit;
22

33
use lang\ast\Code;
4-
use lang\ast\nodes\{InstanceExpression, ScopeExpression, Variable, Literal, ArrayLiteral, Block};
4+
use lang\ast\nodes\{InstanceExpression, ScopeExpression, BinaryExpression, Variable, Literal, ArrayLiteral, Block};
55
use lang\ast\types\{IsUnion, IsFunction, IsArray, IsMap};
66
use lang\ast\{Emitter, Node, Type};
77

@@ -37,8 +37,9 @@ protected function declaration($name) {
3737
* - Any literal
3838
* - Arrays where all members are literals
3939
* - Scope expressions with literal members (self::class, T::const)
40+
* - Binary expression where left- and right hand side are literals
4041
*
41-
* @see https://wiki.php.net/rfc/new_in_initializers
42+
* @see https://wiki.php.net/rfc/const_scalar_exprs
4243
* @param lang.ast.Node $node
4344
* @return bool
4445
*/
@@ -52,6 +53,8 @@ protected function isConstant($node) {
5253
return true;
5354
} else if ($node instanceof ScopeExpression) {
5455
return $node->member instanceof Literal;
56+
} else if ($node instanceof BinaryExpression) {
57+
return $this->isConstant($node->left) && $this->isConstant($node->right);
5558
}
5659
return false;
5760
}

src/test/php/lang/ast/unittest/emit/InitializeWithExpressionsTest.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
* @see https://github.com/xp-framework/compiler/pull/104
1010
* @see https://wiki.php.net/rfc/new_in_initializers
11+
* @see https://wiki.php.net/rfc/const_scalar_exprs
1112
* @see https://wiki.php.net/rfc/calls_in_constant_expressions
1213
*/
1314
class InitializeWithExpressionsTest extends EmittingTest {

0 commit comments

Comments
 (0)