88db742eb6df4329e293b5c15c6886eb8694c740
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
26 #include "php_psi_stdinc.h"
34 struct psi_num_exp
*psi_num_exp_init(token_t t
, void *num
)
36 struct psi_num_exp
*exp
= calloc(1, sizeof(*exp
));
38 switch (exp
->type
= t
) {
41 exp
->data
.numb
= strdup(num
);
53 struct psi_num_exp
*psi_num_exp_copy(struct psi_num_exp
*exp
)
55 struct psi_num_exp
*num
= calloc(1, sizeof(*num
));
60 num
->token
= psi_token_copy(num
->token
);
63 num
->operand
= psi_num_exp_copy(num
->operand
);
74 num
->data
.numb
= strdup(num
->data
.numb
);
77 num
->data
.dvar
= psi_decl_var_copy(num
->data
.dvar
);
85 void psi_num_exp_free(struct psi_num_exp
**exp_ptr
)
88 struct psi_num_exp
*exp
= *exp_ptr
;
102 free(exp
->data
.numb
);
105 psi_decl_var_free(&exp
->data
.dvar
);
111 psi_num_exp_free(&exp
->operand
);
117 static inline wint_t psi_num_exp_op_tok(token_t op
)
132 case PSI_T_AMPERSAND
:
144 void psi_num_exp_dump(int fd
, struct psi_num_exp
*exp
)
149 dprintf(fd
, "%" PRId64
, exp
->data
.ival
.i64
);
152 dprintf(fd
, "%F", exp
->data
.ival
.dval
);
156 dprintf(fd
, "%s", exp
->data
.numb
);
159 dprintf(fd
, "%s", exp
->data
.cnst
->name
);
162 dprintf(fd
, "%s", exp
->data
.enm
->name
);
165 psi_decl_var_dump(fd
, exp
->data
.dvar
);
172 dprintf(fd
, " %c ", psi_num_exp_op_tok(exp
->op
));
179 static inline bool psi_num_exp_validate_enum(struct psi_data
*data
, struct psi_num_exp
*exp
,
180 struct psi_decl_enum
*enm
)
183 struct psi_decl_enum_item
*itm
;
185 while (psi_plist_get(enm
->items
, i
++, &itm
)) {
186 if (!strcmp(itm
->name
, exp
->data
.dvar
->name
)) {
187 psi_decl_var_free(&exp
->data
.dvar
);
188 exp
->type
= PSI_T_ENUM
;
190 return psi_num_exp_validate(data
, exp
, NULL
, NULL
, NULL
, NULL
, enm
);
197 bool psi_num_exp_validate(struct psi_data
*data
, struct psi_num_exp
*exp
,
198 struct psi_impl
*impl
, struct psi_decl
*cb_decl
, struct psi_let_exp
*current_let
,
199 struct psi_set_exp
*current_set
, struct psi_decl_enum
*current_enum
)
203 struct psi_const
*cnst
;
204 struct psi_decl_enum
*enm
;
209 exp
->calc
= psi_calc_bin_lshift
;
212 exp
->calc
= psi_calc_bin_rshift
;
215 exp
->calc
= psi_calc_add
;
218 exp
->calc
= psi_calc_sub
;
221 exp
->calc
= psi_calc_mul
;
224 exp
->calc
= psi_calc_div
;
226 case PSI_T_AMPERSAND
:
227 exp
->calc
= psi_calc_bin_and
;
230 exp
->calc
= psi_calc_bin_xor
;
233 exp
->calc
= psi_calc_bin_or
;
236 data
->error(data
, exp
->token
, PSI_WARNING
,
237 "Unknown numeric operator (%d)", exp
->op
);
240 if (!psi_num_exp_validate(data
, exp
->operand
, impl
, cb_decl
, current_let
,
241 current_set
, current_enum
)) {
254 if (current_enum
&& psi_num_exp_validate_enum(data
, exp
, current_enum
)) {
257 while (psi_plist_get(data
->enums
, i
++, &enm
)) {
258 if (psi_num_exp_validate_enum(data
, exp
, enm
)) {
262 if (exp
->data
.dvar
->arg
) {
265 if (psi_decl_var_validate(data
, exp
->data
.dvar
, impl
? impl
->decl
: NULL
,
266 current_let
, current_set
)) {
269 if (cb_decl
&& psi_decl_var_validate(data
, exp
->data
.dvar
, cb_decl
, NULL
, NULL
)) {
272 data
->error(data
, exp
->token
, PSI_WARNING
,
273 "Unknown variable '%s' in numeric expression",
274 exp
->data
.dvar
->name
);
278 while (psi_plist_get(data
->consts
, i
++, &cnst
)) {
279 if (!strcmp(cnst
->name
, exp
->data
.numb
)) {
280 free(exp
->data
.numb
);
281 exp
->type
= PSI_T_CONST
;
282 exp
->data
.cnst
= cnst
;
286 data
->error(data
, exp
->token
, PSI_WARNING
,
287 "Unknown constant '%s' in numeric expression",
292 switch (is_numeric_string(exp
->data
.numb
, strlen(exp
->data
.numb
), (zend_long
*) &tmp
, (double *) &tmp
, 0)) {
294 free(exp
->data
.numb
);
295 exp
->type
= PSI_T_INT64
;
296 exp
->data
.ival
.i64
= tmp
.zend
.lval
;
300 free(exp
->data
.numb
);
301 exp
->type
= PSI_T_DOUBLE
;
302 exp
->data
.ival
.dval
= tmp
.dval
;
305 data
->error(data
, exp
->token
, PSI_WARNING
, "Expected numeric entity (parser error?)");
315 #include "Zend/zend_constants.h"
317 static inline token_t
psi_num_exp_eval_constant(struct psi_num_exp
*exp
,
318 impl_val
*res
, struct psi_call_frame
*frame
)
320 switch (exp
->data
.cnst
->type
->type
) {
322 res
->i64
= zend_get_constant_str(exp
->data
.cnst
->name
,
323 strlen(exp
->data
.cnst
->name
))->value
.lval
;
324 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIi64
, res
->i64
);
327 res
->dval
= zend_get_constant_str(exp
->data
.cnst
->name
,
328 strlen(exp
->data
.cnst
->name
))->value
.dval
;
329 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIdval
, res
->dval
);
332 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "?(t=%d)", exp
->data
.cnst
->type
->type
);
337 static inline void psi_num_exp_verify_result(token_t t
, impl_val
*res
, struct psi_call_frame
*frame
)
342 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIi8
, res
->i8
);
346 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIi16
, res
->i16
);
350 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIi32
, res
->i32
);
354 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIi64
, res
->i64
);
357 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIfval
, res
->fval
);
360 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIdval
, res
->dval
);
367 static inline token_t
psi_num_exp_eval_decl_var(struct psi_num_exp
*exp
,
368 impl_val
*res
, struct psi_call_frame
*frame
)
371 struct psi_call_frame_symbol
*sym
;
372 struct psi_decl_type
*real
;
375 real
= psi_decl_type_get_real(exp
->data
.dvar
->arg
->type
);
376 size
= psi_decl_arg_get_size(exp
->data
.dvar
->arg
);
377 sym
= psi_call_frame_fetch_symbol(frame
, exp
->data
.dvar
);
378 ref
= deref_impl_val(sym
->ptr
, exp
->data
.dvar
);
380 memcpy(res
, ref
, size
);
385 static inline token_t
psi_num_exp_eval(struct psi_num_exp
*exp
, impl_val
*res
,
386 struct psi_call_frame
*frame
)
390 *res
= exp
->data
.ival
;
391 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIi64
, res
->i64
);
395 *res
= exp
->data
.ival
;
396 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIdval
, res
->dval
);
400 res
->i64
= exp
->data
.enm
->val
;
401 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%" PRIi64
, res
->i64
);
405 return psi_num_exp_eval_constant(exp
, res
, frame
);
409 return psi_num_exp_eval_decl_var(exp
, res
, frame
);
420 static inline int psi_num_exp_op_cmp(token_t op1
, token_t op2
)
422 assert(op1
>= PSI_T_LSHIFT
&& op2
<= PSI_T_PIPE
);
423 assert(op2
>= PSI_T_LSHIFT
&& op2
<= PSI_T_PIPE
);
428 return op2
> PSI_T_RSHIFT
;
432 return op2
> PSI_T_MINUS
? 1 : (op2
< PSI_T_PLUS
? -1 : 0);
436 return op2
> PSI_T_SLASH
? 1 : (op2
< PSI_T_ASTERISK
? -1 : 0);
438 case PSI_T_AMPERSAND
:
441 return -1 * (op2
< PSI_T_AMPERSAND
);
447 token_t
psi_num_exp_exec(struct psi_num_exp
*exp
, impl_val
*res
,
448 struct psi_call_frame
*frame
)
453 num_type
= psi_num_exp_eval(exp
, &num
, frame
);
457 token_t rhs_type
, res_type
;
459 /* only if there's a following op, and we have a higher precedence */
460 if (exp
->operand
->operand
&&
461 psi_num_exp_op_cmp(exp
->op
, exp
->operand
->op
) < 0) {
463 token_t tmp_type
, lhs_type
;
465 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %lc ", psi_num_exp_op_tok(exp
->op
));
467 tmp_type
= psi_num_exp_eval(exp
->operand
, &tmp
, frame
);
468 lhs_type
= exp
->calc(num_type
, &num
, tmp_type
, &tmp
, &lhs
);
470 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %c ", '=');
471 psi_num_exp_verify_result(lhs_type
, &lhs
, frame
);
472 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %c", '\n');
474 rhs_type
= psi_num_exp_exec(exp
->operand
->operand
, &rhs
, frame
);
475 res_type
= exp
->operand
->calc(lhs_type
, &lhs
, rhs_type
, &rhs
, res
);
477 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %lc ", psi_num_exp_op_tok(exp
->op
));
479 rhs_type
= psi_num_exp_exec(exp
->operand
, &rhs
, frame
);
480 res_type
= exp
->calc(num_type
, &num
, rhs_type
, &rhs
, res
);
483 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %c ", '=');
484 psi_num_exp_verify_result(res_type
, res
, frame
);
485 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %c", '\n');