t/infix.xs

filter: Statementstmt 95.4% 84 / 88 Branchbran 75.0% 21 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.5% 105 / 116 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/keyword.c ↑ Summary blib/lib/XS/Parse/Keyword.pm »
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-2024 -- leonerd@leonerd.org.uk
5 */
6  
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10  
11 #include "XSParseInfix.h"
12  
13 #include "perl-backcompat.c.inc"
14  
15 static const char hintkey[] = "t::infix/permit";
16  
17 XOP xop_add;
18  
19 5 OP *pp_add(pTHX)
20 {
21 5 dSP;
22 5 SV *right = POPs;
23 5 SV *left = POPs;
24 5 mPUSHi(SvIV(left) + SvIV(right));
25 5 RETURN;
26 }
27  
28 static const struct XSParseInfixHooks hooks_add = {
29 .cls = XPI_CLS_ADD_MISC,
30 .permit_hintkey = hintkey,
31  
32 .wrapper_func_name = "t::infix::addfunc",
33  
34 .ppaddr = &pp_add,
35 };
36  
37 0 OP *pp_mul(pTHX)
38 {
39 0 croak("TODO"); /* We never actually call code with this so it doesn't matter */
40 }
41  
42 static const struct XSParseInfixHooks hooks_mul = {
43 .cls = XPI_CLS_MUL_MISC,
44 .permit_hintkey = hintkey,
45  
46 .ppaddr = &pp_mul,
47 };
48  
49 1 OP *pp_xor(pTHX)
50 {
51 1 dSP;
52 1 SV *right = POPs;
53 1 SV *left = POPs;
54 1 mPUSHi(SvIV(left) ^ SvIV(right));
55 1 RETURN;
56 }
57  
58 static const struct XSParseInfixHooks hooks_xor = {
59 .cls = XPI_CLS_ADD_MISC,
60 .permit_hintkey = hintkey,
61  
62 .ppaddr = &pp_xor,
63 };
64  
65 3 OP *pp_intersperse(pTHX)
66 {
67 /* This isn't a very efficient implementation but we're not going for
68 * efficiency here in this unit test
69 */
70 3 dSP;
71 3 I32 markidx = POPMARK;
72 3 I32 items = SP - PL_stack_base - markidx;
73  
74 3 SP -= items;
75 3 SV *sep = *SP;
76  
77 3 AV *list = av_make(items, SP+1);
78 3 SAVEFREESV((SV *)list);
79  
80 3 SP--;
81  
82 3 ▶ if(!items)
Branch: 1 branch
BranchTrueFalseTotal
if(!items) 0 3 3
83 0 RETURN;
84  
85 3 ▶ EXTEND(SP, 2*items - 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, 2*items - 1); 3 0 3
EXTEND(SP, 2*items - 1); 0 3 3
86 3 PUSHs(*av_fetch(list, 0, TRUE));
87  
88 I32 i;
89 8 ▶ for(i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 1; i < items; i++) { 5 3 8
90 5 PUSHs(sv_mortalcopy(sep));
91 5 PUSHs(*av_fetch(list, i, TRUE));
92 }
93 3 RETURN;
94 }
95  
96 static const struct XSParseInfixHooks hooks_intersperse = {
97 .cls = XPI_CLS_ADD_MISC,
98 .rhs_flags = XPI_OPERAND_LIST,
99 .permit_hintkey = hintkey,
100  
101 .wrapper_func_name = "t::infix::interspersefunc",
102  
103 .ppaddr = &pp_intersperse,
104 };
105  
106 9 OP *pp_addpairs(pTHX)
107 {
108 9 dSP;
109 9 U32 rhs_mark = POPMARK;
110 9 U32 lhs_mark = POPMARK;
111  
112 9 U32 rhs_count = SP - (PL_stack_base + rhs_mark);
113 9 U32 lhs_count = rhs_mark - lhs_mark;
114  
115 9 SP = PL_stack_base + lhs_mark;
116  
117 9 SV **lhs = PL_stack_base + lhs_mark + 1;
118 9 SV **rhs = PL_stack_base + rhs_mark + 1;
119  
120 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
121  
122 35 ▶ while(lhs_count || rhs_count) {
Branch: 2 branches
BranchTrueFalseTotal
while(lhs_count || rhs_count) { 26 9 35
while(lhs_count || rhs_count) { 0 9 9
123 26 IV val = SvIV(*lhs) + SvIV(*rhs);
124 26 mPUSHi(val);
125  
126 26 lhs++; lhs_count--;
127 26 rhs++; rhs_count--;
128 }
129  
130 9 RETURN;
131 }
132  
133 static const struct XSParseInfixHooks hooks_addpairs = {
134 .cls = XPI_CLS_ADD_MISC,
135 .lhs_flags = XPI_OPERAND_LIST,
136 .rhs_flags = XPI_OPERAND_LIST|XPI_OPERAND_ONLY_LOOK, /* only on RHS so we can test the logic */
137 .permit_hintkey = hintkey,
138  
139 .wrapper_func_name = "t::infix::addpairsfunc",
140  
141 .ppaddr = &pp_addpairs,
142 };
143  
144 5 OP *pp_cat(pTHX)
145 {
146 5 dSP;
147 5 ▶ int n = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private;
Branch: 1 branch
BranchTrueFalseTotal
int n = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private; 2 3 5
148  
149 5 SV *ret = newSVpvs("^");
150 5 SV **args = SP - n + 1;
151 19 ▶ for(int i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(int i = 0; i < n; i++) 14 5 19
152 14 sv_catsv(ret, args[i]);
153  
154 5 sv_catpvs(ret, "^");
155  
156 5 SP -= n;
157 5 mPUSHs(ret);
158  
159 5 RETURN;
160 }
161  
162 static const struct XSParseInfixHooks hooks_cat = {
163 .cls = XPI_CLS_ADD_MISC,
164 .flags = XPI_FLAG_LISTASSOC,
165 .permit_hintkey = hintkey,
166  
167 .wrapper_func_name = "t::infix::catfunc",
168  
169 .ppaddr = &pp_cat,
170 };
171  
172 1 OP *pp_LL(pTHX)
173 {
174 1 dSP;
175 1 ▶ int n = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private;
Branch: 1 branch
BranchTrueFalseTotal
int n = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private; 0 1 1
176  
177 1 ▶ if(n > 2)
Branch: 1 branch
BranchTrueFalseTotal
if(n > 2) 0 1 1
178 0 croak("TODO: unit test cannot cope with n > 2");
179  
180 U32 counts[2];
181 SV **args[2];
182 3 ▶ for(int listi = n-1; listi >= 0; listi--) {
Branch: 1 branch
BranchTrueFalseTotal
for(int listi = n-1; listi >= 0; listi--) { 2 1 3
183 2 SV **mark = PL_stack_base + POPMARK;
184 2 counts[listi] = SP - mark;
185 2 args[listi] = mark + 1;
186 2 SP = mark;
187 }
188  
189 1 SV *ret = newSVpvs("(");
190  
191 3 ▶ for(int listi = 0; listi < n; listi++) {
Branch: 1 branch
BranchTrueFalseTotal
for(int listi = 0; listi < n; listi++) { 2 1 3
192 2 sv_catpvs(ret, "[");
193  
194 4 ▶ for(int argi = 0; argi < counts[listi]; argi++)
Branch: 1 branch
BranchTrueFalseTotal
for(int argi = 0; argi < counts[listi]; argi++) 2 2 4
195 2 sv_catsv(ret, args[listi][argi]);
196  
197 2 sv_catpvs(ret, "]");
198 }
199  
200 1 sv_catpvs(ret, ")");
201  
202 1 mPUSHs(ret);
203 1 RETURN;
204 }
205  
206 static const struct XSParseInfixHooks hooks_LL = {
207 .cls = XPI_CLS_ADD_MISC,
208 .flags = XPI_FLAG_LISTASSOC,
209 .lhs_flags = XPI_OPERAND_LIST|XPI_OPERAND_ONLY_LOOK,
210 .rhs_flags = XPI_OPERAND_LIST|XPI_OPERAND_ONLY_LOOK,
211 .permit_hintkey = hintkey,
212  
213 .wrapper_func_name = "t::infix::LLfunc",
214  
215 .ppaddr = &pp_LL,
216 };
217  
218 2 OP *pp_fqadd(pTHX)
219 /* Like pp_add but we need a second address so as not to upset the deparse tests */
220 {
221 2 return pp_add(aTHX);
222 }
223  
224 static const struct XSParseInfixHooks hooks_fqadd = {
225 .cls = XPI_CLS_ADD_MISC,
226 .ppaddr = &pp_fqadd,
227 };
228  
229 MODULE = t::infix PACKAGE = t::infix
230  
231 BOOT:
232 2 boot_xs_parse_infix(0);
233  
234 2 register_xs_parse_infix("add", &hooks_add, NULL);
235 2 register_xs_parse_infix("mul", &hooks_mul, NULL);
236  
237 2 register_xs_parse_infix("⊕", &hooks_xor, NULL);
238  
239 2 register_xs_parse_infix("intersperse", &hooks_intersperse, NULL);
240  
241 2 register_xs_parse_infix("addpairs", &hooks_addpairs, NULL);
242  
243 2 register_xs_parse_infix("cat", &hooks_cat, NULL);
244 2 register_xs_parse_infix("LL", &hooks_LL, NULL);
245  
246 2 register_xs_parse_infix("t::infix::fqadd", &hooks_fqadd, NULL);
« src/keyword.c ↑ Summary blib/lib/XS/Parse/Keyword.pm »