File: Deparse.pm

package info (click to toggle)
libsyntax-keyword-match-perl 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: perl: 902; ansic: 34; makefile: 3
file content (246 lines) | stat: -rw-r--r-- 6,352 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, 2024 -- leonerd@leonerd.org.uk

package Syntax::Keyword::Match::Deparse 0.15;

use v5.14;
use warnings;

use B qw( opnumber OPf_KIDS OPf_STACKED );

require B::Deparse;

use constant {
   OP_AND         => opnumber('and'),
   OP_COND_EXPR   => opnumber('cond_expr'),
   OP_CUSTOM      => opnumber('custom'),
   OP_ENTER       => opnumber('enter'),
   OP_LINESEQ     => opnumber('lineseq'),
   OP_MATCH       => opnumber('match'),
   OP_NULL        => opnumber('null'),
   OP_OR          => opnumber('or'),
   OP_PADSV       => opnumber('padsv'),
   OP_PADSV_STORE => opnumber('padsv_store'),
   OP_SASSIGN     => opnumber('sassign'),
};

=head1 NAME

C<Syntax::Keyword::Match::Deparse> - L<B::Deparse> support for L<Syntax::Keyword::Match>

=head1 DESCRIPTION

Loading this module will apply some hacks onto L<B::Deparse> that attempts to
provide deparse support for code which uses the syntax provided by
L<Syntax::Keyword::Match>.

=cut

my $orig_pp_leave;
{
   no warnings 'redefine';
   no strict 'refs';
   $orig_pp_leave = *{"B::Deparse::pp_leave"}{CODE};
   *{"B::Deparse::pp_leave"} = \&pp_leave;
}

sub op_dump
{
   my $o = shift;
   my $ret = $o->name;

   my $kid = $o->flags & OPf_KIDS ? $o->first : undef;
   if( $kid && !B::Deparse::null($kid) ) {
      $ret .= "[\n";
      while( $kid && !B::Deparse::null($kid) ) {
         $ret .= join( "\n", map { "  $_" } split m/\n/, op_dump($kid) ) . "\n";
         $kid = $kid->sibling;
      }
      $ret .= "]";
   }

   return $ret;
}

my %operator_for_name = (
   eq    => "==",
   seq   => "eq",
   match => "=~",
   isa   => "isa",
);

sub operator_name
{
   my ( $o ) = @_;
   my $opname = $o->name;
   return $operator_for_name{$opname} // die "TODO: operator name of $opname";
}

sub is_match_on_topic
{
   my ( $o, $topicix ) = @_;

   $o->type == OP_MATCH or return 0;

   if( $^V ge v5.22.0 ) {
      # Perl 5.22 could do OP_MATCH on targ
      return $o->targ == $topicix;
   }
   elsif( $o->flags & OPf_STACKED ) {
      my $kid = $o->first;
      return $kid->type == OP_PADSV && $kid->targ == $topicix;
   }
   else {
      return 0;
   }
}

sub pp_leave
{
   my $self = shift;
   my ( $op ) = @_;

   my $enter = $op->first;
   $enter->type == OP_ENTER or
      return $self->$orig_pp_leave( @_ );

   my $assign = $enter->sibling;
   my $topicix; my $topicop;
   if( $^V ge v5.38.0 ) {
      # Since perl 5.38.0 we had OP_PADSV_STORE
      $assign->type == OP_PADSV_STORE or
         return $self->$orig_pp_leave( @_ );

      my $varname = $self->padname( $topicix = $assign->targ );
      $varname eq '$(Syntax::Keyword::Match/topic)' or
         return $self->$orig_pp_leave( @_ );

      $topicop = $assign->first;
   }
   else {
      # Earlier perls had regular OP_SASSIGN with OP_PADSV target
      $assign->type == OP_SASSIGN or
         return $self->$orig_pp_leave( @_ );

      $topicop = $assign->first;

      my $padsvop = $topicop->sibling;
      $padsvop->type == OP_PADSV or
         return $self->$orig_pp_leave( @_ );

      my $varname = $self->padname( $topicix = $padsvop->targ );
      $varname eq '$(Syntax::Keyword::Match/topic)' or
         return $self->$orig_pp_leave( @_ );
   }

   my $cmpop;
   my @caseblocks;
   my $kid = $assign->sibling;
   while( !B::Deparse::null($kid) ) {
      if( $kid->type == OP_NULL ) {
         $kid = $kid->first;
      }

      if( $kid->type == OP_LINESEQ ) {
         push @caseblocks, "default {" . B::Deparse::scopeop( 1, $self, $kid, 0 ) . "}";
         last;
      }

      my ( $condop, $block );
      if( $kid->type == OP_COND_EXPR ) {
         $condop = $kid->first;
         $block = $condop->sibling;
         $kid = $block->sibling;
      }
      elsif( $kid->type == OP_AND ) {
         $condop = $kid->first;
         $block = $condop->sibling;
         $kid = $block->sibling; # should be NULL
      }
      else {
         warn op_dump($kid);
         die "TODO: not sure how to handle kid=", $kid->name;
      }

      my @cases;
      while( $condop and !B::Deparse::null($condop) ) {
         if( $condop->type == OP_NULL ) {
            $condop = $condop->first;
         }

         my $cond1;
         if( $condop->type == OP_OR ) {
            $cond1 = $condop->first;
            $condop = $cond1->sibling;
         }
         else {
            $cond1 = $condop;
            $condop = undef;
         }

         my $condlhs = $cond1->first;
         if( is_match_on_topic( $cond1, $topicix ) ) {
            die "Unsure how to handle mismatched case cond ops"
               if $cmpop and $cmpop->type != $cond1->type;
            $cmpop //= $cond1;
            # Need to mangle out the target name
            my $pattern = $self->deparse( $cond1, @_ );
            $pattern =~ s{^\(\$\(Syntax::Keyword::Match/topic\) =~ (.*)\)$}{m$1};
            push @cases, "case ($pattern)";
         }
         elsif( !B::Deparse::null($condlhs) and $condlhs->type == OP_PADSV and $condlhs->targ == $topicix ) {
            # There's no way perl code could see the topic padname, so this
            # must be a plain case(EXPR)
            my $condval = $condlhs->sibling;

            # TODO: custom ops might be weird
            die "Unsure how to handle mismatched case cond ops"
               if $cmpop and $cmpop->type != $cond1->type;
            $cmpop //= $cond1;

            push @cases, "case (" . $self->deparse( $condval, @_ ) . ")";
         }
         else {
            my $cond = $self->deparse( $cond1, @_ );
            $cond =~ s/^\((.*)\)$/$1/; # trim surrounding () so we don't get two
            push @cases, "case if ($cond)";
         }
      }

      push @caseblocks, join( ", ", @cases ) .
         " {" . B::Deparse::scopeop( 1, $self, $block, 0 ) . "}";
   }

   my $topic = $self->deparse( $topicop, @_ );

   # Ugh it'd be great if B::Deparse had a solution to this
   my $cmp = operator_name( $cmpop );

   return "match ($topic : $cmp) {\n\t" . join( "\n", @caseblocks ) . "\n\b}";
}

=head1 TODO

=over 4

=item *

Integrate with custom ops (C<equ>, C<eqr>, etc..)

=item *

Handle the experimental dispatch op feature

=back

=cut

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;