Add support for constant scalar expressions (PHP 5.6)

This commit is contained in:
nikic 2014-03-26 21:48:12 +01:00
parent cda6f575f0
commit bea89a0bf2
3 changed files with 1263 additions and 559 deletions

View File

@ -746,15 +746,54 @@ common_scalar:
| name { $$ = Expr\ConstFetch[$1]; }
;
static_scalar: /* compile-time evaluated scalars */
common_scalar { $$ = $1; }
| class_name T_PAAMAYIM_NEKUDOTAYIM class_const_name { $$ = Expr\ClassConstFetch[$1, $3]; }
| '+' static_scalar { $$ = Expr\UnaryPlus[$2]; }
| '-' static_scalar { $$ = Expr\UnaryMinus[$2]; }
/* Arrays are currently not allowed in static scalar operations */
static_scalar:
static_scalar_value { $$ = $1; }
| T_ARRAY '(' static_array_pair_list ')' { $$ = Expr\Array_[$3]; }
| '[' static_array_pair_list ']' { $$ = Expr\Array_[$2]; }
;
static_scalar_value:
common_scalar { $$ = $1; }
| class_name T_PAAMAYIM_NEKUDOTAYIM class_const_name { $$ = Expr\ClassConstFetch[$1, $3]; }
| static_operation { $$ = $1; }
;
static_operation:
static_scalar_value T_BOOLEAN_OR static_scalar_value { $$ = Expr\BinaryOp\BooleanOr [$1, $3]; }
| static_scalar_value T_BOOLEAN_AND static_scalar_value { $$ = Expr\BinaryOp\BooleanAnd[$1, $3]; }
| static_scalar_value T_LOGICAL_OR static_scalar_value { $$ = Expr\BinaryOp\LogicalOr [$1, $3]; }
| static_scalar_value T_LOGICAL_AND static_scalar_value { $$ = Expr\BinaryOp\LogicalAnd[$1, $3]; }
| static_scalar_value T_LOGICAL_XOR static_scalar_value { $$ = Expr\BinaryOp\LogicalXor[$1, $3]; }
| static_scalar_value '|' static_scalar_value { $$ = Expr\BinaryOp\BitwiseOr [$1, $3]; }
| static_scalar_value '&' static_scalar_value { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
| static_scalar_value '^' static_scalar_value { $$ = Expr\BinaryOp\BitwiseXor[$1, $3]; }
| static_scalar_value '.' static_scalar_value { $$ = Expr\BinaryOp\Concat [$1, $3]; }
| static_scalar_value '+' static_scalar_value { $$ = Expr\BinaryOp\Plus [$1, $3]; }
| static_scalar_value '-' static_scalar_value { $$ = Expr\BinaryOp\Minus [$1, $3]; }
| static_scalar_value '*' static_scalar_value { $$ = Expr\BinaryOp\Mul [$1, $3]; }
| static_scalar_value '/' static_scalar_value { $$ = Expr\BinaryOp\Div [$1, $3]; }
| static_scalar_value '%' static_scalar_value { $$ = Expr\BinaryOp\Mod [$1, $3]; }
| static_scalar_value T_SL static_scalar_value { $$ = Expr\BinaryOp\ShiftLeft [$1, $3]; }
| static_scalar_value T_SR static_scalar_value { $$ = Expr\BinaryOp\ShiftRight[$1, $3]; }
| static_scalar_value T_POW static_scalar_value { $$ = Expr\BinaryOp\Pow [$1, $3]; }
| '+' static_scalar_value %prec T_INC { $$ = Expr\UnaryPlus [$2]; }
| '-' static_scalar_value %prec T_INC { $$ = Expr\UnaryMinus[$2]; }
| '!' static_scalar_value { $$ = Expr\BooleanNot[$2]; }
| '~' static_scalar_value { $$ = Expr\BitwiseNot[$2]; }
| static_scalar_value T_IS_IDENTICAL static_scalar_value { $$ = Expr\BinaryOp\Identical [$1, $3]; }
| static_scalar_value T_IS_NOT_IDENTICAL static_scalar_value { $$ = Expr\BinaryOp\NotIdentical [$1, $3]; }
| static_scalar_value T_IS_EQUAL static_scalar_value { $$ = Expr\BinaryOp\Equal [$1, $3]; }
| static_scalar_value T_IS_NOT_EQUAL static_scalar_value { $$ = Expr\BinaryOp\NotEqual [$1, $3]; }
| static_scalar_value '<' static_scalar_value { $$ = Expr\BinaryOp\Smaller [$1, $3]; }
| static_scalar_value T_IS_SMALLER_OR_EQUAL static_scalar_value { $$ = Expr\BinaryOp\SmallerOrEqual[$1, $3]; }
| static_scalar_value '>' static_scalar_value { $$ = Expr\BinaryOp\Greater [$1, $3]; }
| static_scalar_value T_IS_GREATER_OR_EQUAL static_scalar_value { $$ = Expr\BinaryOp\GreaterOrEqual[$1, $3]; }
| static_scalar_value '?' static_scalar_value ':' static_scalar_value { $$ = Expr\Ternary[$1, $3, $5]; }
| static_scalar_value '?' ':' static_scalar_value { $$ = Expr\Ternary[$1, null, $4]; }
| '(' static_scalar_value ')' { $$ = $2; }
;
scalar:
common_scalar { $$ = $1; }
| class_name_or_var T_PAAMAYIM_NEKUDOTAYIM class_const_name

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,470 @@
Expressions in static scalar context
-----
<?php
const T_1 = 1 << 1;
const T_2 = 1 / 2;
const T_3 = 1.5 + 1.5;
const T_4 = "foo" . "bar";
const T_5 = (1.5 + 1.5) * 2;
const T_6 = "foo" . 2 . 3 . 4.0;
const T_7 = __LINE__;
const T_8 = <<<ENDOFSTRING
This is a test string
ENDOFSTRING;
const T_9 = ~-1;
const T_10 = (-1?:1) + (0?2:3);
const T_11 = 1 && 0;
const T_12 = 1 and 1;
const T_13 = 0 || 0;
const T_14 = 1 or 0;
const T_15 = 1 xor 1;
const T_16 = 1 xor 0;
const T_17 = 1 < 0;
const T_18 = 0 <= 0;
const T_19 = 1 > 0;
const T_20 = 1 >= 0;
const T_21 = 1 === 1;
const T_22 = 1 !== 1;
const T_23 = 0 != "0";
const T_24 = 1 == "1";
const T_25 = 1 + 2 * 3;
const T_26 = "1" + 2 + "3";
const T_27 = 2 ** 3;
-----
array(
0: Stmt_Const(
consts: array(
0: Const(
name: T_1
value: Expr_BinaryOp_ShiftLeft(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 1
)
)
)
)
)
1: Stmt_Const(
consts: array(
0: Const(
name: T_2
value: Expr_BinaryOp_Div(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 2
)
)
)
)
)
2: Stmt_Const(
consts: array(
0: Const(
name: T_3
value: Expr_BinaryOp_Plus(
left: Scalar_DNumber(
value: 1.5
)
right: Scalar_DNumber(
value: 1.5
)
)
)
)
)
3: Stmt_Const(
consts: array(
0: Const(
name: T_4
value: Expr_BinaryOp_Concat(
left: Scalar_String(
value: foo
)
right: Scalar_String(
value: bar
)
)
)
)
)
4: Stmt_Const(
consts: array(
0: Const(
name: T_5
value: Expr_BinaryOp_Mul(
left: Expr_BinaryOp_Plus(
left: Scalar_DNumber(
value: 1.5
)
right: Scalar_DNumber(
value: 1.5
)
)
right: Scalar_LNumber(
value: 2
)
)
)
)
)
5: Stmt_Const(
consts: array(
0: Const(
name: T_6
value: Expr_BinaryOp_Concat(
left: Expr_BinaryOp_Concat(
left: Expr_BinaryOp_Concat(
left: Scalar_String(
value: foo
)
right: Scalar_LNumber(
value: 2
)
)
right: Scalar_LNumber(
value: 3
)
)
right: Scalar_DNumber(
value: 4
)
)
)
)
)
6: Stmt_Const(
consts: array(
0: Const(
name: T_7
value: Scalar_MagicConst_Line(
)
)
)
)
7: Stmt_Const(
consts: array(
0: Const(
name: T_8
value: Scalar_String(
value: This is a test string
)
)
)
)
8: Stmt_Const(
consts: array(
0: Const(
name: T_9
value: Expr_BitwiseNot(
expr: Expr_UnaryMinus(
expr: Scalar_LNumber(
value: 1
)
)
)
)
)
)
9: Stmt_Const(
consts: array(
0: Const(
name: T_10
value: Expr_BinaryOp_Plus(
left: Expr_Ternary(
cond: Expr_UnaryMinus(
expr: Scalar_LNumber(
value: 1
)
)
if: null
else: Scalar_LNumber(
value: 1
)
)
right: Expr_Ternary(
cond: Scalar_LNumber(
value: 0
)
if: Scalar_LNumber(
value: 2
)
else: Scalar_LNumber(
value: 3
)
)
)
)
)
)
10: Stmt_Const(
consts: array(
0: Const(
name: T_11
value: Expr_BinaryOp_BooleanAnd(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 0
)
)
)
)
)
11: Stmt_Const(
consts: array(
0: Const(
name: T_12
value: Expr_BinaryOp_LogicalAnd(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 1
)
)
)
)
)
12: Stmt_Const(
consts: array(
0: Const(
name: T_13
value: Expr_BinaryOp_BooleanOr(
left: Scalar_LNumber(
value: 0
)
right: Scalar_LNumber(
value: 0
)
)
)
)
)
13: Stmt_Const(
consts: array(
0: Const(
name: T_14
value: Expr_BinaryOp_LogicalOr(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 0
)
)
)
)
)
14: Stmt_Const(
consts: array(
0: Const(
name: T_15
value: Expr_BinaryOp_LogicalXor(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 1
)
)
)
)
)
15: Stmt_Const(
consts: array(
0: Const(
name: T_16
value: Expr_BinaryOp_LogicalXor(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 0
)
)
)
)
)
16: Stmt_Const(
consts: array(
0: Const(
name: T_17
value: Expr_BinaryOp_Smaller(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 0
)
)
)
)
)
17: Stmt_Const(
consts: array(
0: Const(
name: T_18
value: Expr_BinaryOp_SmallerOrEqual(
left: Scalar_LNumber(
value: 0
)
right: Scalar_LNumber(
value: 0
)
)
)
)
)
18: Stmt_Const(
consts: array(
0: Const(
name: T_19
value: Expr_BinaryOp_Greater(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 0
)
)
)
)
)
19: Stmt_Const(
consts: array(
0: Const(
name: T_20
value: Expr_BinaryOp_GreaterOrEqual(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 0
)
)
)
)
)
20: Stmt_Const(
consts: array(
0: Const(
name: T_21
value: Expr_BinaryOp_Identical(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 1
)
)
)
)
)
21: Stmt_Const(
consts: array(
0: Const(
name: T_22
value: Expr_BinaryOp_NotIdentical(
left: Scalar_LNumber(
value: 1
)
right: Scalar_LNumber(
value: 1
)
)
)
)
)
22: Stmt_Const(
consts: array(
0: Const(
name: T_23
value: Expr_BinaryOp_NotEqual(
left: Scalar_LNumber(
value: 0
)
right: Scalar_String(
value: 0
)
)
)
)
)
23: Stmt_Const(
consts: array(
0: Const(
name: T_24
value: Expr_BinaryOp_Equal(
left: Scalar_LNumber(
value: 1
)
right: Scalar_String(
value: 1
)
)
)
)
)
24: Stmt_Const(
consts: array(
0: Const(
name: T_25
value: Expr_BinaryOp_Plus(
left: Scalar_LNumber(
value: 1
)
right: Expr_BinaryOp_Mul(
left: Scalar_LNumber(
value: 2
)
right: Scalar_LNumber(
value: 3
)
)
)
)
)
)
25: Stmt_Const(
consts: array(
0: Const(
name: T_26
value: Expr_BinaryOp_Plus(
left: Expr_BinaryOp_Plus(
left: Scalar_String(
value: 1
)
right: Scalar_LNumber(
value: 2
)
)
right: Scalar_String(
value: 3
)
)
)
)
)
26: Stmt_Const(
consts: array(
0: Const(
name: T_27
value: Expr_BinaryOp_Pow(
left: Scalar_LNumber(
value: 2
)
right: Scalar_LNumber(
value: 3
)
)
)
)
)
)