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"
35 struct psi_num_exp
*psi_num_exp_init_ternary(token_t op
,
36 struct psi_num_exp
*cond
, struct psi_num_exp
*truthy
,
37 struct psi_num_exp
*falsy
)
39 struct psi_num_exp
*exp
= pecalloc(1, sizeof(*exp
), 1);
42 exp
->data
.t
.cond
= cond
;
43 exp
->data
.t
.truthy
= truthy
;
44 exp
->data
.t
.falsy
= falsy
;
49 struct psi_num_exp
*psi_num_exp_init_binary(token_t op
,
50 struct psi_num_exp
*lhs
, struct psi_num_exp
*rhs
)
52 struct psi_num_exp
*exp
= pecalloc(1, sizeof(*exp
), 1);
55 exp
->data
.b
.lhs
= lhs
;
56 exp
->data
.b
.rhs
= rhs
;
61 struct psi_num_exp
*psi_num_exp_init_unary(token_t op
,
62 struct psi_num_exp
*u
)
64 struct psi_num_exp
*exp
= pecalloc(1, sizeof(*exp
), 1);
72 struct psi_num_exp
*psi_num_exp_init_num(struct psi_number
*n
)
74 struct psi_num_exp
*exp
= pecalloc(1, sizeof(*exp
), 1);
76 exp
->op
= PSI_T_NUMBER
;
82 struct psi_num_exp
*psi_num_exp_init_cast(struct psi_decl_type
*typ
,
83 struct psi_num_exp
*num
)
85 struct psi_num_exp
*exp
= pecalloc(1, sizeof(*exp
), 1);
88 exp
->data
.c
.typ
= typ
;
89 exp
->data
.c
.num
= num
;
94 struct psi_num_exp
*psi_num_exp_copy(struct psi_num_exp
*exp
)
96 struct psi_num_exp
*cpy
;
102 cpy
= pemalloc(sizeof(*cpy
), 1);
107 cpy
->data
.n
= psi_number_copy(exp
->data
.n
);
111 cpy
->data
.c
.typ
= psi_decl_type_copy(exp
->data
.c
.typ
);
112 cpy
->data
.c
.num
= psi_num_exp_copy(exp
->data
.c
.num
);
118 cpy
->data
.u
= psi_num_exp_copy(exp
->data
.u
);
133 case PSI_T_AMPERSAND
:
141 cpy
->data
.b
.lhs
= psi_num_exp_copy(exp
->data
.b
.lhs
);
142 cpy
->data
.b
.rhs
= psi_num_exp_copy(exp
->data
.b
.rhs
);
146 cpy
->data
.t
.cond
= psi_num_exp_copy(exp
->data
.t
.cond
);
147 cpy
->data
.t
.truthy
= psi_num_exp_copy(exp
->data
.t
.truthy
);
148 cpy
->data
.t
.falsy
= psi_num_exp_copy(exp
->data
.t
.falsy
);
156 cpy
->token
= psi_token_copy(exp
->token
);
162 void psi_num_exp_copy_ctor(struct psi_num_exp
**exp_ptr
)
164 *exp_ptr
= psi_num_exp_copy(*exp_ptr
);
167 void psi_num_exp_free(struct psi_num_exp
**c_ptr
)
170 struct psi_num_exp
*c
= *c_ptr
;
176 psi_number_free(&c
->data
.n
);
180 psi_decl_type_free(&c
->data
.c
.typ
);
181 psi_num_exp_free(&c
->data
.c
.num
);
187 psi_num_exp_free(&c
->data
.u
);
202 case PSI_T_AMPERSAND
:
210 psi_num_exp_free(&c
->data
.b
.lhs
);
211 psi_num_exp_free(&c
->data
.b
.rhs
);
215 psi_num_exp_free(&c
->data
.t
.cond
);
216 psi_num_exp_free(&c
->data
.t
.truthy
);
217 psi_num_exp_free(&c
->data
.t
.falsy
);
224 psi_token_free(&c
->token
);
230 static inline const char *psi_num_exp_op_tok(token_t op
)
246 case PSI_T_AMPERSAND
:
293 struct psi_plist
*psi_num_exp_tokens(struct psi_num_exp
*exp
,
294 struct psi_plist
*list
)
296 struct psi_token
*ntoken
;
299 list
= psi_plist_init((psi_plist_dtor
) psi_token_free
);
304 list
= psi_number_tokens(exp
->data
.n
, list
);
308 ntoken
= exp
->data
.c
.typ
->token
;
309 ntoken
= psi_token_init(PSI_T_LPAREN
, "(", 1, ntoken
->col
-1, ntoken
->line
, ntoken
->file
);
310 list
= psi_plist_add(list
, &ntoken
);
311 ntoken
= psi_token_copy(exp
->data
.c
.typ
->token
);
312 list
= psi_plist_add(list
, &ntoken
);
313 ntoken
= psi_token_init(PSI_T_RPAREN
, ")", 1, ntoken
->col
+ntoken
->text
->len
, ntoken
->line
, ntoken
->file
);
314 list
= psi_plist_add(list
, &ntoken
);
320 ntoken
= psi_token_copy(exp
->token
);
321 list
= psi_plist_add(list
, &ntoken
);
322 list
= psi_num_exp_tokens(exp
->data
.u
, list
);
326 ntoken
= psi_token_copy(exp
->token
);
327 list
= psi_plist_add(list
, &ntoken
);
328 list
= psi_num_exp_tokens(exp
->data
.u
, list
);
329 psi_plist_top(list
, &ntoken
);
330 ntoken
= psi_token_init(PSI_T_RPAREN
, ")", 1, ntoken
->col
+ntoken
->text
->len
, ntoken
->line
, ntoken
->file
);
331 list
= psi_plist_add(list
, &ntoken
);
336 if (!exp
->data
.b
.rhs
) {
342 case PSI_T_AMPERSAND
:
358 list
= psi_num_exp_tokens(exp
->data
.b
.lhs
, list
);
359 ntoken
= psi_token_copy(exp
->token
);
360 list
= psi_plist_add(list
, &ntoken
);
361 list
= psi_num_exp_tokens(exp
->data
.b
.rhs
, list
);
365 list
= psi_num_exp_tokens(exp
->data
.t
.cond
, list
);
366 ntoken
= psi_token_copy(exp
->token
);
367 list
= psi_plist_add(list
, &ntoken
);
368 list
= psi_num_exp_tokens(exp
->data
.t
.truthy
, list
);
369 psi_plist_top(list
, &ntoken
);
370 ntoken
= psi_token_init(PSI_T_COLON
, ":", 1, ntoken
->col
+ntoken
->text
->len
, ntoken
->line
, ntoken
->file
);
371 list
= psi_plist_add(list
, &ntoken
);
372 list
= psi_plist_add(list
, &ntoken
);
373 list
= psi_num_exp_tokens(exp
->data
.t
.falsy
, list
);
383 void psi_num_exp_dump(struct psi_dump
*dump
, struct psi_num_exp
*exp
)
387 psi_number_dump(dump
, exp
->data
.n
);
392 psi_decl_type_dump(dump
, exp
->data
.c
.typ
, 0);
394 psi_num_exp_dump(dump
, exp
->data
.c
.num
);
400 PSI_DUMP(dump
, "%s", psi_num_exp_op_tok(exp
->op
));
401 psi_num_exp_dump(dump
, exp
->data
.u
);
406 psi_num_exp_dump(dump
, exp
->data
.u
);
412 if (!exp
->data
.b
.rhs
) {
418 case PSI_T_AMPERSAND
:
433 psi_num_exp_dump(dump
, exp
->data
.b
.lhs
);
434 PSI_DUMP(dump
, " %s ", psi_num_exp_op_tok(exp
->op
));
435 psi_num_exp_dump(dump
, exp
->data
.b
.rhs
);
439 psi_num_exp_dump(dump
, exp
->data
.t
.cond
);
440 PSI_DUMP(dump
, " ? ");
441 psi_num_exp_dump(dump
, exp
->data
.t
.truthy
);
442 PSI_DUMP(dump
, " : ");
443 psi_num_exp_dump(dump
, exp
->data
.t
.falsy
);
452 bool psi_num_exp_validate(struct psi_data
*data
, struct psi_num_exp
*exp
,
453 struct psi_validate_scope
*scope
)
455 if (exp
->op
&& exp
->op
!= PSI_T_NUMBER
) {
458 exp
->calc
= psi_calc_bool_not
;
461 exp
->calc
= psi_calc_bin_not
;
465 exp
->calc
= psi_calc_bool_or
;
468 exp
->calc
= psi_calc_bool_and
;
471 exp
->calc
= psi_calc_cmp_eq
;
474 exp
->calc
= psi_calc_cmp_ne
;
477 exp
->calc
= psi_calc_cmp_le
;
480 exp
->calc
= psi_calc_cmp_ge
;
483 exp
->calc
= psi_calc_cmp_lt
;
486 exp
->calc
= psi_calc_cmp_gt
;
495 exp
->calc
= psi_calc_bin_or
;
498 exp
->calc
= psi_calc_bin_xor
;
500 case PSI_T_AMPERSAND
:
501 exp
->calc
= psi_calc_bin_and
;
504 exp
->calc
= psi_calc_bin_lshift
;
507 exp
->calc
= psi_calc_bin_rshift
;
510 if (exp
->data
.b
.rhs
) {
511 exp
->calc
= psi_calc_add
;
515 if (exp
->data
.b
.rhs
) {
516 exp
->calc
= psi_calc_sub
;
518 exp
->calc
= psi_calc_minus
;
522 exp
->calc
= psi_calc_mul
;
525 exp
->calc
= psi_calc_div
;
528 exp
->calc
= psi_calc_mod
;
531 data
->error(data
, exp
->token
, PSI_WARNING
,
532 "Unknown numeric operator (%d)", exp
->op
);
539 return psi_number_validate(data
, exp
->data
.n
, scope
);
542 return psi_num_exp_validate(data
, exp
->data
.c
.num
, scope
)
543 && psi_decl_type_validate(data
, exp
->data
.c
.typ
, NULL
, scope
);
550 return psi_num_exp_validate(data
, exp
->data
.u
, scope
);
555 if (!exp
->data
.b
.rhs
) {
561 case PSI_T_AMPERSAND
:
577 return psi_num_exp_validate(data
, exp
->data
.b
.lhs
, scope
)
578 && psi_num_exp_validate(data
, exp
->data
.b
.rhs
, scope
);
581 return psi_num_exp_validate(data
, exp
->data
.t
.cond
, scope
)
582 && psi_num_exp_validate(data
, exp
->data
.t
.truthy
, scope
)
583 && psi_num_exp_validate(data
, exp
->data
.t
.falsy
, scope
);
592 static inline void psi_impl_val_dump(token_t t
, impl_val
*res
,
593 struct psi_call_frame
*frame
)
598 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIi8
, res
->i8
);
602 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIi16
, res
->i16
);
606 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIi32
, res
->i32
);
610 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIi64
, res
->i64
);
613 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIfval
, res
->fval
);
616 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIdval
, res
->dval
);
619 case PSI_T_LONG_DOUBLE
:
620 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIldval
, res
->ldval
);
628 static inline void psi_num_exp_verify_result(token_t t
, impl_val
*res
, struct psi_call_frame
*frame
)
630 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%s", " = ");
631 psi_impl_val_dump(t
, res
, frame
);
632 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%s", "\n");
635 static void psi_num_exp_reduce(struct psi_num_exp
*exp
, struct psi_plist
**output_ptr
,
636 struct psi_plist
**input_ptr
, struct psi_call_frame
*frame
, struct psi_cpp
*cpp
)
638 struct psi_plist
*output
= *output_ptr
, *input
= *input_ptr
;
644 struct psi_decl_type
*cast
;
650 entry
.type
= psi_number_eval(exp
->data
.n
, &entry
.data
.value
, frame
, cpp
, exp
);
651 output
= psi_plist_add(output
, &entry
);
655 entry
.type
= exp
->op
;
656 input
= psi_plist_add(input
, &entry
);
657 psi_num_exp_reduce(exp
->data
.u
, &output
, &input
, frame
, cpp
);
658 while (psi_plist_pop(input
, &entry
)) {
659 if (entry
.type
== PSI_T_LPAREN
) {
662 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
663 output
= psi_plist_add(output
, &entry
);
668 while (psi_plist_top(input
, &entry
)) {
669 /* bail out if exp->op >= entry.type */
670 if (psi_calc_oper(exp
->op
, entry
.type
) != 1) {
673 psi_plist_pop(input
, NULL
);
674 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
675 output
= psi_plist_add(output
, &entry
);
677 entry
.type
= exp
->op
;
678 entry
.data
.cast
= exp
->data
.c
.typ
;
679 input
= psi_plist_add(input
, &entry
);
680 psi_num_exp_reduce(exp
->data
.c
.num
, &output
, &input
, frame
, cpp
);
685 while (psi_plist_top(input
, &entry
)) {
686 /* bail out if exp->op >= entry.type */
687 if (psi_calc_oper(exp
->op
, entry
.type
) != 1) {
690 psi_plist_pop(input
, NULL
);
691 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
692 output
= psi_plist_add(output
, &entry
);
694 entry
.type
= exp
->op
;
695 entry
.data
.calc
= exp
->calc
;
696 input
= psi_plist_add(input
, &entry
);
697 psi_num_exp_reduce(exp
->data
.u
, &output
, &input
, frame
, cpp
);
702 impl_val cond_val
= {0};
703 token_t cond_typ
= psi_num_exp_exec(exp
->data
.t
.cond
, &cond_val
, frame
, cpp
);
705 psi_calc_bool_not(cond_typ
, &cond_val
, 0, NULL
, &cond_val
);
707 psi_num_exp_reduce(exp
->data
.t
.falsy
, &output
, &input
, frame
, cpp
);
709 psi_num_exp_reduce(exp
->data
.t
.truthy
, &output
, &input
, frame
, cpp
);
717 if (!exp
->data
.b
.rhs
) {
718 entry
.type
= psi_num_exp_exec(exp
->data
.b
.lhs
, &entry
.data
.value
, frame
, cpp
);
721 entry
.type
= exp
->calc(entry
.type
, &entry
.data
.value
, 0, NULL
, &entry
.data
.value
);
723 output
= psi_plist_add(output
, &entry
);
728 psi_num_exp_reduce(exp
->data
.b
.lhs
, &output
, &input
, frame
, cpp
);
729 while (psi_plist_top(input
, &entry
)) {
730 /* bail out if exp->op > entry.type */
731 if (psi_calc_oper(exp
->op
, entry
.type
) == -1) {
734 psi_plist_pop(input
, NULL
);
735 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
736 output
= psi_plist_add(output
, &entry
);
738 entry
.type
= exp
->op
;
739 entry
.data
.calc
= exp
->calc
;
740 input
= psi_plist_add(input
, &entry
);
741 psi_num_exp_reduce(exp
->data
.b
.rhs
, &output
, &input
, frame
, cpp
);
745 *output_ptr
= output
;
749 token_t
psi_num_exp_exec(struct psi_num_exp
*exp
, impl_val
*res
,
750 struct psi_call_frame
*frame
, struct psi_cpp
*cpp
)
752 struct psi_plist
*output
, *input
;
758 struct psi_decl_type
*cast
;
762 output
= psi_plist_init_ex(sizeof(entry
), NULL
);
763 input
= psi_plist_init_ex(sizeof(entry
), NULL
);
765 psi_num_exp_reduce(exp
, &output
, &input
, frame
, cpp
);
767 while (psi_plist_pop(input
, &entry
)) {
768 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
769 output
= psi_plist_add(output
, &entry
);
771 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%s", "\n");
773 while (psi_plist_shift(output
, &entry
)) {
774 switch (entry
.type
) {
776 input
= psi_plist_add(input
, &entry
);
780 psi_plist_pop(input
, &rhs
);
781 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
782 psi_impl_val_dump(rhs
.type
, &rhs
.data
.value
, frame
);
784 entry
.type
= psi_decl_type_get_real(entry
.data
.cast
)->type
;
785 psi_calc_cast(rhs
.type
, &rhs
.data
.value
, entry
.type
, &entry
.data
.value
);
786 input
= psi_plist_add(input
, &entry
);
787 psi_num_exp_verify_result(entry
.type
, &entry
.data
.value
, frame
);
792 psi_plist_pop(input
, &rhs
);
793 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
794 psi_impl_val_dump(rhs
.type
, &rhs
.data
.value
, frame
);
796 entry
.type
= entry
.data
.calc(rhs
.type
, &rhs
.data
.value
, 0, NULL
, &entry
.data
.value
);
797 input
= psi_plist_add(input
, &entry
);
798 psi_num_exp_verify_result(entry
.type
, &entry
.data
.value
, frame
);
813 case PSI_T_AMPERSAND
:
821 psi_plist_pop(input
, &rhs
);
822 psi_plist_pop(input
, &lhs
);
824 psi_impl_val_dump(lhs
.type
, &lhs
.data
.value
, frame
);
825 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
826 psi_impl_val_dump(rhs
.type
, &rhs
.data
.value
, frame
);
828 entry
.type
= entry
.data
.calc(
829 lhs
.type
, &lhs
.data
.value
,
830 rhs
.type
, &rhs
.data
.value
,
832 input
= psi_plist_add(input
, &entry
);
833 psi_num_exp_verify_result(entry
.type
, &entry
.data
.value
, frame
);
837 if (!psi_plist_count(output
)) {
842 psi_plist_free(output
);
843 psi_plist_free(input
);
845 *res
= entry
.data
.value
;