File: infix.xs

package info (click to toggle)
libxs-parse-keyword-perl 0.48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 572 kB
  • sloc: ansic: 2,037; perl: 1,013; sh: 6; makefile: 3
file content (246 lines) | stat: -rw-r--r-- 4,938 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*  You may distribute under the terms of either the GNU General Public License
 *  or the Artistic License (the same terms as Perl itself)
 *
 *  (C) Paul Evans, 2021-2024 -- leonerd@leonerd.org.uk
 */

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "XSParseInfix.h"

#include "perl-backcompat.c.inc"

static const char hintkey[] = "t::infix/permit";

XOP xop_add;

OP *pp_add(pTHX)
{
  dSP;
  SV *right = POPs;
  SV *left  = POPs;
  mPUSHi(SvIV(left) + SvIV(right));
  RETURN;
}

static const struct XSParseInfixHooks hooks_add = {
  .cls = XPI_CLS_ADD_MISC,
  .permit_hintkey = hintkey,

  .wrapper_func_name = "t::infix::addfunc",

  .ppaddr = &pp_add,
};

OP *pp_mul(pTHX)
{
  croak("TODO"); /* We never actually call code with this so it doesn't matter */
}

static const struct XSParseInfixHooks hooks_mul = {
  .cls = XPI_CLS_MUL_MISC,
  .permit_hintkey = hintkey,

  .ppaddr = &pp_mul,
};

OP *pp_xor(pTHX)
{
  dSP;
  SV *right = POPs;
  SV *left  = POPs;
  mPUSHi(SvIV(left) ^ SvIV(right));
  RETURN;
}

static const struct XSParseInfixHooks hooks_xor = {
  .cls = XPI_CLS_ADD_MISC,
  .permit_hintkey = hintkey,

  .ppaddr = &pp_xor,
};

OP *pp_intersperse(pTHX)
{
  /* This isn't a very efficient implementation but we're not going for
   * efficiency here in this unit test
   */
  dSP;
  I32 markidx = POPMARK;
  I32 items = SP - PL_stack_base - markidx;

  SP -= items;
  SV *sep = *SP;

  AV *list = av_make(items, SP+1);
  SAVEFREESV((SV *)list);

  SP--;

  if(!items)
    RETURN;

  EXTEND(SP, 2*items - 1);
  PUSHs(*av_fetch(list, 0, TRUE));

  I32 i;
  for(i = 1; i < items; i++) {
    PUSHs(sv_mortalcopy(sep));
    PUSHs(*av_fetch(list, i, TRUE));
  }
  RETURN;
}

static const struct XSParseInfixHooks hooks_intersperse = {
  .cls = XPI_CLS_ADD_MISC,
  .rhs_flags = XPI_OPERAND_LIST,
  .permit_hintkey = hintkey,

  .wrapper_func_name = "t::infix::interspersefunc",

  .ppaddr = &pp_intersperse,
};

OP *pp_addpairs(pTHX)
{
  dSP;
  U32 rhs_mark = POPMARK;
  U32 lhs_mark = POPMARK;

  U32 rhs_count = SP - (PL_stack_base + rhs_mark);
  U32 lhs_count = rhs_mark - lhs_mark;

  SP = PL_stack_base + lhs_mark;

  SV **lhs = PL_stack_base + lhs_mark + 1;
  SV **rhs = PL_stack_base + rhs_mark + 1;

  PUSHMARK(SP);

  while(lhs_count || rhs_count) {
    IV val = SvIV(*lhs) + SvIV(*rhs);
    mPUSHi(val);

    lhs++; lhs_count--;
    rhs++; rhs_count--;
  }

  RETURN;
}

static const struct XSParseInfixHooks hooks_addpairs = {
  .cls = XPI_CLS_ADD_MISC,
  .lhs_flags = XPI_OPERAND_LIST,
  .rhs_flags = XPI_OPERAND_LIST|XPI_OPERAND_ONLY_LOOK, /* only on RHS so we can test the logic */
  .permit_hintkey = hintkey,

  .wrapper_func_name = "t::infix::addpairsfunc",

  .ppaddr = &pp_addpairs,
};

OP *pp_cat(pTHX)
{
  dSP;
  int n = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private;

  SV *ret = newSVpvs("^");
  SV **args = SP - n + 1;
  for(int i = 0; i < n; i++)
    sv_catsv(ret, args[i]);

  sv_catpvs(ret, "^");

  SP -= n;
  mPUSHs(ret);

  RETURN;
}

static const struct XSParseInfixHooks hooks_cat = {
  .cls = XPI_CLS_ADD_MISC,
  .flags = XPI_FLAG_LISTASSOC,
  .permit_hintkey = hintkey,

  .wrapper_func_name = "t::infix::catfunc",

  .ppaddr = &pp_cat,
};

OP *pp_LL(pTHX)
{
  dSP;
  int n = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private;

  if(n > 2)
    croak("TODO: unit test cannot cope with n > 2");

  U32 counts[2];
  SV **args[2];
  for(int listi = n-1; listi >= 0; listi--) {
    SV **mark = PL_stack_base + POPMARK;
    counts[listi] = SP - mark;
    args[listi] = mark + 1;
    SP = mark;
  }

  SV *ret = newSVpvs("(");

  for(int listi = 0; listi < n; listi++) {
    sv_catpvs(ret, "[");

    for(int argi = 0; argi < counts[listi]; argi++)
      sv_catsv(ret, args[listi][argi]);

    sv_catpvs(ret, "]");
  }

  sv_catpvs(ret, ")");

  mPUSHs(ret);
  RETURN;
}

static const struct XSParseInfixHooks hooks_LL = {
  .cls = XPI_CLS_ADD_MISC,
  .flags = XPI_FLAG_LISTASSOC,
  .lhs_flags = XPI_OPERAND_LIST|XPI_OPERAND_ONLY_LOOK,
  .rhs_flags = XPI_OPERAND_LIST|XPI_OPERAND_ONLY_LOOK,
  .permit_hintkey = hintkey,

  .wrapper_func_name = "t::infix::LLfunc",

  .ppaddr = &pp_LL,
};

OP *pp_fqadd(pTHX)
/* Like pp_add but we need a second address so as not to upset the deparse tests */
{
  return pp_add(aTHX);
}

static const struct XSParseInfixHooks hooks_fqadd = {
  .cls = XPI_CLS_ADD_MISC,
  .ppaddr = &pp_fqadd,
};

MODULE = t::infix  PACKAGE = t::infix

BOOT:
  boot_xs_parse_infix(0);

  register_xs_parse_infix("add", &hooks_add, NULL);
  register_xs_parse_infix("mul", &hooks_mul, NULL);

  register_xs_parse_infix("⊕", &hooks_xor, NULL);

  register_xs_parse_infix("intersperse", &hooks_intersperse, NULL);

  register_xs_parse_infix("addpairs", &hooks_addpairs, NULL);

  register_xs_parse_infix("cat", &hooks_cat, NULL);
  register_xs_parse_infix("LL", &hooks_LL, NULL);

  register_xs_parse_infix("t::infix::fqadd", &hooks_fqadd, NULL);