Debian Codegraph @

t/infix.xs

raw
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 -- [email protected]
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 OP *pp_add(pTHX)
20 {
21 dSP;
22 SV *right = POPs;
23 SV *left = POPs;
24 mPUSHi(SvIV(left) + SvIV(right));
25 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 OP *pp_mul(pTHX)
38 {
39 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 OP *pp_xor(pTHX)
50 {
51 dSP;
52 SV *right = POPs;
53 SV *left = POPs;
54 mPUSHi(SvIV(left) ^ SvIV(right));
55 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 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 dSP;
71 I32 markidx = POPMARK;
72 I32 items = SP - PL_stack_base - markidx;
73
74 SP -= items;
75 SV *sep = *SP;
76
77 AV *list = av_make(items, SP+1);
78 SAVEFREESV((SV *)list);
79
80 SP--;
81
82 if(!items)
83 RETURN;
84
85 EXTEND(SP, 2*items - 1);
86 PUSHs(*av_fetch(list, 0, TRUE));
87
88 I32 i;
89 for(i = 1; i < items; i++) {
90 PUSHs(sv_mortalcopy(sep));
91 PUSHs(*av_fetch(list, i, TRUE));
92 }
93 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 OP *pp_addpairs(pTHX)
107 {
108 dSP;
109 U32 rhs_mark = POPMARK;
110 U32 lhs_mark = POPMARK;
111
112 U32 rhs_count = SP - (PL_stack_base + rhs_mark);
113 U32 lhs_count = rhs_mark - lhs_mark;
114
115 SP = PL_stack_base + lhs_mark;
116
117 SV **lhs = PL_stack_base + lhs_mark + 1;
118 SV **rhs = PL_stack_base + rhs_mark + 1;
119
120 PUSHMARK(SP);
121
122 while(lhs_count || rhs_count) {
123 IV val = SvIV(*lhs) + SvIV(*rhs);
124 mPUSHi(val);
125
126 lhs++; lhs_count--;
127 rhs++; rhs_count--;
128 }
129
130 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 OP *pp_cat(pTHX)
145 {
146 dSP;
147 int n = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private;
148
149 SV *ret = newSVpvs("^");
150 SV **args = SP - n + 1;
151 for(int i = 0; i < n; i++)
152 sv_catsv(ret, args[i]);
153
154 sv_catpvs(ret, "^");
155
156 SP -= n;
157 mPUSHs(ret);
158
159 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 OP *pp_LL(pTHX)
173 {
174 dSP;
175 int n = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private;
176
177 if(n > 2)
178 croak("TODO: unit test cannot cope with n > 2");
179
180 U32 counts[2];
181 SV **args[2];
182 for(int listi = n-1; listi >= 0; listi--) {
183 SV **mark = PL_stack_base + POPMARK;
184 counts[listi] = SP - mark;
185 args[listi] = mark + 1;
186 SP = mark;
187 }
188
189 SV *ret = newSVpvs("(");
190
191 for(int listi = 0; listi < n; listi++) {
192 sv_catpvs(ret, "[");
193
194 for(int argi = 0; argi < counts[listi]; argi++)
195 sv_catsv(ret, args[listi][argi]);
196
197 sv_catpvs(ret, "]");
198 }
199
200 sv_catpvs(ret, ")");
201
202 mPUSHs(ret);
203 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 OP *pp_fqadd(pTHX)
219 /* Like pp_add but we need a second address so as not to upset the deparse tests */
220 {
221 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 boot_xs_parse_infix(0);
233
234 register_xs_parse_infix("add", &hooks_add, NULL);
235 register_xs_parse_infix("mul", &hooks_mul, NULL);
236
237 register_xs_parse_infix("⊕", &hooks_xor, NULL);
238
239 register_xs_parse_infix("intersperse", &hooks_intersperse, NULL);
240
241 register_xs_parse_infix("addpairs", &hooks_addpairs, NULL);
242
243 register_xs_parse_infix("cat", &hooks_cat, NULL);
244 register_xs_parse_infix("LL", &hooks_LL, NULL);
245
246 register_xs_parse_infix("t::infix::fqadd", &hooks_fqadd, NULL);