| 1 | use v5; | ||
| 2 | package Perlito5::Macro; | ||
| 3 | use strict; | ||
| 4 | use Perlito5::Clone; | ||
| 5 | |||
| 6 | # provide "goto LABEL" within a statement list | ||
| 7 | # | ||
| 8 | # { | ||
| 9 | # 123; | ||
| 10 | # my $var; | ||
| 11 | # goto LABEL; | ||
| 12 | # 123; | ||
| 13 | # LABEL: | ||
| 14 | # 456; | ||
| 15 | # } | ||
| 16 | # | ||
| 17 | # a "forward goto" becomes: | ||
| 18 | |||
| 19 | # LABEL: | ||
| 20 | # { | ||
| 21 | # 123; | ||
| 22 | # my $var; | ||
| 23 | # do { 456; last LABEL; }; | ||
| 24 | # 123; | ||
| 25 | # 456; | ||
| 26 | # } | ||
| 27 | |||
| 28 | # See: | ||
| 29 | # https://jamey.thesharps.us/2016/05/09/how-to-eliminate-goto-statements/ | ||
| 30 | |||
| 31 | sub rewrite_goto { | ||
| 32 | my ($stmts) = @_; | ||
| 33 | return $stmts if !@Perlito5::GOTO; # no "goto" | ||
| 34 | |||
| 35 | # ignore "goto &sub" - this is processed elsewhere | ||
| 36 | # ignore "goto $str" - this is not yet implemented - TODO | ||
| 37 | # accept "goto LABEL" (bareword) | ||
| 38 | @Perlito5::GOTO = grep { | ||
| 39 | $_->{arguments}[0]{bareword} | ||
| 40 | } @Perlito5::GOTO; | ||
| 41 | return $stmts if !@Perlito5::GOTO; | ||
| 42 | |||
| 43 | my $change = 0; | ||
| 44 | my $outer_label = Perlito5::get_label(); | ||
| 45 | my @unprocessed; | ||
| 46 | GOTO: | ||
| 47 | for my $goto (@Perlito5::GOTO) { | ||
| 48 | my $label = $goto->{arguments}[0]{code}; | ||
| 49 | next GOTO unless $label; | ||
| 50 | |||
| 51 | # lookup for labels | ||
| 52 | my @label; | ||
| 53 | for my $id (reverse (0 .. $#$stmts)) { | ||
| 54 | my $ast = $stmts->[$id]; | ||
| 55 | if ($ast->{label} && $ast->{label} eq $label) { | ||
| 56 | my @stmt_list = @{$stmts}[ $id .. $#$stmts ]; | ||
| 57 | # warn "Block uses goto: ", Perlito5::Dumper::Dumper($goto); | ||
| 58 | # warn "Block goes here: ", Perlito5::Dumper::Dumper(\@stmt_list); | ||
| 59 | |||
| 60 | if ( $id == 0 ) { | ||
| 61 | # if $id == 0, replace "goto" with "redo" | ||
| 62 | $goto->{code} = "redo"; | ||
| 63 | $goto->{arguments} =[ | ||
| 64 | Perlito5::AST::Apply->new( | ||
| 65 | code => $outer_label, | ||
| 66 | bareword => 1, | ||
| 67 | ) | ||
| 68 | ]; | ||
| 69 | $change = 1; | ||
| 70 | next GOTO; | ||
| 71 | } | ||
| 72 | |||
| 73 | # TODO - this doesn't cover all cases | ||
| 74 | |||
| 75 | # do { @stmt; last LABEL }; | ||
| 76 | $goto->{code} = "do"; | ||
| 77 | $goto->{arguments} = [ | ||
| 78 | Perlito5::AST::Block->new( | ||
| 79 | stmts => [ | ||
| 80 | @{ Perlito5::Clone::clone(\@stmt_list) }, | ||
| 81 | Perlito5::AST::Apply->new( | ||
| 82 | code => "last", | ||
| 83 | arguments => [ | ||
| 84 | Perlito5::AST::Apply->new( | ||
| 85 | code => $outer_label, | ||
| 86 | bareword => 1, | ||
| 87 | ), | ||
| 88 | ], | ||
| 89 | ), | ||
| 90 | ], | ||
| 91 | ), | ||
| 92 | ]; | ||
| 93 | $change = 1; | ||
| 94 | next GOTO; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | push @unprocessed, $goto; | ||
| 98 | } | ||
| 99 | @Perlito::GOTO = @unprocessed; | ||
| 100 | |||
| 101 | if ($change) { | ||
| 102 | # wrap the statements in a block with the new label | ||
| 103 | return [ | ||
| 104 | Perlito5::AST::Block->new( | ||
| 105 | label => $outer_label, | ||
| 106 | stmts => [ @$stmts ], | ||
| 107 | ), | ||
| 108 | ]; | ||
| 109 | } | ||
| 110 | |||
| 111 | return $stmts; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | { | ||
| 116 | package Perlito5::AST::Apply; | ||
| 117 | use strict; | ||
| 118 | |||
| 119 | my %op = ( | ||
| 120 | 'infix:<+=>' => 'infix:<+>', | ||
| 121 | 'infix:<-=>' => 'infix:<->', | ||
| 122 | 'infix:<*=>' => 'infix:<*>', | ||
| 123 | 'infix:</=>' => 'infix:</>', | ||
| 124 | 'infix:<%=>' => 'infix:<%>', | ||
| 125 | 'infix:<||=>' => 'infix:<||>', | ||
| 126 | 'infix:<&&=>' => 'infix:<&&>', | ||
| 127 | 'infix:<|=>' => 'infix:<|>', | ||
| 128 | 'infix:<&=>' => 'infix:<&>', | ||
| 129 | 'infix:<^=>' => 'infix:<^>', | ||
| 130 | 'infix:<//=>' => 'infix:<//>', | ||
| 131 | 'infix:<**=>' => 'infix:<**>', | ||
| 132 | 'infix:<.=>' => 'list:<.>', | ||
| 133 | 'infix:<x=>' => 'infix:<x>', | ||
| 134 | 'infix:<<<=>' => 'infix:<<<>', | ||
| 135 | 'infix:<>>=>' => 'infix:<>>>', | ||
| 136 | 'infix:<&.=>' => 'infix:<&.>', | ||
| 137 | 'infix:<|.=>' => 'infix:<|.>', | ||
| 138 | 'infix:<^.=>' => 'infix:<^.>', | ||
| 139 | ); | ||
| 140 | |||
| 141 | sub op_assign { | ||
| 142 | my $self = $_[0]; | ||
| 143 | |||
| 144 | my $code = $self->{code}; | ||
| 145 | return 0 if ref($code); | ||
| 146 | |||
| 147 | if (exists( $op{$code} )) { | ||
| 148 | return Perlito5::AST::Apply->new( | ||
| 149 | code => 'infix:<=>', | ||
| 150 | arguments => [ | ||
| 151 | $self->{arguments}->[0], | ||
| 152 | Perlito5::AST::Apply->new( | ||
| 153 | code => $op{$code}, | ||
| 154 | arguments => $self->{arguments}, | ||
| 155 | ( $self->{_integer} ? ( _integer => 1 ) : () ), | ||
| 156 | ), | ||
| 157 | ] | ||
| 158 | ); | ||
| 159 | } | ||
| 160 | |||
| 161 | return 0; | ||
| 162 | } | ||
| 163 | |||
| 164 | my %op_auto = ( | ||
| 165 | 'prefix:<++>' => 1, | ||
| 166 | 'prefix:<-->' => 1, | ||
| 167 | 'postfix:<++>' => 1, | ||
| 168 | 'postfix:<-->' => 1, | ||
| 169 | ); | ||
| 170 | |||
| 171 | sub op_auto { | ||
| 172 | my $self = $_[0]; | ||
| 173 | |||
| 174 | my $code = $self->{code}; | ||
| 175 | return 0 if ref($code); | ||
| 176 | |||
| 177 | if (exists( $op_auto{$code} )) { | ||
| 178 | # ++( $v = 2 ) | ||
| 179 | # do { $v = 2; ++$v } | ||
| 180 | |||
| 181 | my $paren = $self->{arguments}[0]; | ||
| 182 | if ($paren->{code} eq 'circumfix:<( )>') { | ||
| 183 | |||
| 184 | my $arg = $paren->{arguments}[-1]; | ||
| 185 | if ($arg->{code} eq 'infix:<=>') { | ||
| 186 | |||
| 187 | my $var = $arg->{arguments}[0]; | ||
| 188 | |||
| 189 | return Perlito5::AST::Apply->new( | ||
| 190 | code => 'do', | ||
| 191 | arguments => [ Perlito5::AST::Block->new( | ||
| 192 | stmts => [ | ||
| 193 | $paren, # assignment | ||
| 194 | Perlito5::AST::Apply->new( | ||
| 195 | code => $code, # autoincrement | ||
| 196 | arguments => [ $var ], | ||
| 197 | ), | ||
| 198 | ], | ||
| 199 | ) ], | ||
| 200 | ); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | return 0; | ||
| 206 | } | ||
| 207 | |||
| 208 | } # /package | ||
| 209 | |||
| 210 | |||
| 211 | { | ||
| 212 | package Perlito5::AST::Sub; | ||
| 213 | use strict; | ||
| 214 | use feature 'say'; | ||
| 215 | use Perlito5::TreeGrammar; | ||
| 216 | # Transformation for implementing state variables (perldoc -f state) by | ||
| 217 | # enclosing the sub in a do block, which in turn contains lexical variables | ||
| 218 | # that corresponding to the state variables in use in the sub. Returns a `do` | ||
| 219 | # expr node enclosing the modified sub, or 0 if no statevars are found in $self. | ||
| 220 | sub maybe_rewrite_statevars { | ||
| 221 | my ($self) = @_; | ||
| 222 | my $block = $self->{block} || return 0; | ||
| 223 | my @init_flags; # Holds the *names* of intialization flags for state vars. | ||
| 224 | my @vars; | ||
| 225 | my @base_rules = ([Lookup => 'block'], [Lookup => 'stmts']); # $self->{block}{stmts} | ||
| 226 | for my $idx (0..$#{ $block->{stmts} }) { | ||
| 227 | my $stmt = $block->{stmts}[$idx]; | ||
| 228 | my ($node, @rules) = find_state_expr($stmt, @base_rules, [Index => $idx]); | ||
| 229 | if (defined $node) { | ||
| 230 | my ($transformed, $var, $flagvar) = rewrite_state_expr($node); | ||
| 231 | # Everything in @vars will be added to a big `my` decl block. See below. | ||
| 232 | push @vars, $var, $flagvar; | ||
| 233 | # Now we will replace $node in the tree with $transformed. | ||
| 234 | # To do that, we note that the last rule in @rules *must* | ||
| 235 | # be an Index or a Lookup. We pop this last rule into | ||
| 236 | # $last_rule, and then push an Action rule into | ||
| 237 | # @rules. This Action rule will be fired when the rules | ||
| 238 | # before it in @rules have all executed, resulting in the | ||
| 239 | # parent of $node being passed to the Action sub. At that | ||
| 240 | # point, we take this parent node (say, $parent), and | ||
| 241 | # depending on whether $last_rule->[0] is a Lookup or an Index, | ||
| 242 | # we do $parent->{$last_rule->[1]} = $transformed or | ||
| 243 | # $parent->[$last_rule->[1]] = $transformed. | ||
| 244 | my $last_rule = pop @rules; | ||
| 245 | push @rules, [Action => sub { | ||
| 246 | my ($parent) = @_; # This node will be the parent of $node. | ||
| 247 | if ($last_rule->[0] eq 'Lookup') { | ||
| 248 | $parent->{$last_rule->[1]} = $transformed; | ||
| 249 | } else { | ||
| 250 | $parent->[$last_rule->[1]] = $transformed; | ||
| 251 | } | ||
| 252 | }]; | ||
| 253 | # This is the nested rule built from @rules, that correctly | ||
| 254 | # walks the AST till the parent of $node. | ||
| 255 | my $rule = nest(\@rules); | ||
| 256 | Perlito5::TreeGrammar::render($rule, $self); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | # If there are any state var expressions that we have rewritten, | ||
| 260 | # we shall return a `do` block containing a `my` declaraction of | ||
| 261 | # the transformed variables followed by the mutated Sub node for | ||
| 262 | # $self. | ||
| 263 | if (scalar @vars) { | ||
| 264 | return Perlito5::AST::Apply->new( | ||
| 265 | code => 'do', | ||
| 266 | namespace => $block->{namespace}, | ||
| 267 | arguments => [Perlito5::AST::Block->new( | ||
| 268 | sig => undef, | ||
| 269 | stmts => [ | ||
| 270 | myvar_declaration_stmt(@vars), | ||
| 271 | $self, | ||
| 272 | ] | ||
| 273 | )], | ||
| 274 | ); | ||
| 275 | } | ||
| 276 | return 0; | ||
| 277 | } | ||
| 278 | |||
| 279 | # myvar_declaration_stmt($var_node1, $var_node2, ...) | ||
| 280 | # | ||
| 281 | # Returns a `my` declaration node from a bunch of Var nodes, equivalent to the | ||
| 282 | # statement | ||
| 283 | # | ||
| 284 | # my ($v1, $v2, ...); | ||
| 285 | # | ||
| 286 | # where $v1, $v2 etc are the names of the passed in Var nodes. | ||
| 287 | # | ||
| 288 | sub myvar_declaration_stmt { | ||
| 289 | die 'Can only handle `my` variables' | ||
| 290 | if (grep { ($_->{decl} // 'my') ne 'my' } @_); | ||
| 291 | return map { | ||
| 292 | Perlito5::AST::Decl->new( | ||
| 293 | decl => 'my', | ||
| 294 | var => $_, | ||
| 295 | ) | ||
| 296 | } @_; | ||
| 297 | } | ||
| 298 | |||
| 299 | # rewrite_state_expr($refnode) | ||
| 300 | # | ||
| 301 | # Given an AST node that is a state variable declaration/assignment, | ||
| 302 | # returns a list of three items: | ||
| 303 | # | ||
| 304 | # - $transformed The transformed node. Specifically, a state declaration or assignment is | ||
| 305 | # transformed to a `do` block that closes over a `my` variable with the same | ||
| 306 | # name as the original state variable. An additional flag variable is created | ||
| 307 | # for initializing this closed over `my` variable exactly once. | ||
| 308 | # | ||
| 309 | # - $var An *AST node* representing the aforementioned `my` variable. It is the job | ||
| 310 | # of the caller to place this in a scope that can be closed over by | ||
| 311 | # $transformed. | ||
| 312 | # | ||
| 313 | # - $flagvar The flag variable node, which must also be placed in the enclosing scope | ||
| 314 | # of $transformed. | ||
| 315 | # | ||
| 316 | sub rewrite_state_expr { | ||
| 317 | my ($target) = @_; | ||
| 318 | my ($decl, $rhs); | ||
| 319 | if (ref $target eq 'Perlito5::AST::Apply') { | ||
| 320 | $decl = $target->{arguments}[0]; | ||
| 321 | $rhs = $target->{arguments}[1]; | ||
| 322 | } elsif (ref $target eq 'Perlito5::AST::Decl') { | ||
| 323 | $decl = $target; | ||
| 324 | } else { | ||
| 325 | die "Invalid node type for state variable transformation: " . (ref $target); | ||
| 326 | } | ||
| 327 | my $state_var = $decl->{var}; | ||
| 328 | # This is the lexical variable that we'll inject into the surrounding | ||
| 329 | # do block | ||
| 330 | my $var = Perlito5::AST::Var->new( | ||
| 331 | namespace => $state_var->{namespace}, | ||
| 332 | sigil => $state_var->{sigil}, | ||
| 333 | name => $state_var->{name}, | ||
| 334 | _id => $state_var->{_id}, | ||
| 335 | _decl => 'my', | ||
| 336 | ); | ||
| 337 | # A label to ensure hygienic names for the initialization flag. | ||
| 338 | my $label = Perlito5::get_label(); | ||
| 339 | # The flag variable which will go in to the surrounding `do` block. | ||
| 340 | my $flagvar = Perlito5::AST::Var->new( | ||
| 341 | name => $var->{name} . "_inited_" . $label, # e.g., x_initied_tmp404 | ||
| 342 | sigil => '$', | ||
| 343 | namespace => '', | ||
| 344 | _id => $Perlito5::ID++, | ||
| 345 | _decl => 'my', | ||
| 346 | ); | ||
| 347 | # The `do` block that actually checks for the init flag and initializes | ||
| 348 | # the "state" variable (and of course sets the flag). | ||
| 349 | my $init_block = Perlito5::AST::Apply->new( | ||
| 350 | code => 'do', | ||
| 351 | namespace => $decl->{namespace}, | ||
| 352 | arguments => [Perlito5::AST::Block->new( | ||
| 353 | sig => undef, | ||
| 354 | stmts => [ | ||
| 355 | # First statement in the do block sets the init flag to 1. | ||
| 356 | Perlito5::AST::Apply->new( | ||
| 357 | code => 'infix:<=>', | ||
| 358 | namespace => $decl->{namespace}, | ||
| 359 | arguments => [ | ||
| 360 | $flagvar, | ||
| 361 | Perlito5::AST::Int->new(int => 1), | ||
| 362 | ], | ||
| 363 | ), | ||
| 364 | # Next, we set the variable to whatever the original state | ||
| 365 | # variable was set to. This being the last statement of the | ||
| 366 | # do block, it will also become the value of the block. | ||
| 367 | # If $target points to an Apply node, then we also construct | ||
| 368 | # an apply node, else we leave it as a my decl. | ||
| 369 | ((ref $target eq 'Perlito5::AST::Apply') ? | ||
| 370 | Perlito5::AST::Apply->new( | ||
| 371 | code => 'infix:<=>', | ||
| 372 | namespace => $decl->{namespace}, | ||
| 373 | arguments => [$var, $rhs], | ||
| 374 | ) : $var), | ||
| 375 | ], | ||
| 376 | )], | ||
| 377 | ); | ||
| 378 | # Replace the Apply node with a ternary op that checks if the state | ||
| 379 | # variable has been initialized and initializes it using the above `do` | ||
| 380 | # block if not. | ||
| 381 | my $transformed = Perlito5::AST::Apply->new( | ||
| 382 | code => 'ternary:<? :>', | ||
| 383 | namespace => $decl->{namespace}, | ||
| 384 | arguments => [$flagvar, $var, $init_block], | ||
| 385 | ); | ||
| 386 | # Return our new code and all the variables that need to be in scope for | ||
| 387 | # the block to work. | ||
| 388 | return ($transformed, $var, $flagvar); | ||
| 389 | } | ||
| 390 | |||
| 391 | # nest(ARRAYREF) | ||
| 392 | # | ||
| 393 | # Given an arrayref of the form | ||
| 394 | # | ||
| 395 | # [ [a1, a2, a3, ...], [b1, b2, b3, ...], [c1, c2, c3, ...] ] | ||
| 396 | # | ||
| 397 | # produces an arrayref of nested arrayrefs, with the first element of each | ||
| 398 | # flattened to one level: | ||
| 399 | # | ||
| 400 | # [ a1, a2, a3, ..., [b1, b2, b3, ..., [c1, c2, c3, ... [...]]]] | ||
| 401 | # | ||
| 402 | # As a concrete example, | ||
| 403 | # | ||
| 404 | # [[ Lookup => 'blah' ], [ Index => 0 ], [ Lookup => 'bar' ], [ Action => $sub ]] | ||
| 405 | # | ||
| 406 | # becomes | ||
| 407 | # | ||
| 408 | # [ Lookup => 'blah', [ Index => 0, [ Lookup => 'bar', [ Action => $sub ]]]] | ||
| 409 | # | ||
| 410 | # Note the flattening is only upto one level, e.g., | ||
| 411 | # | ||
| 412 | # [[a, [b, c]], [d, e]] | ||
| 413 | # | ||
| 414 | # becomes | ||
| 415 | # | ||
| 416 | # [a, [b, c], [d, e]] | ||
| 417 | # | ||
| 418 | # and not | ||
| 419 | # | ||
| 420 | # [a, b, c, [d, e]] | ||
| 421 | # | ||
| 422 | sub nest { | ||
| 423 | my ($xs) = @_; | ||
| 424 | if (scalar @$xs == 0) { | ||
| 425 | return; | ||
| 426 | } | ||
| 427 | return [flatten($xs->[0]), nest([@{$xs}[1..$#$xs]])]; | ||
| 428 | } | ||
| 429 | |||
| 430 | # flatten(ARRAYREF|SCALAR) | ||
| 431 | # | ||
| 432 | # If $x is an ARRAYREF, return @$x, otherwise returns $x. | ||
| 433 | sub flatten { | ||
| 434 | my ($arg) = @_; | ||
| 435 | if (ref $arg eq 'ARRAY') { | ||
| 436 | return (@{$arg}); | ||
| 437 | } | ||
| 438 | return $arg; | ||
| 439 | } | ||
| 440 | |||
| 441 | # find_state_expr($node, @rules) | ||
| 442 | # | ||
| 443 | # Find a state variable declaration in the AST subtree rooted at | ||
| 444 | # $node. @rules is a *list* of Perlito5::TreeGrammar rules (and not a | ||
| 445 | # single monolithic rule tree) required to reach $node. | ||
| 446 | # | ||
| 447 | # If a state variable declaration/initialization expression is found, | ||
| 448 | # this function returns the state decl node and the rule list required | ||
| 449 | # get to it. | ||
| 450 | # | ||
| 451 | # If @rules is empty, the returned list of rules will be the rules | ||
| 452 | # required to get to the found state variable decl/init node *from* | ||
| 453 | # $node. | ||
| 454 | # | ||
| 455 | # If no state variable declaration/initialization is found, undef is | ||
| 456 | # returned. | ||
| 457 | sub find_state_expr { | ||
| 458 | my ($node, @rules) = @_; | ||
| 459 | if (is_node_state_decl($node) || is_node_state_assignment($node)) { | ||
| 460 | return ($node, @rules); | ||
| 461 | } | ||
| 462 | # Recursively scan each node in $node->{stmts} or $node->{arguments}, | ||
| 463 | # if we can. | ||
| 464 | for my $branch (qw(stmts arguments)) { | ||
| 465 | if (exists $node->{$branch} && ref $node->{$branch} eq 'ARRAY'){ | ||
| 466 | for (0..$#{$node->{$branch}}) { | ||
| 467 | my ($retnode, @retrules) = find_state_expr( | ||
| 468 | $node->{$branch}[$_], | ||
| 469 | # The rules are the rules to reach $node | ||
| 470 | # plus the rules to reach $node->{$branch}{$_} | ||
| 471 | # from $node. | ||
| 472 | @rules, [Lookup => $branch], [Index => $_] | ||
| 473 | ); | ||
| 474 | if ($retnode) { | ||
| 475 | return ($retnode, @retrules); | ||
| 476 | } | ||
| 477 | } | ||
| 478 | } | ||
| 479 | } | ||
| 480 | return undef; | ||
| 481 | } | ||
| 482 | |||
| 483 | # Check if $node represents code of the form `state $var = <something>` | ||
| 484 | sub is_node_state_assignment { | ||
| 485 | my ($node) = @_; | ||
| 486 | (ref $node eq 'Perlito5::AST::Apply' && | ||
| 487 | $node->{code} eq 'infix:<=>' && | ||
| 488 | is_node_state_decl($node->{arguments}[0])); | ||
| 489 | } | ||
| 490 | # Is the given node a state variable declaration. | ||
| 491 | sub is_node_state_decl { | ||
| 492 | my ($node) = @_; | ||
| 493 | (ref $node eq 'Perlito5::AST::Decl' && $node->{decl} eq 'state'); | ||
| 494 | } | ||
| 495 | } # package Perlito5::AST::Sub | ||
| 496 | |||
| 497 | |||
| 498 | sub while_file { | ||
| 499 | my $self = $_[0]; | ||
| 500 | return 0 | ||
| 501 | if ref($self) ne 'Perlito5::AST::While'; | ||
| 502 | my $cond = $self->{cond}; | ||
| 503 | if ((ref($cond) eq 'Perlito5::AST::Apply') && ($cond->{code} eq 'readline')) { | ||
| 504 | # while (<>) ... is rewritten as while ( defined($_ = <>) ) { ... | ||
| 505 | $self->{cond} = bless({ | ||
| 506 | 'arguments' => [ | ||
| 507 | bless({ | ||
| 508 | 'arguments' => [ | ||
| 509 | Perlito5::AST::Var::SCALAR_ARG(), | ||
| 510 | $cond, | ||
| 511 | ], | ||
| 512 | 'code' => 'infix:<=>', | ||
| 513 | 'namespace' => '', | ||
| 514 | }, 'Perlito5::AST::Apply'), | ||
| 515 | ], | ||
| 516 | 'bareword' => '', | ||
| 517 | 'code' => 'defined', | ||
| 518 | 'namespace' => '', | ||
| 519 | }, 'Perlito5::AST::Apply'); | ||
| 520 | return $self; | ||
| 521 | } | ||
| 522 | return 0; | ||
| 523 | } | ||
| 524 | |||
| 525 | sub insert_return_in_block { | ||
| 526 | my ($self) = @_; | ||
| 527 | if (@{$self->{stmts}} == 0) { | ||
| 528 | push @{$self->{stmts}}, | ||
| 529 | Perlito5::AST::Apply->new( | ||
| 530 | 'arguments' => [], | ||
| 531 | 'code' => 'return', | ||
| 532 | 'namespace' => '', | ||
| 533 | '_return_from_block' => 1, | ||
| 534 | ); | ||
| 535 | } | ||
| 536 | else { | ||
| 537 | my $last_statement = pop(@{$self->{stmts}}); | ||
| 538 | push(@{$self->{stmts}}, insert_return( $last_statement )); | ||
| 539 | } | ||
| 540 | return $self; | ||
| 541 | } | ||
| 542 | sub insert_return_in_if { | ||
| 543 | my $self = $_[0]; | ||
| 544 | $self->{body} = insert_return_in_block($self->{body} || Perlito5::AST::Block->new(stmts => [])); | ||
| 545 | $self->{otherwise} = insert_return_in_block($self->{otherwise} || Perlito5::AST::Block->new(stmts => [])); | ||
| 546 | return $self; | ||
| 547 | } | ||
| 548 | sub insert_return { | ||
| 549 | my $self = $_[0]; | ||
| 550 | if ((ref($self) eq 'Perlito5::AST::If')) { | ||
| 551 | return insert_return_in_if($self); | ||
| 552 | } | ||
| 553 | if ((ref($self) eq 'Perlito5::AST::Block')) { | ||
| 554 | return insert_return_in_block($self); | ||
| 555 | } | ||
| 556 | if ((ref($self) eq 'Perlito5::AST::For')) { | ||
| 557 | return ( | ||
| 558 | $self, | ||
| 559 | Perlito5::AST::Apply->new( | ||
| 560 | 'arguments' => [ Perlito5::AST::Buf->new( buf => "" ) ], | ||
| 561 | 'code' => 'return', | ||
| 562 | 'namespace' => '', | ||
| 563 | '_return_from_block' => 1, | ||
| 564 | ), | ||
| 565 | ); | ||
| 566 | } | ||
| 567 | if ( (ref($self) eq 'Perlito5::AST::While') ) { | ||
| 568 | if ( $self->{cond}->isa('Perlito5::AST::Int') && $self->{cond}{int} ) | ||
| 569 | { | ||
| 570 | # do not emit "return" after while(1){...} because "unreachable statement" | ||
| 571 | return $self; | ||
| 572 | } | ||
| 573 | else { | ||
| 574 | return ( | ||
| 575 | $self, | ||
| 576 | Perlito5::AST::Apply->new( | ||
| 577 | 'arguments' => [ Perlito5::AST::Int->new( int => 0 ) ], | ||
| 578 | 'code' => 'return', | ||
| 579 | 'namespace' => '', | ||
| 580 | '_return_from_block' => 1, | ||
| 581 | ), | ||
| 582 | ); | ||
| 583 | } | ||
| 584 | } | ||
| 585 | if ( (ref($self) eq 'Perlito5::AST::Sub' ) ) { | ||
| 586 | if ( ! $self->{name} ) | ||
| 587 | { | ||
| 588 | return Perlito5::AST::Apply->new( | ||
| 589 | 'arguments' => [ $self ], | ||
| 590 | 'code' => 'return', | ||
| 591 | 'namespace' => '', | ||
| 592 | '_return_from_block' => 1, | ||
| 593 | ); | ||
| 594 | } | ||
| 595 | else { | ||
| 596 | return ( | ||
| 597 | $self, | ||
| 598 | Perlito5::AST::Apply->new( | ||
| 599 | 'arguments' => [ Perlito5::AST::Int->new( int => 0 ) ], | ||
| 600 | 'code' => 'return', | ||
| 601 | 'namespace' => '', | ||
| 602 | '_return_from_block' => 1, | ||
| 603 | ), | ||
| 604 | ); | ||
| 605 | } | ||
| 606 | } | ||
| 607 | if ( (ref($self) eq 'Perlito5::AST::Int' ) | ||
| 608 | || (ref($self) eq 'Perlito5::AST::Num' ) | ||
| 609 | || (ref($self) eq 'Perlito5::AST::Buf' ) | ||
| 610 | || (ref($self) eq 'Perlito5::AST::Index' ) | ||
| 611 | || (ref($self) eq 'Perlito5::AST::Lookup' ) | ||
| 612 | || (ref($self) eq 'Perlito5::AST::Call' ) | ||
| 613 | || (ref($self) eq 'Perlito5::AST::Var' ) | ||
| 614 | || (ref($self) eq 'Perlito5::AST::Decl' ) | ||
| 615 | ) { | ||
| 616 | return Perlito5::AST::Apply->new( | ||
| 617 | 'arguments' => [ $self ], | ||
| 618 | 'code' => 'return', | ||
| 619 | 'namespace' => '', | ||
| 620 | '_return_from_block' => 1, | ||
| 621 | ); | ||
| 622 | } | ||
| 623 | if ( (ref($self) eq 'Perlito5::AST::Apply' ) ) { | ||
| 624 | if ( $self->code eq 'return' ) { | ||
| 625 | return $self; | ||
| 626 | } | ||
| 627 | return Perlito5::AST::Apply->new( | ||
| 628 | 'arguments' => [ $self ], | ||
| 629 | 'code' => 'return', | ||
| 630 | 'namespace' => '', | ||
| 631 | '_return_from_block' => 1, | ||
| 632 | ); | ||
| 633 | } | ||
| 634 | return $self; | ||
| 635 | } | ||
| 636 | |||
| 637 | |||
| 638 | # sub split_deep_if { | ||
| 639 | # my $stmt = $_[0]; | ||
| 640 | # | ||
| 641 | # if (ref($stmt) eq 'Perlito5::AST::If' && $stmt->{otherwise} && $stmt->{otherwise}->isa('Perlito5::AST::Block')) { | ||
| 642 | # # if ... else if ... | ||
| 643 | # my $stmts = $stmt->{otherwise}{stmts}; | ||
| 644 | # my $v = $stmts; | ||
| 645 | # if ($stmts && @$stmts == 1) { | ||
| 646 | # my $stmt = $stmts->[0]; | ||
| 647 | # if (ref($stmt) eq 'Perlito5::AST::If' && $stmt->{otherwise} && $stmt->{otherwise}->isa('Perlito5::AST::Block')) { | ||
| 648 | # # if ... else if ... | ||
| 649 | # ### my $stmts = $stmt->{otherwise}{stmts}; | ||
| 650 | # ### if ($stmts && @$stmts == 1) { | ||
| 651 | # ### my $stmt = $stmts->[0]; | ||
| 652 | # ### if (ref($stmt) eq 'Perlito5::AST::If' && $stmt->{otherwise} && $stmt->{otherwise}->isa('Perlito5::AST::Block')) { | ||
| 653 | # ### # if ... else if ... | ||
| 654 | # my $stmts = $stmt->{otherwise}{stmts}; | ||
| 655 | # | ||
| 656 | # if ($stmts && @$stmts == 1) { | ||
| 657 | # my $stmt = $v->[0]; | ||
| 658 | # $v->[0] = | ||
| 659 | # Perlito5::AST::Apply->new( | ||
| 660 | # 'arguments' => [ | ||
| 661 | # Perlito5::AST::Block->new( | ||
| 662 | # 'stmts' => [ $stmt ], | ||
| 663 | # ), | ||
| 664 | # ], | ||
| 665 | # 'code' => 'do', | ||
| 666 | # ); | ||
| 667 | # } | ||
| 668 | # ### } | ||
| 669 | # ### } | ||
| 670 | # } | ||
| 671 | # } | ||
| 672 | # } | ||
| 673 | # } | ||
| 674 | |||
| 675 | sub split_code_too_large { | ||
| 676 | # work around Java "Code too large" error | ||
| 677 | my @stmts = @_; | ||
| 678 | # for my $stmt (@stmts) { | ||
| 679 | # split_deep_if($stmt); # find deep nested ifs | ||
| 680 | # } | ||
| 681 | while (@stmts > 20) { | ||
| 682 | # print STDERR "Code too large, split ", scalar(@stmts), " nodes\n"; | ||
| 683 | my @do = splice(@stmts, -15, 15); | ||
| 684 | push @stmts, | ||
| 685 | Perlito5::AST::Apply->new( | ||
| 686 | 'arguments' => [ | ||
| 687 | Perlito5::AST::Block->new( | ||
| 688 | 'stmts' => \@do, | ||
| 689 | ), | ||
| 690 | ], | ||
| 691 | 'code' => 'do', | ||
| 692 | ); | ||
| 693 | } | ||
| 694 | return @stmts; | ||
| 695 | } | ||
| 696 | |||
| 697 | |||
| 698 | sub preprocess_regex { | ||
| 699 | my $regex = shift; | ||
| 700 | |||
| 701 | if ((ref($regex) eq 'Perlito5::AST::Apply') && $regex->{code} eq 'circumfix:<( )>') { | ||
| 702 | # $x =~ ( ... ) | ||
| 703 | ($regex) = @{ $regex->{arguments} }; | ||
| 704 | return preprocess_regex($regex); | ||
| 705 | } | ||
| 706 | |||
| 707 | if ( (ref($regex) eq 'Perlito5::AST::Buf') # $x =~ '\w' | ||
| 708 | || (ref($regex) eq 'Perlito5::AST::Var') # $x =~ $regex | ||
| 709 | || ((ref($regex) eq 'Perlito5::AST::Apply') && $regex->{code} eq 'list:<.>') # $x =~ ($r1 . $r2) | ||
| 710 | ) | ||
| 711 | { | ||
| 712 | $regex = Perlito5::AST::Apply->new( | ||
| 713 | code => 'p5:m', | ||
| 714 | arguments => [ $regex, Perlito5::AST::Buf->new( buf => '' ) ] | ||
| 715 | ); | ||
| 716 | } | ||
| 717 | |||
| 718 | return $regex; | ||
| 719 | } | ||
| 720 | |||
| 721 | 1; | ||
| 722 | |||
| 723 | =begin | ||
| 724 | |||
| 725 | =head1 NAME | ||
| 726 | |||
| 727 | Perlito5::Macro - Ast macros for Perlito | ||
| 728 | |||
| 729 | =head1 SYNOPSIS | ||
| 730 | |||
| 731 | $ast = $ast.op_assign() | ||
| 732 | |||
| 733 | =head1 DESCRIPTION | ||
| 734 | |||
| 735 | This module implements some Ast transformations for the Perlito compiler. | ||
| 736 | |||
| 737 | =head1 AUTHORS | ||
| 738 | |||
| 739 | Flavio Soibelmann Glock <fglock@gmail.com>. | ||
| 740 | The Pugs Team. | ||
| 741 | |||
| 742 | =head1 SEE ALSO | ||
| 743 | |||
| 744 | The Perl 6 homepage at L<http://dev.perl.org/perl6>. | ||
| 745 | |||
| 746 | The Pugs homepage at L<http://pugscode.org/>. | ||
| 747 | |||
| 748 | =head1 COPYRIGHT | ||
| 749 | |||
| 750 | Copyright 2011, 2012 by Flavio Soibelmann Glock. | ||
| 751 | |||
| 752 | This program is free software; you can redistribute it and/or modify it | ||
| 753 | under the same terms as Perl itself. | ||
| 754 | |||
| 755 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 756 | |||
| 757 | =end | ||
| 758 |