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