lib/XS/Parse/Infix/FromPerl.xs

filter: Statementstmt 90.8% 99 / 109 Branchbran 60.0% 24 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.5% 123 / 149 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/XS/Parse/Keyword/FromPerl.xs ↑ Summary lib/Optree/Generate.xs »
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, 2023 -- leonerd@leonerd.org.uk
5 */
6 #include "EXTERN.h"
7 #include "perl.h"
8 #include "XSUB.h"
9  
10 #include "XSParseInfix.h"
11  
12 #include "perl-backcompat.c.inc"
13 #include "perl-additions.c.inc"
14  
15 #include "newSVop.c.inc"
16  
17 struct XPIFPHookdata {
18 /* Phase callbacks */
19 CV *permitcv;
20 CV *new_opcv;
21  
22 SV *hookdata;
23 };
24  
25 1 static bool cb_permit(pTHX_ void *hookdata)
26 {
27 1 struct XPIFPHookdata *data = hookdata;
28  
29 1 dSP;
30  
31 1 ENTER;
32 1 SAVETMPS;
33  
34 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
35 1 ▶ if(data->hookdata)
Branch: 1 branch
BranchTrueFalseTotal
if(data->hookdata) 1 0 1
36 1 ▶ XPUSHs(sv_mortalcopy(data->hookdata));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_mortalcopy(data->hookdata)); 0 1 1
37 else
38 0 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 0 0
39 1 PUTBACK;
40  
41 1 call_sv((SV *)data->permitcv, G_SCALAR);
42  
43 1 SPAGAIN;
44 1 bool ret = SvTRUEx(POPs);
45  
46 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 0 1
47 1 LEAVE;
48  
49 1 return ret;
50 }
51  
52 3 static OP *cb_new_op(pTHX_ U32 flags, OP *lhs, OP *rhs, SV **parsedata, void *hookdata)
53 {
54 3 struct XPIFPHookdata *data = hookdata;
55  
56 3 dSP;
57  
58 3 ENTER;
59 3 SAVETMPS;
60  
61 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
62 3 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 3 3
63 3 mPUSHu(flags);
64 3 PUSHs(sv_2mortal(newSVop(lhs)));
65 3 PUSHs(sv_2mortal(newSVop(rhs)));
66 3 PUSHs(&PL_sv_undef); /* parsedata; ignore for now */
67 3 ▶ if(data->hookdata)
Branch: 1 branch
BranchTrueFalseTotal
if(data->hookdata) 1 2 3
68 1 PUSHs(sv_mortalcopy(data->hookdata));
69 else
70 2 PUSHs(&PL_sv_undef);
71 3 PUTBACK;
72  
73 3 call_sv((SV *)data->new_opcv, G_SCALAR);
74  
75 3 SPAGAIN;
76 3 OP *ret = SvOPo(POPs);
77  
78 3 PUTBACK;
79  
80 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3 0 3
81 3 LEAVE;
82  
83 3 return ret;
84 }
85  
86 3 static void S_setup_constants(pTHX)
87 {
88 HV *stash;
89 AV *export;
90  
91 #define DO_CONSTANT(c) \
92 newCONSTSUB(stash, #c, newSViv(c)); \
93 av_push(export, newSVpv(#c, 0))
94  
95 3 stash = gv_stashpvs("XS::Parse::Infix::FromPerl", TRUE);
96 3 export = get_av("XS::Parse::Infix::FromPerl::EXPORT_OK", TRUE);
97  
98 3 DO_CONSTANT(XPI_CLS_NONE);
99 3 DO_CONSTANT(XPI_CLS_PREDICATE);
100 3 DO_CONSTANT(XPI_CLS_RELATION);
101 3 DO_CONSTANT(XPI_CLS_EQUALITY);
102 3 DO_CONSTANT(XPI_CLS_SMARTMATCH);
103 3 DO_CONSTANT(XPI_CLS_MATCHRE);
104 3 DO_CONSTANT(XPI_CLS_ISA);
105 3 DO_CONSTANT(XPI_CLS_MATCH_MISC);
106 3 DO_CONSTANT(XPI_CLS_ORDERING);
107 3 DO_CONSTANT(XPI_CLS_LOW_MISC);
108 3 DO_CONSTANT(XPI_CLS_LOGICAL_OR_LOW_MISC);
109 3 DO_CONSTANT(XPI_CLS_LOGICAL_AND_LOW_MISC);
110 3 DO_CONSTANT(XPI_CLS_ASSIGN_MISC);
111 3 DO_CONSTANT(XPI_CLS_LOGICAL_OR_MISC);
112 3 DO_CONSTANT(XPI_CLS_LOGICAL_AND_MISC);
113 3 DO_CONSTANT(XPI_CLS_ADD_MISC);
114 3 DO_CONSTANT(XPI_CLS_MUL_MISC);
115 3 DO_CONSTANT(XPI_CLS_POW_MISC);
116 3 DO_CONSTANT(XPI_CLS_HIGH_MISC);
117 3 }
118  
119 MODULE = XS::Parse::Infix::FromPerl PACKAGE = XS::Parse::Infix::FromPerl
120  
121 void
122 register_xs_parse_infix(const char *name, ...)
123 CODE:
124 3 dKWARG(1);
125  
126 3 struct XSParseInfixHooks hooks = {0};
127 3 struct XPIFPHookdata data = {0};
128 3 SV *wrapper_func_namesv = NULL;
129 3 SV *permit_hintkeysv = NULL;
130  
131 static const char *args[] = {
132 "flags",
133 "lhs_flags",
134 "rhs_flags",
135 "cls",
136 "wrapper_func_name",
137 "permit_hintkey",
138 "permit",
139 "new_op",
140 /* TODO: parse? */
141 "hookdata",
142 };
143 13 ▶ while(KWARG_NEXT(args))
Branch: 1 branch
BranchTrueFalseTotal
while(KWARG_NEXT(args)) 10 3 13
144 10 switch(kwarg) {
145 0 case 0: /* flags */
146 case 1: /* lhs_flags */
147 case 2: /* rhs_flags */
148 0 croak("TODO: flags not currently supported");
149  
150 3 case 3: /* cls */
151 3 hooks.cls = SvUV(kwval);
152 3 break;
153  
154 0 case 4: /* wrapper_func_name */
155 0 wrapper_func_namesv = kwval;
156 0 break;
157  
158 2 case 5: /* permit_hintkey */
159 2 permit_hintkeysv = kwval;
160 2 break;
161  
162 1 case 6: /* permit */
163 1 ▶ if(!SvROK(kwval) || SvTYPE(SvRV(kwval)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(kwval) || SvTYPE(SvRV(kwval)) != SVt_PVCV) 1 0 1
if(!SvROK(kwval) || SvTYPE(SvRV(kwval)) != SVt_PVCV) 0 1 1
164 0 croak("Expected 'permit' to be a CODE ref");
165 1 data.permitcv = (CV *)SvREFCNT_inc((SV *)CV_FROM_REF(kwval));
166 1 break;
167  
168 3 case 7: /* new_op */
169 3 ▶ if(!SvROK(kwval) || SvTYPE(SvRV(kwval)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(kwval) || SvTYPE(SvRV(kwval)) != SVt_PVCV) 3 0 3
if(!SvROK(kwval) || SvTYPE(SvRV(kwval)) != SVt_PVCV) 0 3 3
170 0 croak("Expected 'new_op' to be a CODE ref");
171 3 data.new_opcv = (CV *)SvREFCNT_inc((SV *)CV_FROM_REF(kwval));
172 3 break;
173  
174 1 case 8: /* hookdata */
175 1 data.hookdata = newSVsv(kwval);
176 1 break;
177 }
178  
179 3 ▶ if(!permit_hintkeysv && !data.permitcv)
Branch: 2 branches
BranchTrueFalseTotal
if(!permit_hintkeysv && !data.permitcv) 1 2 3
if(!permit_hintkeysv && !data.permitcv) 0 1 1
180 0 croak("Require at least one of 'permit_hintkey' or 'permit'");
181  
182 struct XSParseInfixHooks *hooksptr;
183 3 Newx(hooksptr, 1, struct XSParseInfixHooks);
184 3 *hooksptr = hooks;
185 3 ▶ if(wrapper_func_namesv)
Branch: 1 branch
BranchTrueFalseTotal
if(wrapper_func_namesv) 0 3 3
186 0 hooksptr->wrapper_func_name = savepv(SvPV_nolen(wrapper_func_namesv));
187 3 ▶ if(permit_hintkeysv)
Branch: 1 branch
BranchTrueFalseTotal
if(permit_hintkeysv) 2 1 3
188 2 hooksptr->permit_hintkey = savepv(SvPV_nolen(permit_hintkeysv));
189 3 ▶ if(data.permitcv)
Branch: 1 branch
BranchTrueFalseTotal
if(data.permitcv) 1 2 3
190 1 hooksptr->permit = &cb_permit;
191 3 ▶ if(data.new_opcv)
Branch: 1 branch
BranchTrueFalseTotal
if(data.new_opcv) 3 0 3
192 3 hooksptr->new_op = &cb_new_op;
193  
194 struct XPIFPHookdata *dataptr;
195 3 Newx(dataptr, 1, struct XPIFPHookdata);
196 3 *dataptr = data;
197  
198 3 register_xs_parse_infix(savepv(name), hooksptr, dataptr);
199  
200 BOOT:
201 3 boot_xs_parse_infix(0);
202  
203 3 S_setup_constants(aTHX);
« lib/XS/Parse/Keyword/FromPerl.xs ↑ Summary lib/Optree/Generate.xs »