| 1 | package Perlito5::Perl5::PrettyPrinter; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # my %dispatch = ( | ||
| 6 | # stmt => \&statement, # if (expr) {stms} | ||
| 7 | # stmt_modifier => \&statement_modifier, # stmt if expr | ||
| 8 | # block => \&block, # {stmts} | ||
| 9 | # keyword => \&keyword, # if | ||
| 10 | # bareword => \&bareword, # main | ||
| 11 | # number => \&number, # number | ||
| 12 | # op => \&op, # expr | ||
| 13 | # paren => \&paren, # (expr) | ||
| 14 | # paren_semicolon => \&paren_semicolon, # (expr;expr;expr) | ||
| 15 | # apply => \&apply, # subr(expr) | ||
| 16 | # call => \&call, # expr->subr(expr) | ||
| 17 | # comment => \&comment, # # comment | ||
| 18 | # label => \&label, # L1: | ||
| 19 | # ); | ||
| 20 | |||
| 21 | # XXX - TODO - workaround initialization order in the javascript backend | ||
| 22 | my %dispatch = ( | ||
| 23 | stmt => sub { statement(@_) }, # if (expr) {stms} | ||
| 24 | stmt_modifier => sub { statement_modifier(@_) }, # stmt if expr | ||
| 25 | block => sub { block(@_) }, # {stmts} | ||
| 26 | keyword => sub { keyword(@_) }, # if | ||
| 27 | bareword => sub { bareword(@_) }, # main | ||
| 28 | number => sub { number(@_) }, # number | ||
| 29 | op => sub { op(@_) }, # expr | ||
| 30 | paren => sub { paren(@_) }, # (expr) | ||
| 31 | paren_semicolon => sub { paren_semicolon(@_) }, # (expr;expr;expr) | ||
| 32 | apply => sub { apply(@_) }, # subr(expr) | ||
| 33 | call => sub { call(@_) }, # expr->subr(expr) | ||
| 34 | comment => sub { comment(@_) }, # # comment | ||
| 35 | label => sub { label(@_) }, # L1: | ||
| 36 | ); | ||
| 37 | |||
| 38 | my %pair = ( | ||
| 39 | '(' => ')', | ||
| 40 | '[' => ']', | ||
| 41 | '{' => '}', | ||
| 42 | '<' => '>', | ||
| 43 | ); | ||
| 44 | |||
| 45 | our %op = ( | ||
| 46 | 'prefix:<$>' => { fix => 'deref', prec => 0, str => '$' }, | ||
| 47 | 'prefix:<@>' => { fix => 'deref', prec => 0, str => '@' }, | ||
| 48 | 'prefix:<%>' => { fix => 'deref', prec => 0, str => '%' }, | ||
| 49 | 'prefix:<&>' => { fix => 'deref', prec => 0, str => '&' }, | ||
| 50 | 'prefix:<*>' => { fix => 'deref', prec => 0, str => '*' }, | ||
| 51 | 'prefix:<$#>' => { fix => 'deref', prec => 0, str => '$#' }, | ||
| 52 | |||
| 53 | 'circumfix:<[ ]>' => { fix => 'circumfix', prec => 0, str => '[' }, | ||
| 54 | 'circumfix:<{ }>' => { fix => 'circumfix', prec => 0, str => '{' }, | ||
| 55 | 'circumfix:<( )>' => { fix => 'circumfix', prec => 0, str => '(' }, | ||
| 56 | |||
| 57 | 'infix:<->>' => { fix => 'infix', prec => -1, str => '->' }, | ||
| 58 | |||
| 59 | 'prefix:<-->' => { fix => 'prefix', prec => 1, str => '--' }, | ||
| 60 | 'prefix:<++>' => { fix => 'prefix', prec => 1, str => '++' }, | ||
| 61 | 'postfix:<-->' => { fix => 'postfix', prec => 1, str => '--' }, | ||
| 62 | 'postfix:<++>' => { fix => 'postfix', prec => 1, str => '++' }, | ||
| 63 | |||
| 64 | 'infix:<**>' => { fix => 'infix', prec => 2, str => '**' }, | ||
| 65 | |||
| 66 | 'prefix:<\\>' => { fix => 'prefix', prec => 3, str => '\\' }, | ||
| 67 | 'prefix:<+>' => { fix => 'prefix', prec => 3, str => '+' }, | ||
| 68 | 'prefix:<->' => { fix => 'prefix', prec => 3, str => '-' }, | ||
| 69 | 'prefix:<~>' => { fix => 'prefix', prec => 3, str => '~' }, | ||
| 70 | 'prefix:<!>' => { fix => 'prefix', prec => 3, str => '!' }, | ||
| 71 | |||
| 72 | 'infix:<=~>' => { fix => 'infix', prec => 4, str => ' =~ ' }, | ||
| 73 | 'infix:<!~>' => { fix => 'infix', prec => 4, str => ' !~ ' }, | ||
| 74 | |||
| 75 | 'infix:<*>' => { fix => 'infix', prec => 5, str => ' * ' }, | ||
| 76 | 'infix:</>' => { fix => 'infix', prec => 5, str => ' / ' }, | ||
| 77 | 'infix:<%>' => { fix => 'infix', prec => 5, str => ' % ' }, | ||
| 78 | 'infix:<x>' => { fix => 'infix', prec => 5, str => ' x ' }, | ||
| 79 | |||
| 80 | 'infix:<+>' => { fix => 'infix', prec => 6, str => ' + ' }, | ||
| 81 | 'infix:<->' => { fix => 'infix', prec => 6, str => ' - ' }, | ||
| 82 | 'list:<.>' => { fix => 'list', prec => 6, str => ' . ' }, | ||
| 83 | |||
| 84 | 'infix:<<<>' => { fix => 'infix', prec => 7, str => ' << ' }, | ||
| 85 | 'infix:<>>>' => { fix => 'infix', prec => 7, str => ' >> ' }, | ||
| 86 | |||
| 87 | # named unary - see below | ||
| 88 | |||
| 89 | 'infix:<lt>' => { fix => 'infix', prec => 9, str => ' lt ' }, | ||
| 90 | 'infix:<le>' => { fix => 'infix', prec => 9, str => ' le ' }, | ||
| 91 | 'infix:<gt>' => { fix => 'infix', prec => 9, str => ' gt ' }, | ||
| 92 | 'infix:<ge>' => { fix => 'infix', prec => 9, str => ' ge ' }, | ||
| 93 | 'infix:<<=>' => { fix => 'infix', prec => 9, str => ' <= ' }, | ||
| 94 | 'infix:<>=>' => { fix => 'infix', prec => 9, str => ' >= ' }, | ||
| 95 | 'infix:<<>' => { fix => 'infix', prec => 9, str => ' < ' }, | ||
| 96 | 'infix:<>>' => { fix => 'infix', prec => 9, str => ' > ' }, | ||
| 97 | |||
| 98 | 'infix:<<=>>' => { fix => 'infix', prec => 10, str => ' <=> ' }, | ||
| 99 | 'infix:<cmp>' => { fix => 'infix', prec => 10, str => ' cmp ' }, | ||
| 100 | 'infix:<==>' => { fix => 'infix', prec => 10, str => ' == ' }, | ||
| 101 | 'infix:<!=>' => { fix => 'infix', prec => 10, str => ' != ' }, | ||
| 102 | 'infix:<ne>' => { fix => 'infix', prec => 10, str => ' ne ' }, | ||
| 103 | 'infix:<eq>' => { fix => 'infix', prec => 10, str => ' eq ' }, | ||
| 104 | |||
| 105 | 'infix:<&>' => { fix => 'infix', prec => 11, str => ' & ' }, | ||
| 106 | |||
| 107 | 'infix:<|>' => { fix => 'infix', prec => 12, str => ' | ' }, | ||
| 108 | 'infix:<^>' => { fix => 'infix', prec => 12, str => ' ^ ' }, | ||
| 109 | |||
| 110 | 'infix:<..>' => { fix => 'infix', prec => 13, str => ' .. ' }, | ||
| 111 | 'infix:<...>' => { fix => 'infix', prec => 13, str => ' ... ' }, | ||
| 112 | 'infix:<~~>' => { fix => 'infix', prec => 13, str => ' ~~ ' }, | ||
| 113 | |||
| 114 | 'infix:<&&>' => { fix => 'infix', prec => 14, str => ' && ' }, | ||
| 115 | |||
| 116 | 'infix:<||>' => { fix => 'infix', prec => 15, str => ' || ' }, | ||
| 117 | 'infix:<//>' => { fix => 'infix', prec => 15, str => ' // ' }, | ||
| 118 | |||
| 119 | 'ternary:<? :>' => { fix => 'ternary', prec => 16 }, | ||
| 120 | |||
| 121 | 'infix:<=>' => { fix => 'infix', prec => 17, str => ' = ' }, | ||
| 122 | 'infix:<**=>' => { fix => 'infix', prec => 17, str => ' **= ' }, | ||
| 123 | 'infix:<+=>' => { fix => 'infix', prec => 17, str => ' += ' }, | ||
| 124 | 'infix:<-=>' => { fix => 'infix', prec => 17, str => ' -= ' }, | ||
| 125 | 'infix:<*=>' => { fix => 'infix', prec => 17, str => ' *= ' }, | ||
| 126 | 'infix:</=>' => { fix => 'infix', prec => 17, str => ' /= ' }, | ||
| 127 | 'infix:<x=>' => { fix => 'infix', prec => 17, str => ' x= ' }, | ||
| 128 | 'infix:<|=>' => { fix => 'infix', prec => 17, str => ' |= ' }, | ||
| 129 | 'infix:<&=>' => { fix => 'infix', prec => 17, str => ' &= ' }, | ||
| 130 | 'infix:<.=>' => { fix => 'infix', prec => 17, str => ' .= ' }, | ||
| 131 | 'infix:<<<=>' => { fix => 'infix', prec => 17, str => ' <<= ' }, | ||
| 132 | 'infix:<>>=>' => { fix => 'infix', prec => 17, str => ' >>= ' }, | ||
| 133 | 'infix:<%=>' => { fix => 'infix', prec => 17, str => ' %= ' }, | ||
| 134 | 'infix:<||=>' => { fix => 'infix', prec => 17, str => ' ||= ' }, | ||
| 135 | 'infix:<&&=>' => { fix => 'infix', prec => 17, str => ' &&= ' }, | ||
| 136 | 'infix:<^=>' => { fix => 'infix', prec => 17, str => ' ^= ' }, | ||
| 137 | 'infix:<//=>' => { fix => 'infix', prec => 17, str => ' //= ' }, | ||
| 138 | |||
| 139 | 'infix:<=>>' => { fix => 'infix', prec => 18, str => ' => ' }, | ||
| 140 | |||
| 141 | 'list:<,>' => { fix => 'list', prec => 19, str => ', ' }, | ||
| 142 | |||
| 143 | 'prefix:<not>' => { fix => 'prefix', prec => 20, str => 'not ' }, | ||
| 144 | |||
| 145 | 'infix:<and>' => { fix => 'infix', prec => 21, str => ' and ' }, | ||
| 146 | |||
| 147 | 'infix:<or>' => { fix => 'infix', prec => 22, str => ' or ' }, | ||
| 148 | 'infix:<xor>' => { fix => 'infix', prec => 22, str => ' xor ' }, | ||
| 149 | ); | ||
| 150 | |||
| 151 | # more unary operators | ||
| 152 | $op{ "prefix:<$_>" } = { fix => 'prefix', prec => 8, str => "$_ " } | ||
| 153 | for qw( | ||
| 154 | -r -w -x -o | ||
| 155 | -R -W -X -O | ||
| 156 | -e -z -s | ||
| 157 | -f -d -l -p -S -b -c -t | ||
| 158 | -u -g -k | ||
| 159 | -T -B | ||
| 160 | -M -A -C | ||
| 161 | ); | ||
| 162 | $op{ "prefix:<$_>" } = { fix => 'parsed', prec => 15, str => "$_" } | ||
| 163 | for qw( do sub my our state local | ||
| 164 | eval map grep sort print use ); | ||
| 165 | |||
| 166 | my %tab; | ||
| 167 | sub tab { | ||
| 168 | my $level = $_[0]; | ||
| 169 | $tab{$level} //= " " x $level; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub render { | ||
| 173 | my ( $data, $level, $out ) = @_; | ||
| 174 | if ( ref($data) ) { | ||
| 175 | $dispatch{ $data->[0] }->( $data, $level, $out ); | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | push @$out, $data; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | sub op_precedence { | ||
| 183 | my ($data) = @_; | ||
| 184 | return 0 if !ref($data); | ||
| 185 | return 0 if $data->[0] ne 'op'; | ||
| 186 | return $op{ $data->[1] }{prec} || 0; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub statement_need_semicolon { | ||
| 190 | my ($data) = @_; | ||
| 191 | return 1 if !ref($data); | ||
| 192 | return 0 | ||
| 193 | if $data->[0] eq 'block' || $data->[0] eq 'comment' || $data->[0] eq 'label'; | ||
| 194 | if ( $data->[0] eq 'stmt' ) { | ||
| 195 | # stmt => [ keyword => 'if' ], | ||
| 196 | if ( ref( $data->[1] ) ) { | ||
| 197 | my $dd = $data->[1]; # [ keyword => 'if' ], | ||
| 198 | if ( $dd->[0] eq 'keyword' ) { | ||
| 199 | return 0 | ||
| 200 | if ref( $data->[-1] ) && $data->[-1][0] eq 'block'; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | return 1; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub op_render { | ||
| 208 | my ( $data, $level, $out, $current_op ) = @_; | ||
| 209 | if ( ref($data) ) { | ||
| 210 | my $this_prec = op_precedence($data); | ||
| 211 | my $use_paren = $this_prec && $current_op->{prec} | ||
| 212 | && ($current_op->{prec} < $this_prec | ||
| 213 | || $current_op->{prec} == 6); | ||
| 214 | push @$out, '(' if $use_paren; | ||
| 215 | render( $data, $level, $out ); | ||
| 216 | push @$out, ')' if $use_paren; | ||
| 217 | } | ||
| 218 | else { | ||
| 219 | push @$out, $data; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | sub op { | ||
| 224 | my ( $data, $level, $out ) = @_; | ||
| 225 | my $op = $data->[1]; | ||
| 226 | my $spec = $op{$op} || die "unknown op: $op"; | ||
| 227 | if ( $spec->{fix} eq 'infix' ) { | ||
| 228 | op_render( $data->[2], $level, $out, $spec ); | ||
| 229 | push @$out, $spec->{str}; | ||
| 230 | op_render( $data->[3], $level, $out, $spec ); | ||
| 231 | } | ||
| 232 | elsif ( $spec->{fix} eq 'prefix' ) { | ||
| 233 | push @$out, $spec->{str}; | ||
| 234 | if ($op eq 'prefix:<not>' && !$data->[2]) { | ||
| 235 | # not() needs parenthesis | ||
| 236 | push @$out, '()'; | ||
| 237 | } | ||
| 238 | else { | ||
| 239 | op_render( $data->[2], $level, $out, $spec ); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | elsif ( $spec->{fix} eq 'postfix' ) { | ||
| 243 | op_render( $data->[2], $level, $out, $spec ); | ||
| 244 | push @$out, $spec->{str}; | ||
| 245 | } | ||
| 246 | elsif ( $spec->{fix} eq 'ternary' ) { | ||
| 247 | op_render( $data->[2], $level, $out, $spec ); | ||
| 248 | push @$out, ' ? '; | ||
| 249 | op_render( $data->[3], $level, $out, $spec ); | ||
| 250 | push @$out, ' : '; | ||
| 251 | op_render( $data->[4], $level, $out, $spec ); | ||
| 252 | } | ||
| 253 | elsif ( $spec->{fix} eq 'deref' ) { | ||
| 254 | # push @$out, $spec->{str}, '{'; | ||
| 255 | push @$out, $spec->{str}; | ||
| 256 | op_render( $data->[2], $level, $out, $spec ); | ||
| 257 | # push @$out, '}'; | ||
| 258 | } | ||
| 259 | elsif ( $spec->{fix} eq 'circumfix' ) { | ||
| 260 | push @$out, $spec->{str}; | ||
| 261 | for my $line ( 2 .. $#$data ) { | ||
| 262 | op_render( $data->[$line], $level, $out, $spec ); | ||
| 263 | push @$out, ', ' if $line != $#$data; | ||
| 264 | } | ||
| 265 | push @$out, $pair{$spec->{str}}; | ||
| 266 | } | ||
| 267 | elsif ( $spec->{fix} eq 'list' ) { | ||
| 268 | for my $line ( 2 .. $#$data ) { | ||
| 269 | op_render( $data->[$line], $level, $out, $spec ); | ||
| 270 | push @$out, $spec->{str} if $line != $#$data; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | elsif ( $spec->{fix} eq 'parsed' ) { | ||
| 274 | push @$out, $spec->{str}; | ||
| 275 | for my $line ( 2 .. $#$data ) { | ||
| 276 | my $d = $data->[$line]; | ||
| 277 | push @$out, ' '; | ||
| 278 | render( $d, $level, $out ); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | else { | ||
| 282 | die "unknown fixity: $spec->{fix}"; | ||
| 283 | } | ||
| 284 | return; | ||
| 285 | } | ||
| 286 | |||
| 287 | sub call { | ||
| 288 | my ( $data, $level, $out ) = @_; | ||
| 289 | my @dd = @$data; | ||
| 290 | shift @dd; | ||
| 291 | my $open = '('; | ||
| 292 | render( shift(@dd), $level, $out ); | ||
| 293 | push @$out, '->'; | ||
| 294 | my $d = $dd[0]; | ||
| 295 | render( $d, $level, $out ); | ||
| 296 | $dd[0] = 'list:<,>'; | ||
| 297 | push @$out, $open; | ||
| 298 | op( [ op => @dd ], $level, $out ); | ||
| 299 | push @$out, $pair{$open}; | ||
| 300 | } | ||
| 301 | |||
| 302 | sub apply { | ||
| 303 | my ( $data, $level, $out ) = @_; | ||
| 304 | my @dd = @$data; | ||
| 305 | shift @dd; | ||
| 306 | my $open = shift @dd; | ||
| 307 | my $d = $dd[0]; | ||
| 308 | if ($d eq 'return') { | ||
| 309 | # 'return' should not have 'function' parentheses, because | ||
| 310 | # return 4,5 # '5' in scalar context | ||
| 311 | # return (4,5) # '2' in scalar context | ||
| 312 | $open = ' '; | ||
| 313 | } | ||
| 314 | render( $d, $level, $out ); | ||
| 315 | $dd[0] = 'list:<,>'; | ||
| 316 | push @$out, $open; | ||
| 317 | op( [ op => @dd ], $level, $out ); | ||
| 318 | push @$out, $pair{$open}; | ||
| 319 | } | ||
| 320 | |||
| 321 | sub paren { | ||
| 322 | my ( $data, $level, $out ) = @_; | ||
| 323 | my @dd = @$data; | ||
| 324 | shift @dd; | ||
| 325 | my $open = $dd[0]; | ||
| 326 | $dd[0] = 'list:<,>'; | ||
| 327 | push @$out, $open; | ||
| 328 | op( [ op => @dd ], $level, $out ); | ||
| 329 | push @$out, $pair{$open}; | ||
| 330 | } | ||
| 331 | |||
| 332 | sub paren_semicolon { | ||
| 333 | my ( $data, $level, $out ) = @_; | ||
| 334 | push @$out, $data->[1]; | ||
| 335 | for my $line ( 2 .. $#$data ) { | ||
| 336 | render( $data->[$line], $level, $out ) if @{ $data->[$line] }; | ||
| 337 | if ( $line != $#$data ) { | ||
| 338 | if ( @{ $data->[ $line + 1 ] } ) { | ||
| 339 | push @$out, '; '; | ||
| 340 | } | ||
| 341 | else { | ||
| 342 | push @$out, ';'; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | } | ||
| 346 | push @$out, $pair{ $data->[1] }; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub label { | ||
| 350 | my ( $data, $level, $out ) = @_; | ||
| 351 | push @$out, $data->[1], ":"; | ||
| 352 | return; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub keyword { | ||
| 356 | my ( $data, $level, $out ) = @_; | ||
| 357 | push @$out, $data->[1]; | ||
| 358 | return; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub bareword { | ||
| 362 | my ( $data, $level, $out ) = @_; | ||
| 363 | push @$out, $data->[1]; | ||
| 364 | return; | ||
| 365 | } | ||
| 366 | |||
| 367 | sub number { | ||
| 368 | my ( $data, $level, $out ) = @_; | ||
| 369 | push @$out, $data->[1]; | ||
| 370 | return; | ||
| 371 | } | ||
| 372 | |||
| 373 | sub comment { | ||
| 374 | my ( $data, $level, $out ) = @_; | ||
| 375 | push @$out, $data->[1]; | ||
| 376 | return; | ||
| 377 | } | ||
| 378 | |||
| 379 | sub statement { | ||
| 380 | my ( $data, $level, $out ) = @_; | ||
| 381 | for my $line ( 1 .. $#$data ) { | ||
| 382 | my $d = $data->[$line]; | ||
| 383 | render( $d, $level, $out ); | ||
| 384 | push @$out, ' ' if $line != $#$data; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | sub statement_modifier { | ||
| 389 | my ( $data, $level, $out ) = @_; | ||
| 390 | render( $data->[1], $level, $out ); | ||
| 391 | push @$out, "\n", tab($level + 1); | ||
| 392 | render( $data->[2], $level, $out ); | ||
| 393 | } | ||
| 394 | |||
| 395 | sub block { | ||
| 396 | my ( $data, $level, $out ) = @_; | ||
| 397 | if ( @$data == 1 ) { | ||
| 398 | push @$out, "{}"; | ||
| 399 | return; | ||
| 400 | } | ||
| 401 | if ( @$data == 2 ) { | ||
| 402 | push @$out, '{;', "\n"; # disambiguate { $x, $y } block vs. hash | ||
| 403 | } | ||
| 404 | else { | ||
| 405 | push @$out, '{', "\n"; | ||
| 406 | } | ||
| 407 | $level++; | ||
| 408 | for my $line ( 1 .. $#$data ) { | ||
| 409 | my $d = $data->[$line]; | ||
| 410 | push @$out, tab($level); | ||
| 411 | render( $d, $level, $out ); | ||
| 412 | push @$out, ';' if $line != $#$data && statement_need_semicolon($d); | ||
| 413 | push @$out, "\n"; | ||
| 414 | } | ||
| 415 | $level--; | ||
| 416 | push @$out, tab($level), '}'; | ||
| 417 | } | ||
| 418 | |||
| 419 | sub pretty_print { | ||
| 420 | my ( $data, $level, $out ) = @_; | ||
| 421 | for my $line ( 0 .. $#$data ) { | ||
| 422 | my $d = $data->[$line]; | ||
| 423 | push @$out, tab($level); | ||
| 424 | render( $d, $level, $out ); | ||
| 425 | push @$out, ';' if $line != $#$data && statement_need_semicolon($d); | ||
| 426 | push @$out, "\n"; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | |||
| 430 | 1; | ||
| 431 | |||
| 432 | __END__ |