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 void psi_num_exp_dump(int fd
, struct psi_num_exp
*exp
)
122 dprintf(fd
, "%" PRId64
, exp
->data
.ival
.i64
);
125 dprintf(fd
, "%F", exp
->data
.ival
.dval
);
129 dprintf(fd
, "%s", exp
->data
.numb
);
132 dprintf(fd
, "%s", exp
->data
.cnst
->name
);
135 dprintf(fd
, "%s", exp
->data
.enm
->name
);
138 psi_decl_var_dump(fd
, exp
->data
.dvar
);
163 dprintf(fd
, " %c ", op
);
170 static inline bool psi_num_exp_validate_enum(struct psi_data
*data
, struct psi_num_exp
*exp
,
171 struct psi_decl_enum
*enm
)
174 struct psi_decl_enum_item
*itm
;
176 while (psi_plist_get(enm
->items
, i
++, &itm
)) {
177 if (!strcmp(itm
->name
, exp
->data
.dvar
->name
)) {
178 psi_decl_var_free(&exp
->data
.dvar
);
179 exp
->type
= PSI_T_ENUM
;
181 return psi_num_exp_validate(data
, exp
, NULL
, NULL
, NULL
, NULL
, enm
);
188 bool psi_num_exp_validate(struct psi_data
*data
, struct psi_num_exp
*exp
,
189 struct psi_impl
*impl
, struct psi_decl
*cb_decl
, struct psi_let_exp
*current_let
,
190 struct psi_set_exp
*current_set
, struct psi_decl_enum
*current_enum
)
194 struct psi_const
*cnst
;
195 struct psi_decl_enum
*enm
;
200 exp
->calc
= psi_calc_add
;
203 exp
->calc
= psi_calc_sub
;
206 exp
->calc
= psi_calc_mul
;
209 exp
->calc
= psi_calc_div
;
212 data
->error(data
, exp
->token
, PSI_WARNING
,
213 "Unknown numeric operator (%d)", exp
->op
);
216 if (!psi_num_exp_validate(data
, exp
->operand
, impl
, cb_decl
, current_let
,
217 current_set
, current_enum
)) {
230 if (current_enum
&& psi_num_exp_validate_enum(data
, exp
, current_enum
)) {
233 while (psi_plist_get(data
->enums
, i
++, &enm
)) {
234 if (psi_num_exp_validate_enum(data
, exp
, enm
)) {
238 if (exp
->data
.dvar
->arg
) {
241 if (psi_decl_var_validate(data
, exp
->data
.dvar
, impl
? impl
->decl
: NULL
,
242 current_let
, current_set
)) {
245 if (cb_decl
&& psi_decl_var_validate(data
, exp
->data
.dvar
, cb_decl
, NULL
, NULL
)) {
248 data
->error(data
, exp
->token
, PSI_WARNING
,
249 "Unknown variable '%s' in numeric expression",
250 exp
->data
.dvar
->name
);
254 while (psi_plist_get(data
->consts
, i
++, &cnst
)) {
255 if (!strcmp(cnst
->name
, exp
->data
.numb
)) {
256 free(exp
->data
.numb
);
257 exp
->type
= PSI_T_CONST
;
258 exp
->data
.cnst
= cnst
;
262 data
->error(data
, exp
->token
, PSI_WARNING
,
263 "Unknown constant '%s' in numeric expression",
268 switch (is_numeric_string(exp
->data
.numb
, strlen(exp
->data
.numb
), (zend_long
*) &tmp
, (double *) &tmp
, 0)) {
270 free(exp
->data
.numb
);
271 exp
->type
= PSI_T_INT64
;
272 exp
->data
.ival
.i64
= tmp
.zend
.lval
;
276 free(exp
->data
.numb
);
277 exp
->type
= PSI_T_DOUBLE
;
278 exp
->data
.ival
.dval
= tmp
.dval
;
281 data
->error(data
, exp
->token
, PSI_WARNING
, "Expected numeric entity (parser error?)");
291 #include "Zend/zend_constants.h"
293 static inline token_t
psi_num_exp_eval_constant(struct psi_num_exp
*exp
,
294 impl_val
*res
, struct psi_call_frame
*frame
)
296 switch (exp
->data
.cnst
->type
->type
) {
298 res
->i64
= zend_get_constant_str(exp
->data
.cnst
->name
,
299 strlen(exp
->data
.cnst
->name
))->value
.lval
;
302 res
->dval
= zend_get_constant_str(exp
->data
.cnst
->name
,
303 strlen(exp
->data
.cnst
->name
))->value
.dval
;
310 static inline token_t
psi_num_exp_eval_decl_var(struct psi_num_exp
*exp
,
311 impl_val
*res
, struct psi_call_frame
*frame
)
314 struct psi_call_frame_symbol
*sym
;
315 struct psi_decl_type
*real
;
318 real
= psi_decl_type_get_real(exp
->data
.dvar
->arg
->type
);
319 size
= psi_decl_arg_get_size(exp
->data
.dvar
->arg
);
320 sym
= psi_call_frame_fetch_symbol(frame
, exp
->data
.dvar
);
321 ref
= deref_impl_val(sym
->ptr
, exp
->data
.dvar
);
323 memcpy(res
, ref
, size
);
325 switch (real
->type
) {
344 static inline token_t
psi_num_exp_eval(struct psi_num_exp
*exp
, impl_val
*res
,
345 struct psi_call_frame
*frame
)
350 *res
= exp
->data
.ival
;
354 res
->i64
= exp
->data
.enm
->val
;
358 return psi_num_exp_eval_constant(exp
, res
, frame
);
362 return psi_num_exp_eval_decl_var(exp
, res
, frame
);
371 token_t
psi_num_exp_exec(struct psi_num_exp
*exp
, impl_val
*res
,
372 struct psi_call_frame
*frame
)
375 token_t num_type
= psi_num_exp_eval(exp
, &num
, frame
);
377 /* FIXME operator precedence */
380 token_t tmp_type
= psi_num_exp_exec(exp
->operand
, &tmp
, frame
);
382 return exp
->calc(num_type
, &num
, tmp_type
, &tmp
, res
);