XS::Parse::Infix::FromPerl#

📦 std

New infix operators written in Perl: register_xs_parse_infix names an operator, its precedence class (XPI_CLS_ADD_MISC for addition’s, and the other XPI_CLS_* the module exports), and a new_op sub that gets the two operands’ optrees and returns the operator’s, made with Optree::Generate. The operator is active where its permit_hintkey is set in %^H, or where its permit sub says; a wrapper_func_name also makes a function that applies it.

This is a native implementation of XS-Parse-Keyword-FromPerl 0.10’s Infix/FromPerl.xs over the XS::Parse::Keyword engine; its XS/Parse/Infix/FromPerl.pm is used unchanged.

Synopsis#

use XS::Parse::Infix::FromPerl qw(register_xs_parse_infix XPI_CLS_ADD_MISC);
use Optree::Generate qw(newBINOP OP_ADD);

BEGIN {
    register_xs_parse_infix(add =>
        cls            => XPI_CLS_ADD_MISC,
        permit_hintkey => "main/add",
        new_op         => sub {
            my ($flags, $lhs, $rhs) = @_;
            return newBINOP(OP_ADD, 0, $lhs, $rhs);
        },
    );
}
BEGIN { $^H{"main/add"}++ }
my ($x, $y) = (1, 2);
print $x add $y, "\n";    # 3

It binds the engine as XS consumers do, by boot_xs_parse_infix.

Functions#

Infix operators#

register_xs_parse_infix#

register_xs_parse_infix($name, %args): make $name an infix operator. The arguments are cls (its precedence class), permit_hintkey and permit (where it is active; at least one is needed), new_op (gets the flags, the two operands as B::OP objects, an undef and hookdata, and returns the operator’s optree), wrapper_func_name and hookdata. flags, lhs_flags and rhs_flags are not supported and croak.