| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use v5.14; |
| 4 |
use warnings; |
| 5 |
use utf8; |
| 6 |
|
| 7 |
use Test2::V0; |
| 8 |
|
| 9 |
use B::Deparse; |
| 10 |
my $deparser = B::Deparse->new( "-p" ); |
| 11 |
|
| 12 |
use lib "t"; |
| 13 |
use testcase "t::infix"; |
| 14 |
|
| 15 |
BEGIN { plan skip_all => "No PL_infix_plugin" unless XS::Parse::Infix::HAVE_PL_INFIX_PLUGIN; } |
| 16 |
|
| 17 |
use feature 'current_sub'; |
| 18 |
|
| 19 |
BEGIN { $^H{"t::infix/permit"} = 1; } |
| 20 |
|
| 21 |
{ |
| 22 |
my $result = 10 add 20; |
| 23 |
is( $result, 30, 'add infix operator' ); |
| 24 |
|
| 25 |
$result = 15 ⊕ 20; |
| 26 |
is( $result, 27, 'xor infix operator' ); |
| 27 |
|
| 28 |
my $aref = ["|" intersperse qw( a b c )]; |
| 29 |
is( $aref, [qw( a | b | c )], |
| 30 |
'intersperse infix operator' ); |
| 31 |
|
| 32 |
my @list = qw( x y z ); |
| 33 |
$aref = ["|" intersperse @list]; |
| 34 |
is( $aref, [qw( x | y | z )], |
| 35 |
'intersperse infix operator on PADAV' ); |
| 36 |
|
| 37 |
is( [ (2, 4, 6) addpairs (1, 1, 1) ], |
| 38 |
[ 3, 5, 7 ], 'addpairs infix operator' ); |
| 39 |
} |
| 40 |
|
| 41 |
# fully-qualified + lexical alias |
| 42 |
{ |
| 43 |
BEGIN { t::infix->XS::Parse::Infix::apply_infix( 1, [ "fqadd" ], qw( fqadd ) ); } |
| 44 |
|
| 45 |
my $result = 5 fqadd 10; |
| 46 |
is( $result, 15, 'fqadd infix operator' ); |
| 47 |
} |
| 48 |
|
| 49 |
{ |
| 50 |
my @importargs; |
| 51 |
BEGIN { |
| 52 |
@importargs = ( 123, fqadd => { -as => "localname" }, 456 ); |
| 53 |
t::infix->XS::Parse::Infix::apply_infix( 1, \@importargs, qw( fqadd ) ); } |
| 54 |
|
| 55 |
my $result = 6 localname 12; |
| 56 |
is( $result, 18, 'infix operator renamed' ); |
| 57 |
|
| 58 |
is( \@importargs, [ 123, 456 ], 'apply_infix correctly mutated import args' ); |
| 59 |
} |
| 60 |
|
| 61 |
sub _getoptree |
| 62 |
{ |
| 63 |
my ( $sub ) = @_; |
| 64 |
|
| 65 |
# Ugh - this would be so much neater if we could pass coderefs into |
| 66 |
# B::walkoptree directly |
| 67 |
# Additionally there's no pre-mid-postfix walk options :( |
| 68 |
|
| 69 |
my $dump_optree = sub { |
| 70 |
my $sub = __SUB__; |
| 71 |
my ( $op ) = @_; |
| 72 |
my $opname = $op->name; |
| 73 |
|
| 74 |
# Avoid test-dependence on the actual ppaddr by mangling out the name |
| 75 |
$opname =~ s/0x[[:xdigit:]]+/0xXXX/; |
| 76 |
|
| 77 |
return $op->first->$sub if $opname eq "null"; |
| 78 |
|
| 79 |
my @kids; |
| 80 |
if( $op->flags & B::OPf_KIDS ) { |
| 81 |
my $kid = $op->first; |
| 82 |
while( $kid ) { |
| 83 |
push @kids, $kid->$sub; |
| 84 |
|
| 85 |
$kid = $kid->sibling; undef $kid if ref($kid) eq "B::NULL"; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
my $ret = $opname; |
| 90 |
$ret .= "[" . join( ", ", @kids ) . "]" if @kids; |
| 91 |
return $ret; |
| 92 |
}; |
| 93 |
|
| 94 |
# Reach inside to the first statement |
| 95 |
return B::svref_2object( $sub )->ROOT->first->first->sibling |
| 96 |
->$dump_optree; |
| 97 |
} |
| 98 |
|
| 99 |
sub is_optree |
| 100 |
{ |
| 101 |
my ( $sub, $exp, $name ) = @_; |
| 102 |
is( _getoptree( $sub ), $exp, $name ); |
| 103 |
} |
| 104 |
|
| 105 |
{ |
| 106 |
is_optree sub { $_[0] add $_[1] }, |
| 107 |
"infix_add_0xXXX[aelemfast, aelemfast]", |
| 108 |
'optree of call to infix operator'; |
| 109 |
|
| 110 |
# Check precedence of operator parsing by observing the following precedence |
| 111 |
# ordering: |
| 112 |
# <--High Low--> |
| 113 |
# ** * + && |
| 114 |
|
| 115 |
is_optree sub { $_[0] * $_[1] add $_[2] * $_[3] }, |
| 116 |
"infix_add_0xXXX[multiply[aelemfast, aelemfast], multiply[aelemfast, aelemfast]]", |
| 117 |
'optree binds add lower than *'; |
| 118 |
is_optree sub { $_[0] + $_[1] add $_[2] + $_[3] }, |
| 119 |
"add[infix_add_0xXXX[add[aelemfast, aelemfast], aelemfast], aelemfast]", |
| 120 |
'optree binds add equal to +'; |
| 121 |
is_optree sub { $_[0] && $_[1] add $_[2] && $_[3] }, |
| 122 |
"and[and[aelemfast, infix_add_0xXXX[aelemfast, aelemfast]], aelemfast]", |
| 123 |
'optree binds add higher than &&'; |
| 124 |
|
| 125 |
is_optree sub { $_[0] ** $_[1] mul $_[2] ** $_[3] }, |
| 126 |
"infix_mul_0xXXX[pow[aelemfast, aelemfast], pow[aelemfast, aelemfast]]", |
| 127 |
'optree binds mul lower than **'; |
| 128 |
is_optree sub { $_[0] * $_[1] mul $_[2] * $_[3] }, |
| 129 |
"multiply[infix_mul_0xXXX[multiply[aelemfast, aelemfast], aelemfast], aelemfast]", |
| 130 |
'optree binds mul equal to *'; |
| 131 |
is_optree sub { $_[0] + $_[1] mul $_[2] + $_[3] }, |
| 132 |
"add[add[aelemfast, infix_mul_0xXXX[aelemfast, aelemfast]], aelemfast]", |
| 133 |
'optree binds mul higher than +'; |
| 134 |
|
| 135 |
is_optree sub { $_[0] * ($_[1] add $_[2]) * $_[3] }, |
| 136 |
"multiply[multiply[aelemfast, infix_add_0xXXX[aelemfast, aelemfast]], aelemfast]", |
| 137 |
'optree of call to infix operator at forced precedence'; |
| 138 |
} |
| 139 |
|
| 140 |
sub is_deparsed |
| 141 |
{ |
| 142 |
my ( $sub, $exp, $name ) = @_; |
| 143 |
|
| 144 |
my $got = $deparser->coderef2text( $sub ); |
| 145 |
|
| 146 |
# Deparsed output is '{ ... }'-wrapped |
| 147 |
$got = ( $got =~ m/^{\n(.*)\n}$/s )[0]; |
| 148 |
|
| 149 |
# Deparsed output will have a lot of pragmata and so on; just grab the |
| 150 |
# final line |
| 151 |
$got = ( split m/\n/, $got )[-1]; |
| 152 |
$got =~ s/^\s+//; |
| 153 |
|
| 154 |
is( $got, $exp, $name ); |
| 155 |
} |
| 156 |
|
| 157 |
{ |
| 158 |
is_deparsed sub { $_[0] add $_[1] }, |
| 159 |
'$_[0] add $_[1];', |
| 160 |
'deparsed call to infix add operator'; |
| 161 |
|
| 162 |
is_deparsed sub { $_[0] * $_[1] add $_[2] * $_[3] }, |
| 163 |
'($_[0] * $_[1]) add ($_[2] * $_[3]);', |
| 164 |
'deparsed call to infix add operator at default precedence'; |
| 165 |
|
| 166 |
is_deparsed sub { $_[0] ⊕ $_[1] }, |
| 167 |
'$_[0] ⊕ $_[1];', |
| 168 |
'deparsed operator yields UTF-8'; |
| 169 |
|
| 170 |
is_deparsed sub { "+" intersperse (1,2,3) }, |
| 171 |
q['+' intersperse (1, 2, 3);], |
| 172 |
'deparsed call to infix operator with list RHS'; |
| 173 |
|
| 174 |
is_deparsed sub { (1,2,3) addpairs (4,5,6) }, |
| 175 |
'(1, 2, 3) addpairs (4, 5, 6);', |
| 176 |
'deparsed call to infix list/list operator'; |
| 177 |
} |
| 178 |
|
| 179 |
# list-associative operator |
| 180 |
{ |
| 181 |
is( "a" cat "b" cat "c", "^abc^", |
| 182 |
'cat operator runs correctly' ); |
| 183 |
|
| 184 |
is_optree sub { "a" cat "b" cat "c" }, |
| 185 |
"infix_cat_0xXXX[const, const, const]", |
| 186 |
'optree of list-associative cat operator'; |
| 187 |
|
| 188 |
is_optree sub { ( "a" cat "b" ) cat "c" }, |
| 189 |
"infix_cat_0xXXX[infix_cat_0xXXX[const, const], const]", |
| 190 |
'parens on LHS defeat list-associativity'; |
| 191 |
is_optree sub { "a" cat ( "b" cat "c" ) }, |
| 192 |
"infix_cat_0xXXX[const, infix_cat_0xXXX[const, const]]", |
| 193 |
'parens on RHS defeat list-associativity'; |
| 194 |
|
| 195 |
is_deparsed sub { "a" cat "b" cat "c" }, |
| 196 |
q['a' cat 'b' cat 'c';], |
| 197 |
'deparsed list-associative cat operator'; |
| 198 |
} |
| 199 |
|
| 200 |
done_testing; |