basic support for builtins
[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 struct psi_validate_scope;
41
42 struct psi_num_exp {
43 struct psi_token *token;
44 token_t op;
45 union {
46 struct {
47 struct psi_num_exp *lhs;
48 struct psi_num_exp *rhs;
49 } b;
50 struct {
51 struct psi_decl_type *typ;
52 struct psi_num_exp *num;
53 } c;
54 struct {
55 struct psi_num_exp *cond;
56 struct psi_num_exp *truthy;
57 struct psi_num_exp *falsy;
58 } t;
59 struct psi_num_exp *u;
60 struct psi_number *n;
61 } data;
62 token_t (*calc)(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res);
63 };
64
65 struct psi_num_exp *psi_num_exp_init_ternary(token_t op,
66 struct psi_num_exp *cond, struct psi_num_exp *truthy,
67 struct psi_num_exp *falsy);
68 struct psi_num_exp *psi_num_exp_init_binary(token_t op,
69 struct psi_num_exp *lhs, struct psi_num_exp *rhs);
70 struct psi_num_exp *psi_num_exp_init_unary(token_t op,
71 struct psi_num_exp *u);
72 struct psi_num_exp *psi_num_exp_init_num(struct psi_number *n);
73 struct psi_num_exp *psi_num_exp_init_cast(struct psi_decl_type *typ,
74 struct psi_num_exp *num);
75 void psi_num_exp_free(struct psi_num_exp **c_ptr);
76
77 struct psi_num_exp *psi_num_exp_copy(struct psi_num_exp *exp);
78 void psi_num_exp_copy_ctor(struct psi_num_exp **exp_ptr);
79
80 void psi_num_exp_dump(int fd, struct psi_num_exp *exp);
81 bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp,
82 struct psi_validate_scope *scope);
83
84 token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res,
85 struct psi_call_frame *frame, struct psi_cpp *cpp);
86
87 struct psi_plist *psi_num_exp_tokens(struct psi_num_exp *exp,
88 struct psi_plist *list);
89
90
91 #include "calc.h"
92 static inline zend_long psi_num_exp_get_long(struct psi_num_exp *exp,
93 struct psi_call_frame *frame, struct psi_cpp *cpp) {
94 impl_val res = {0};
95
96 psi_calc_cast(psi_num_exp_exec(exp, &res, frame, cpp), &res,
97 PSI_T_INT64, &res);
98
99 return res.i64;
100 }
101 static inline double psi_num_exp_get_double(struct psi_num_exp *exp,
102 struct psi_call_frame *frame, struct psi_cpp *cpp) {
103 impl_val res = {0};
104
105 psi_calc_cast(psi_num_exp_exec(exp, &res, frame, cpp), &res,
106 PSI_T_DOUBLE, &res);
107
108 return res.dval;
109 }
110
111 #endif