cpp
[m6w6/ext-psi] / src / types / num_exp.h
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
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.
13
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 *******************************************************************************/
25
26 #ifndef PSI_TYPES_NUM_EXP_H
27 #define PSI_TYPES_NUM_EXP_H
28
29 #include "token.h"
30 #include "Zend/zend_types.h"
31
32 struct psi_data;
33 struct psi_token;
34 struct psi_impl;
35 struct psi_const;
36 struct psi_decl_enum_item;
37 struct psi_let_exp;
38 struct psi_set_exp;
39 struct psi_call_frame;
40
41 struct psi_num_exp {
42 struct psi_token *token;
43 token_t op;
44 union {
45 struct {
46 struct psi_num_exp *lhs;
47 struct psi_num_exp *rhs;
48 } b;
49 struct {
50 struct psi_decl_type *typ;
51 struct psi_num_exp *num;
52 } c;
53 struct {
54 struct psi_num_exp *cond;
55 struct psi_num_exp *truthy;
56 struct psi_num_exp *falsy;
57 } t;
58 struct psi_num_exp *u;
59 struct psi_number *n;
60 } data;
61 token_t (*calc)(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res);
62 };
63
64 struct psi_num_exp *psi_num_exp_init_ternary(token_t op,
65 struct psi_num_exp *cond, struct psi_num_exp *truthy,
66 struct psi_num_exp *falsy);
67 struct psi_num_exp *psi_num_exp_init_binary(token_t op,
68 struct psi_num_exp *lhs, struct psi_num_exp *rhs);
69 struct psi_num_exp *psi_num_exp_init_unary(token_t op,
70 struct psi_num_exp *u);
71 struct psi_num_exp *psi_num_exp_init_num(struct psi_number *n);
72 struct psi_num_exp *psi_num_exp_init_cast(struct psi_decl_type *typ,
73 struct psi_num_exp *num);
74 void psi_num_exp_free(struct psi_num_exp **c_ptr);
75
76 struct psi_num_exp *psi_num_exp_copy(struct psi_num_exp *exp);
77 void psi_num_exp_dump(int fd, struct psi_num_exp *exp);
78 bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp,
79 struct psi_impl *impl, struct psi_decl *cb_decl,
80 struct psi_let_exp *current_let, struct psi_set_exp *current_set,
81 struct psi_decl_enum *current_enum);
82
83 token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res,
84 struct psi_call_frame *frame, HashTable *defs);
85
86 #include <assert.h>
87
88 static inline zend_long psi_long_num_exp(struct psi_num_exp *exp,
89 struct psi_call_frame *frame, HashTable *defs) {
90 impl_val val = {0};
91
92 switch (psi_num_exp_exec(exp, &val, frame, defs)) {
93 case PSI_T_UINT8: return val.u8;
94 case PSI_T_UINT16: return val.u16;
95 case PSI_T_UINT32: return val.u32;
96 case PSI_T_UINT64: return val.u64; /* FIXME */
97 case PSI_T_INT8: return val.i8;
98 case PSI_T_INT16: return val.i16;
99 case PSI_T_INT32: return val.i32;
100 case PSI_T_INT64: return val.i64;
101 case PSI_T_FLOAT: return val.fval;
102 case PSI_T_DOUBLE: return val.dval;
103 default:
104 assert(0);
105 }
106 return 0;
107 }
108
109
110 #endif