lib/Syntax/Operator/In.xs

filter: Statementstmt 77.2% 78 / 101 Branchbran 61.5% 16 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.0% 94 / 127 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« hax/newOP_CUSTOM.c.inc ↑ Summary
1 /* You may distribute under the terms of either the GNU General Public License
2 * or the Artistic License (the same terms as Perl itself)
3 *
4 * (C) Paul Evans, 2021 -- leonerd@leonerd.org.uk
5 */
6 #include "EXTERN.h"
7 #include "perl.h"
8 #include "XSUB.h"
9  
10 #define HAVE_PERL_VERSION(R, V, S) \
11 (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
12  
13 #include "perl-backcompat.c.inc"
14  
15 #include "newOP_CUSTOM.c.inc"
16 #include "BINOP_ANY.c.inc"
17  
18 #include "XSParseInfix.h"
19  
20 #if HAVE_PERL_VERSION(5,45,3)
21 # define HAVE_UNDEF_AWARE_EQUALITY
22 #endif
23  
24 enum Inop_Operator {
25 INOP_CUSTOM,
26 INOP_NUMBER,
27 INOP_NUMBER_UNDEF,
28 INOP_STRING,
29 INOP_STRING_UNDEF,
30 };
31  
32 32 static OP *pp_in(pTHX)
33 {
34 32 dSP;
35 32 dMARK;
36 SV **svp;
37 32 enum Inop_Operator type = PL_op->op_private;
38  
39 OP cmpop;
40 32 switch(type) {
41 0 case INOP_CUSTOM:
42 {
43 0 ANY *op_any = cBINOP_ANY->op_any;
44 0 cmpop.op_type = OP_CUSTOM;
45 0 cmpop.op_flags = 0;
46 0 cmpop.op_ppaddr = op_any[0].any_ptr;
47 0 break;
48 }
49  
50 16 case INOP_NUMBER:
51 16 cmpop.op_type = OP_EQ;
52 16 cmpop.op_flags = 0;
53 16 cmpop.op_ppaddr = PL_ppaddr[OP_EQ];
54 16 break;
55  
56 16 case INOP_STRING:
57 16 cmpop.op_type = OP_SEQ;
58 16 cmpop.op_flags = 0;
59 16 cmpop.op_ppaddr = PL_ppaddr[OP_SEQ];
60 16 break;
61  
62 #ifdef HAVE_UNDEF_AWARE_EQUALITY
63 case INOP_NUMBER_UNDEF:
64 cmpop.op_type = OP_EQU;
65 cmpop.op_flags = 0;
66 cmpop.op_ppaddr = PL_ppaddr[OP_EQU];
67 break;
68  
69 case INOP_STRING_UNDEF:
70 cmpop.op_type = OP_SEQU;
71 cmpop.op_flags = 0;
72 cmpop.op_ppaddr = PL_ppaddr[OP_SEQU];
73 break;
74 #else
75 0 case INOP_NUMBER_UNDEF:
76 case INOP_STRING_UNDEF:
77 0 croak("TODO: This perl cannot handle undef-aware equality");
78 #endif
79 }
80  
81 32 SV *lhs = *MARK;
82 32 SV **listend = SP;
83  
84 32 SP = MARK - 1;
85 32 PUTBACK;
86  
87 32 ENTER;
88 32 SAVEVPTR(PL_op);
89 32 PL_op = &cmpop;
90 32 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 32 32
91  
92 129 ▶ for(svp = MARK + 1; svp <= listend; svp++) {
Branch: 1 branch
BranchTrueFalseTotal
for(svp = MARK + 1; svp <= listend; svp++) { 116 13 129
93 116 SV *rhs = *svp;
94  
95 116 PUSHs(lhs);
96 116 PUSHs(rhs);
97 116 PUTBACK;
98  
99 116 (*cmpop.op_ppaddr)(aTHX);
100  
101 116 SPAGAIN;
102  
103 116 SV *ret = POPs;
104  
105 116 ▶ if(SvTRUE(ret)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvTRUE(ret)) { 19 97 116
106 19 LEAVE;
107  
108 19 PUSHs(&PL_sv_yes);
109 19 RETURN;
110 }
111 }
112  
113 13 LEAVE;
114  
115 13 PUSHs(&PL_sv_no);
116 13 RETURN;
117 }
118  
119 15 static void parse_in(pTHX_ U32 flags, SV **parsedata, void *hookdata)
120 {
121 15 bool using_circumfix = false;
122 15 ▶ if(lex_peek_unichar(0) == '<')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == '<') 4 11 15
123 4 using_circumfix = true;
124 11 ▶ else if(lex_peek_unichar(0) != ':')
Branch: 1 branch
BranchTrueFalseTotal
else if(lex_peek_unichar(0) != ':') 0 11 11
125 0 croak("Expected ':' or '<'");
126 15 lex_read_unichar(0);
127  
128 15 lex_read_space(0);
129  
130 struct XSParseInfixInfo *info;
131 15 ▶ if(!parse_infix(XPI_SELECT_ANY, &info))
Branch: 1 branch
BranchTrueFalseTotal
if(!parse_infix(XPI_SELECT_ANY, &info)) 0 15 15
132 0 croak("Expected an equality test operator");
133 15 ▶ if(info->cls != XPI_CLS_EQUALITY)
Branch: 1 branch
BranchTrueFalseTotal
if(info->cls != XPI_CLS_EQUALITY) 0 15 15
134 0 croak("The %s operator is not permitted for the in: meta-operator (cls=%d)", info->opname, info->cls);
135  
136 /* parsedata will be an AV containing
137 * [0] IV = enum Inop_Operator
138 * [1] UV = PTR to pp_addr if CUSTOM
139 */
140 15 AV *parsedata_av = newAV();
141 15 *parsedata = newRV_noinc((SV *)parsedata_av);
142  
143 /* See if we got one of the core ones */
144 15 ▶ if(info->opcode == OP_EQ) {
Branch: 1 branch
BranchTrueFalseTotal
if(info->opcode == OP_EQ) { 8 7 15
145 8 av_push(parsedata_av, newSViv(INOP_NUMBER));
146 }
147 7 ▶ else if(info->opcode == OP_SEQ) {
Branch: 1 branch
BranchTrueFalseTotal
else if(info->opcode == OP_SEQ) { 7 0 7
148 7 av_push(parsedata_av, newSViv(INOP_STRING));
149 }
150 #ifdef HAVE_UNDEF_AWARE_EQUALITY
151 else if(info->opcode == OP_EQU) {
152 av_push(parsedata_av, newSViv(INOP_NUMBER_UNDEF));
153 }
154 else if(info->opcode == OP_SEQU) {
155 av_push(parsedata_av, newSViv(INOP_STRING_UNDEF));
156 }
157 #endif
158 0 ▶ else if(info->opcode == OP_CUSTOM) {
Branch: 1 branch
BranchTrueFalseTotal
else if(info->opcode == OP_CUSTOM) { 0 0 0
159 0 ▶ if(info->hooks->new_op)
Branch: 1 branch
BranchTrueFalseTotal
if(info->hooks->new_op) 0 0 0
160 0 croak("TODO: handle custom op using the new_op function for '%s'", info->opname);
161  
162 0 av_push(parsedata_av, newSViv(INOP_CUSTOM));
163 0 av_push(parsedata_av, newSVuv(PTR2UV(info->hooks->ppaddr)));
164 }
165 else
166 0 croak("Expected an equality test operator name but found '%s'", info->opname);
167  
168 15 ▶ if(using_circumfix) {
Branch: 1 branch
BranchTrueFalseTotal
if(using_circumfix) { 4 11 15
169 4 ▶ if(lex_peek_unichar(0) != '>')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '>') 0 4 4
170 0 croak("Expected '>'");
171 4 lex_read_unichar(0);
172 }
173 15 }
174  
175 15 static OP *newop_in(pTHX_ U32 flags, OP *lhs, OP *rhs, SV **parsedata, void *hookdata)
176 {
177 15 AV *parsedata_av = AV_FROM_REF(*parsedata);
178  
179 15 enum Inop_Operator operator = SvIV(AvARRAY(parsedata_av)[0]);
180  
181 OP *ret;
182 15 switch(operator) {
183 0 case INOP_CUSTOM:
184 0 ret = newBINOP_ANY_CUSTOM(&pp_in, 0, lhs, rhs, 1);
185 0 cBINOP_ANYx(ret)->op_any[0].any_ptr = INT2PTR(void *, SvUV(AvARRAY(parsedata_av)[1]));
186 0 ret->op_private = INOP_CUSTOM;
187 0 break;
188  
189 15 case INOP_NUMBER:
190 case INOP_NUMBER_UNDEF:
191 case INOP_STRING:
192 case INOP_STRING_UNDEF:
193 15 ret = newBINOP_CUSTOM(&pp_in, 0, lhs, rhs);
194 15 ret->op_private = operator;
195 15 break;
196 }
197  
198 15 return ret;
199 }
200  
201 14 static OP *newop_in_str(pTHX_ U32 flags, OP *lhs, OP *rhs, SV **parsedata, void *hookdata)
202 {
203 14 OP *ret = newBINOP_CUSTOM(&pp_in, 0, lhs, rhs);
204 14 ret->op_private = INOP_STRING;
205  
206 14 return ret;
207 }
208  
209 13 static OP *newop_in_num(pTHX_ U32 flags, OP *lhs, OP *rhs, SV **parsedata, void *hookdata)
210 {
211 13 OP *ret = newBINOP_CUSTOM(&pp_in, 0, lhs, rhs);
212 13 ret->op_private = INOP_NUMBER;
213  
214 13 return ret;
215 }
216  
217 struct XSParseInfixHooks infix_in = {
218 .cls = XPI_CLS_MATCH_MISC,
219 .rhs_flags = XPI_OPERAND_LIST,
220  
221 .parse = &parse_in,
222 .new_op = &newop_in,
223 };
224  
225 struct XSParseInfixHooks infix_elem_str = {
226 .cls = XPI_CLS_MATCH_MISC,
227 .rhs_flags = XPI_OPERAND_LIST,
228 .permit_hintkey = "Syntax::Operator::Elem/elem",
229  
230 .wrapper_func_name = "Syntax::Operator::Elem::elem_str",
231  
232 .new_op = &newop_in_str,
233 };
234  
235 struct XSParseInfixHooks infix_elem_num = {
236 .cls = XPI_CLS_MATCH_MISC,
237 .rhs_flags = XPI_OPERAND_LIST,
238 .permit_hintkey = "Syntax::Operator::Elem/elem",
239  
240 .wrapper_func_name = "Syntax::Operator::Elem::elem_num",
241  
242 .new_op = &newop_in_num,
243 };
244  
245 MODULE = Syntax::Operator::In PACKAGE = Syntax::Operator::In
246  
247 BOOT:
248 7 boot_xs_parse_infix(0.51);
249  
250 7 register_xs_parse_infix("Syntax::Operator::In::in", &infix_in, NULL);
251  
252 7 register_xs_parse_infix("Syntax::Operator::Elem::elem", &infix_elem_str, NULL);
253 7 register_xs_parse_infix("Syntax::Operator::Elem::∈", &infix_elem_num, NULL);
« hax/newOP_CUSTOM.c.inc ↑ Summary