ce89de515800786d2420a7d62de00869593da2da
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(1, exp
->data
.c
.typ
, 0);
399 PSI_DUMP(dump
, "%s", psi_num_exp_op_tok(exp
->op
));
400 psi_num_exp_dump(dump
, exp
->data
.u
);
405 psi_num_exp_dump(dump
, exp
->data
.u
);
411 if (!exp
->data
.b
.rhs
) {
417 case PSI_T_AMPERSAND
:
432 psi_num_exp_dump(dump
, exp
->data
.b
.lhs
);
433 PSI_DUMP(dump
, " %s ", psi_num_exp_op_tok(exp
->op
));
434 psi_num_exp_dump(dump
, exp
->data
.b
.rhs
);
438 psi_num_exp_dump(dump
, exp
->data
.t
.cond
);
439 PSI_DUMP(dump
, " ? ");
440 psi_num_exp_dump(dump
, exp
->data
.t
.truthy
);
441 PSI_DUMP(dump
, " : ");
442 psi_num_exp_dump(dump
, exp
->data
.t
.falsy
);
451 bool psi_num_exp_validate(struct psi_data
*data
, struct psi_num_exp
*exp
,
452 struct psi_validate_scope
*scope
)
454 if (exp
->op
&& exp
->op
!= PSI_T_NUMBER
) {
457 exp
->calc
= psi_calc_bool_not
;
460 exp
->calc
= psi_calc_bin_not
;
464 exp
->calc
= psi_calc_bool_or
;
467 exp
->calc
= psi_calc_bool_and
;
470 exp
->calc
= psi_calc_cmp_eq
;
473 exp
->calc
= psi_calc_cmp_ne
;
476 exp
->calc
= psi_calc_cmp_le
;
479 exp
->calc
= psi_calc_cmp_ge
;
482 exp
->calc
= psi_calc_cmp_lt
;
485 exp
->calc
= psi_calc_cmp_gt
;
494 exp
->calc
= psi_calc_bin_or
;
497 exp
->calc
= psi_calc_bin_xor
;
499 case PSI_T_AMPERSAND
:
500 exp
->calc
= psi_calc_bin_and
;
503 exp
->calc
= psi_calc_bin_lshift
;
506 exp
->calc
= psi_calc_bin_rshift
;
509 if (exp
->data
.b
.rhs
) {
510 exp
->calc
= psi_calc_add
;
514 if (exp
->data
.b
.rhs
) {
515 exp
->calc
= psi_calc_sub
;
517 exp
->calc
= psi_calc_minus
;
521 exp
->calc
= psi_calc_mul
;
524 exp
->calc
= psi_calc_div
;
527 exp
->calc
= psi_calc_mod
;
530 data
->error(data
, exp
->token
, PSI_WARNING
,
531 "Unknown numeric operator (%d)", exp
->op
);
538 return psi_number_validate(data
, exp
->data
.n
, scope
);
541 return psi_num_exp_validate(data
, exp
->data
.c
.num
, scope
)
542 && psi_decl_type_validate(data
, exp
->data
.c
.typ
, NULL
, scope
);
549 return psi_num_exp_validate(data
, exp
->data
.u
, scope
);
554 if (!exp
->data
.b
.rhs
) {
560 case PSI_T_AMPERSAND
:
576 return psi_num_exp_validate(data
, exp
->data
.b
.lhs
, scope
)
577 && psi_num_exp_validate(data
, exp
->data
.b
.rhs
, scope
);
580 return psi_num_exp_validate(data
, exp
->data
.t
.cond
, scope
)
581 && psi_num_exp_validate(data
, exp
->data
.t
.truthy
, scope
)
582 && psi_num_exp_validate(data
, exp
->data
.t
.falsy
, scope
);
591 static inline void psi_impl_val_dump(token_t t
, impl_val
*res
,
592 struct psi_call_frame
*frame
)
597 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIi8
, res
->i8
);
601 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIi16
, res
->i16
);
605 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIi32
, res
->i32
);
609 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIi64
, res
->i64
);
612 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIfval
, res
->fval
);
615 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIdval
, res
->dval
);
618 case PSI_T_LONG_DOUBLE
:
619 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %" PRIldval
, res
->ldval
);
627 static inline void psi_num_exp_verify_result(token_t t
, impl_val
*res
, struct psi_call_frame
*frame
)
629 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%s", " = ");
630 psi_impl_val_dump(t
, res
, frame
);
631 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%s", "\n");
634 static void psi_num_exp_reduce(struct psi_num_exp
*exp
, struct psi_plist
**output_ptr
,
635 struct psi_plist
**input_ptr
, struct psi_call_frame
*frame
, struct psi_cpp
*cpp
)
637 struct psi_plist
*output
= *output_ptr
, *input
= *input_ptr
;
643 struct psi_decl_type
*cast
;
649 entry
.type
= psi_number_eval(exp
->data
.n
, &entry
.data
.value
, frame
, cpp
, exp
);
650 output
= psi_plist_add(output
, &entry
);
654 entry
.type
= exp
->op
;
655 input
= psi_plist_add(input
, &entry
);
656 psi_num_exp_reduce(exp
->data
.u
, &output
, &input
, frame
, cpp
);
657 while (psi_plist_pop(input
, &entry
)) {
658 if (entry
.type
== PSI_T_LPAREN
) {
661 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
662 output
= psi_plist_add(output
, &entry
);
667 while (psi_plist_top(input
, &entry
)) {
668 /* bail out if exp->op >= entry.type */
669 if (psi_calc_oper(exp
->op
, entry
.type
) != 1) {
672 psi_plist_pop(input
, NULL
);
673 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
674 output
= psi_plist_add(output
, &entry
);
676 entry
.type
= exp
->op
;
677 entry
.data
.cast
= exp
->data
.c
.typ
;
678 input
= psi_plist_add(input
, &entry
);
679 psi_num_exp_reduce(exp
->data
.c
.num
, &output
, &input
, frame
, cpp
);
684 while (psi_plist_top(input
, &entry
)) {
685 /* bail out if exp->op >= entry.type */
686 if (psi_calc_oper(exp
->op
, entry
.type
) != 1) {
689 psi_plist_pop(input
, NULL
);
690 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
691 output
= psi_plist_add(output
, &entry
);
693 entry
.type
= exp
->op
;
694 entry
.data
.calc
= exp
->calc
;
695 input
= psi_plist_add(input
, &entry
);
696 psi_num_exp_reduce(exp
->data
.u
, &output
, &input
, frame
, cpp
);
701 impl_val cond_val
= {0};
702 token_t cond_typ
= psi_num_exp_exec(exp
->data
.t
.cond
, &cond_val
, frame
, cpp
);
704 psi_calc_bool_not(cond_typ
, &cond_val
, 0, NULL
, &cond_val
);
706 psi_num_exp_reduce(exp
->data
.t
.falsy
, &output
, &input
, frame
, cpp
);
708 psi_num_exp_reduce(exp
->data
.t
.truthy
, &output
, &input
, frame
, cpp
);
716 if (!exp
->data
.b
.rhs
) {
717 entry
.type
= psi_num_exp_exec(exp
->data
.b
.lhs
, &entry
.data
.value
, frame
, cpp
);
720 entry
.type
= exp
->calc(entry
.type
, &entry
.data
.value
, 0, NULL
, &entry
.data
.value
);
722 output
= psi_plist_add(output
, &entry
);
727 psi_num_exp_reduce(exp
->data
.b
.lhs
, &output
, &input
, frame
, cpp
);
728 while (psi_plist_top(input
, &entry
)) {
729 /* bail out if exp->op > entry.type */
730 if (psi_calc_oper(exp
->op
, entry
.type
) == -1) {
733 psi_plist_pop(input
, NULL
);
734 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
735 output
= psi_plist_add(output
, &entry
);
737 entry
.type
= exp
->op
;
738 entry
.data
.calc
= exp
->calc
;
739 input
= psi_plist_add(input
, &entry
);
740 psi_num_exp_reduce(exp
->data
.b
.rhs
, &output
, &input
, frame
, cpp
);
744 *output_ptr
= output
;
748 token_t
psi_num_exp_exec(struct psi_num_exp
*exp
, impl_val
*res
,
749 struct psi_call_frame
*frame
, struct psi_cpp
*cpp
)
751 struct psi_plist
*output
, *input
;
757 struct psi_decl_type
*cast
;
761 output
= psi_plist_init_ex(sizeof(entry
), NULL
);
762 input
= psi_plist_init_ex(sizeof(entry
), NULL
);
764 psi_num_exp_reduce(exp
, &output
, &input
, frame
, cpp
);
766 while (psi_plist_pop(input
, &entry
)) {
767 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
768 output
= psi_plist_add(output
, &entry
);
770 if (frame
) PSI_DEBUG_PRINT(frame
->context
, "%s", "\n");
772 while (psi_plist_shift(output
, &entry
)) {
773 switch (entry
.type
) {
775 input
= psi_plist_add(input
, &entry
);
779 psi_plist_pop(input
, &rhs
);
780 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
781 psi_impl_val_dump(rhs
.type
, &rhs
.data
.value
, frame
);
783 entry
.type
= psi_decl_type_get_real(entry
.data
.cast
)->type
;
784 psi_calc_cast(rhs
.type
, &rhs
.data
.value
, entry
.type
, &entry
.data
.value
);
785 input
= psi_plist_add(input
, &entry
);
786 psi_num_exp_verify_result(entry
.type
, &entry
.data
.value
, frame
);
791 psi_plist_pop(input
, &rhs
);
792 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
793 psi_impl_val_dump(rhs
.type
, &rhs
.data
.value
, frame
);
795 entry
.type
= entry
.data
.calc(rhs
.type
, &rhs
.data
.value
, 0, NULL
, &entry
.data
.value
);
796 input
= psi_plist_add(input
, &entry
);
797 psi_num_exp_verify_result(entry
.type
, &entry
.data
.value
, frame
);
812 case PSI_T_AMPERSAND
:
820 psi_plist_pop(input
, &rhs
);
821 psi_plist_pop(input
, &lhs
);
823 psi_impl_val_dump(lhs
.type
, &lhs
.data
.value
, frame
);
824 if (frame
) PSI_DEBUG_PRINT(frame
->context
, " %s", psi_num_exp_op_tok(entry
.type
));
825 psi_impl_val_dump(rhs
.type
, &rhs
.data
.value
, frame
);
827 entry
.type
= entry
.data
.calc(
828 lhs
.type
, &lhs
.data
.value
,
829 rhs
.type
, &rhs
.data
.value
,
831 input
= psi_plist_add(input
, &entry
);
832 psi_num_exp_verify_result(entry
.type
, &entry
.data
.value
, frame
);
836 if (!psi_plist_count(output
)) {
841 psi_plist_free(output
);
842 psi_plist_free(input
);
844 *res
= entry
.data
.value
;