Skip to content

Commit cd7d784

Browse files
committed
Define a PL_infix_plugin hook, of a similar style to PL_keyword_plugin
Runs for identifier-named custom infix operators and sequences of non-identifier symbol characters. Defines multiple precedence levels for custom infix operators that fit alongside exponentiation, multiplication, addition, or relational comparision operators, as well as a "high" and "low" at either end.
1 parent fe31205 commit cd7d784

File tree

11 files changed

+1566
-1261
lines changed

11 files changed

+1566
-1261
lines changed

embed.fnc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,7 @@ Apd |void |cv_set_call_checker_flags|NN CV *cv \
15721572
|NN Perl_call_checker ckfun \
15731573
|NN SV *ckobj|U32 ckflags
15741574
Apd |void |wrap_op_checker|Optype opcode|NN Perl_check_t new_checker|NN Perl_check_t *old_checker_p
1575+
Axpd |void |wrap_infix_plugin|NN Perl_infix_plugin_t new_plugin|NN Perl_infix_plugin_t *old_plugin_p
15751576
Axpd |void |wrap_keyword_plugin|NN Perl_keyword_plugin_t new_plugin|NN Perl_keyword_plugin_t *old_plugin_p
15761577
CpR |PERL_SI*|new_stackinfo|I32 stitems|I32 cxitems
15771578
Apd |char* |scan_vstring |NN const char *s|NN const char *const e \
@@ -1600,6 +1601,7 @@ p |void |opslab_force_free|NN OPSLAB *slab
16001601
#endif
16011602
: Used in perly.y
16021603
p |void |package |NN OP* o
1604+
p |OP * |build_infix_plugin|NN OP *lhs|NN OP *rhs|NN void *tokendata
16031605
: Used in perly.y
16041606
p |void |package_version|NN OP* v
16051607
: Used in toke.c and perly.y
@@ -3821,6 +3823,8 @@ Apxd |void|cop_store_label \
38213823

38223824
epo |int |keyword_plugin_standard|NN char* keyword_ptr|STRLEN keyword_len|NN OP** op_ptr
38233825

3826+
epo |STRLEN |infix_plugin_standard|NN char* operator_ptr|STRLEN operator_len|NN struct Perl_custom_infix** def
3827+
38243828
#if defined(USE_ITHREADS)
38253829
# if defined(PERL_IN_SV_C)
38263830
S |void |unreferenced_to_tmp_stack|NN AV *const unreferenced

embed.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@
761761
#define whichsig_pv(a) Perl_whichsig_pv(aTHX_ a)
762762
#define whichsig_pvn(a,b) Perl_whichsig_pvn(aTHX_ a,b)
763763
#define whichsig_sv(a) Perl_whichsig_sv(aTHX_ a)
764+
#define wrap_infix_plugin(a,b) Perl_wrap_infix_plugin(aTHX_ a,b)
764765
#define wrap_keyword_plugin(a,b) Perl_wrap_keyword_plugin(aTHX_ a,b)
765766
#define wrap_op_checker(a,b,c) Perl_wrap_op_checker(aTHX_ a,b,c)
766767
#if !(defined(PERL_USE_3ARG_SIGHANDLER))
@@ -1226,6 +1227,7 @@
12261227
#define boot_core_UNIVERSAL() Perl_boot_core_UNIVERSAL(aTHX)
12271228
#define boot_core_builtin() Perl_boot_core_builtin(aTHX)
12281229
#define boot_core_mro() Perl_boot_core_mro(aTHX)
1230+
#define build_infix_plugin(a,b,c) Perl_build_infix_plugin(aTHX_ a,b,c)
12291231
#define cando(a,b,c) Perl_cando(aTHX_ a,b,c)
12301232
#define check_utf8_print(a,b) Perl_check_utf8_print(aTHX_ a,b)
12311233
#define ck_anoncode(a) Perl_ck_anoncode(aTHX_ a)

perl.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6003,10 +6003,27 @@ typedef void (*XSINIT_t) (pTHX);
60036003
typedef void (*ATEXIT_t) (pTHX_ void*);
60046004
typedef void (*XSUBADDR_t) (pTHX_ CV *);
60056005

6006+
/* TODO: find somewhere to store this */
6007+
enum Perl_custom_infix_precedence {
6008+
INFIX_PREC_LOW = 10, /* non-associative */
6009+
INFIX_PREC_REL = 30, /* non-associative, just below `==` */
6010+
INFIX_PREC_ADD = 50, /* left-associative, same precedence as `+` */
6011+
INFIX_PREC_MUL = 70, /* left-associative, same precedence as `*` */
6012+
INFIX_PREC_POW = 90, /* right-associative, same precedence as `**` */
6013+
INFIX_PREC_HIGH = 110, /* non-associative */
6014+
};
6015+
struct Perl_custom_infix;
6016+
struct Perl_custom_infix {
6017+
enum Perl_custom_infix_precedence prec;
6018+
void (*parse)(pTHX_ SV **opdata, struct Perl_custom_infix *); /* optional */
6019+
OP *(*build_op)(pTHX_ SV **opdata, OP *lhs, OP *rhs, struct Perl_custom_infix *);
6020+
};
6021+
60066022
typedef OP* (*Perl_ppaddr_t)(pTHX);
60076023
typedef OP* (*Perl_check_t) (pTHX_ OP*);
60086024
typedef void(*Perl_ophook_t)(pTHX_ OP*);
60096025
typedef int (*Perl_keyword_plugin_t)(pTHX_ char*, STRLEN, OP**);
6026+
typedef STRLEN (*Perl_infix_plugin_t)(pTHX_ char*, STRLEN, struct Perl_custom_infix **);
60106027
typedef void(*Perl_cpeep_t)(pTHX_ OP *, OP *);
60116028

60126029
typedef void(*globhook_t)(pTHX);

perlvars.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,12 @@ Instead, use the function L</wrap_keyword_plugin>.
235235
*/
236236

237237
#if defined(USE_ITHREADS)
238-
PERLVAR(G, keyword_plugin_mutex, perl_mutex) /* Mutex for PL_keyword_plugin */
238+
PERLVAR(G, keyword_plugin_mutex, perl_mutex) /* Mutex for PL_keyword_plugin and PL_infix_plugin */
239239
#endif
240240
PERLVARI(G, keyword_plugin, Perl_keyword_plugin_t, Perl_keyword_plugin_standard)
241241

242+
PERLVARI(G, infix_plugin, Perl_infix_plugin_t, Perl_infix_plugin_standard)
243+
242244
PERLVARI(G, op_sequence, HV *, NULL) /* dump.c */
243245
PERLVARI(G, op_seq, UV, 0) /* dump.c */
244246

0 commit comments

Comments
 (0)