| 1 | package Language::Tea::ASTRefactor; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Language::Tea::Traverse; | ||
| 6 | use Language::Tea::StatementContext; | ||
| 7 | use Language::Tea::NodeUpLinker; | ||
| 8 | use Scalar::Util qw(blessed); | ||
| 9 | |||
| 10 | our $REFACTOR_VARIABLE = 0; | ||
| 11 | |||
| 12 | sub refactor { | ||
| 13 | my $root = shift; | ||
| 14 | Language::Tea::Traverse::visit_prefix( | ||
| 15 | $root, | ||
| 16 | sub { | ||
| 17 | my ( $node, $parent ) = ( shift, shift ); | ||
| 18 | return if ref $node eq 'HASH'; | ||
| 19 | for ( ref $node ) { | ||
| 20 | /^TeaPart::If$/ && do { | ||
| 21 | |||
| 22 | #warn "Reached an If..."; | ||
| 23 | if ( $node->{context}{rvalue} ) { | ||
| 24 | |||
| 25 | # if as rvalue: Refactored to defining a variable that will | ||
| 26 | # be setted both in then and in else. This variable will be | ||
| 27 | # used in the place where the if was. | ||
| 28 | warn | ||
| 29 | "Refactoring rvalue if condition using temporary variable at $node->{info}{file} line $node->{info}{line}\n"; | ||
| 30 | |||
| 31 | # we need to add lines in the code, so let's get to the closest | ||
| 32 | # liner to get its block. | ||
| 33 | my $walker = $node; | ||
| 34 | while ( !$walker->{context}{liner} ) { | ||
| 35 | |||
| 36 | #warn 'getting to the liner... '.ref $walker; | ||
| 37 | $walker = $walker->{__node_parent__}; | ||
| 38 | } | ||
| 39 | my $block = $walker->{__node_parent__}; | ||
| 40 | |||
| 41 | #warn 'walker stopped at '.(ref $walker).' block is '.(ref $block); | ||
| 42 | |||
| 43 | my $name = | ||
| 44 | 'refactoring_variable_' . $REFACTOR_VARIABLE++; | ||
| 45 | |||
| 46 | # Here we do a copy of the node to a new thing, because | ||
| 47 | # we'll re-bless the current node | ||
| 48 | my $ifstatement = bless { %{$node} }, 'TeaPart::If'; | ||
| 49 | $ifstatement->{__node_parent__} = $block; | ||
| 50 | $ifstatement->{context} = { | ||
| 51 | ireturn => 0, | ||
| 52 | rvalue => 0, | ||
| 53 | liner => 1 | ||
| 54 | }; | ||
| 55 | |||
| 56 | my $define_as_null = 0; | ||
| 57 | |||
| 58 | $node->{condition}{context}{rvalue} = 1; | ||
| 59 | $node->{condition}{__node_parent__} = $ifstatement; | ||
| 60 | $node->{then}{__node_parent__} = $ifstatement; | ||
| 61 | my $ireturn_then = | ||
| 62 | $node->{then}{arg_code}{statement}[-1]; | ||
| 63 | $ireturn_then->{context}{liner} = 0; | ||
| 64 | $ireturn_then->{context}{rvalue} = 1; | ||
| 65 | |||
| 66 | $node->{then}{arg_code}{statement}[-1] = bless { | ||
| 67 | info => { %{ $node->{info} } }, | ||
| 68 | __node_parent__ => $node->{then}{arg_code}, | ||
| 69 | context => | ||
| 70 | { liner => 1, rvalue => 0, ireturn => 0 }, | ||
| 71 | func => ( | ||
| 72 | bless { | ||
| 73 | info => { %{ $node->{info} } }, | ||
| 74 | context => | ||
| 75 | { liner => 0, rvalue => 0, ireturn => 0 }, | ||
| 76 | arg_symbol => 'set!', | ||
| 77 | }, | ||
| 78 | 'TeaPart::arg_symbol' | ||
| 79 | ), | ||
| 80 | arg => [ | ||
| 81 | ( | ||
| 82 | bless { | ||
| 83 | info => { %{ $node->{info} } }, | ||
| 84 | context => { | ||
| 85 | liner => 0, | ||
| 86 | rvalue => 1, | ||
| 87 | ireturn => 0 | ||
| 88 | }, | ||
| 89 | arg_symbol => $name | ||
| 90 | }, | ||
| 91 | 'TeaPart::arg_symbol' | ||
| 92 | ), | ||
| 93 | ( | ||
| 94 | bless { | ||
| 95 | info => { %{ $node->{info} } }, | ||
| 96 | context => { | ||
| 97 | liner => 0, | ||
| 98 | rvalue => 1, | ||
| 99 | ireturn => 0 | ||
| 100 | }, | ||
| 101 | arg_do => | ||
| 102 | { statement => [$ireturn_then] }, | ||
| 103 | }, | ||
| 104 | 'TeaPart::arg_do' | ||
| 105 | ) | ||
| 106 | ] | ||
| 107 | }, | ||
| 108 | 'TeaPart::Apply'; | ||
| 109 | $node->{then}{arg_code}{statement}[-1]{arg}[1] | ||
| 110 | {__node_parent__} = | ||
| 111 | $node->{then}{arg_code}{statement}[-1]; | ||
| 112 | |||
| 113 | if ( exists $node->{else} and blessed $node->{else} ) { | ||
| 114 | $node->{else}{__node_parent__} = $ifstatement; | ||
| 115 | my $ireturn_else = | ||
| 116 | $node->{else}{arg_code}{statement}[-1]; | ||
| 117 | $ireturn_else->{context}{liner} = 0; | ||
| 118 | $ireturn_else->{context}{rvalue} = 1; | ||
| 119 | $node->{else}{arg_code}{statement}[-1] = bless { | ||
| 120 | __node_parent__ => $node->{else}{arg_code}, | ||
| 121 | info => { %{ $node->{info} } }, | ||
| 122 | context => | ||
| 123 | { liner => 1, rvalue => 0, ireturn => 0 }, | ||
| 124 | func => ( | ||
| 125 | bless { | ||
| 126 | info => { %{ $node->{info} } }, | ||
| 127 | context => { | ||
| 128 | liner => 0, | ||
| 129 | rvalue => 0, | ||
| 130 | ireturn => 0 | ||
| 131 | }, | ||
| 132 | arg_symbol => 'set!', | ||
| 133 | }, | ||
| 134 | 'TeaPart::arg_symbol' | ||
| 135 | ), | ||
| 136 | arg => [ | ||
| 137 | ( | ||
| 138 | bless { | ||
| 139 | info => { %{ $node->{info} } }, | ||
| 140 | context => { | ||
| 141 | liner => 0, | ||
| 142 | rvalue => 0, | ||
| 143 | ireturn => 0 | ||
| 144 | }, | ||
| 145 | arg_symbol => $name | ||
| 146 | }, | ||
| 147 | 'TeaPart::arg_symbol' | ||
| 148 | ), | ||
| 149 | ( | ||
| 150 | bless { | ||
| 151 | __node_parent__ => | ||
| 152 | $node->{else}{arg_code}, | ||
| 153 | info => { %{ $node->{info} } }, | ||
| 154 | context => { | ||
| 155 | liner => 0, | ||
| 156 | rvalue => 1, | ||
| 157 | ireturn => 0 | ||
| 158 | }, | ||
| 159 | arg_do => | ||
| 160 | { statement => [$ireturn_else] }, | ||
| 161 | }, | ||
| 162 | 'TeaPart::arg_do' | ||
| 163 | ) | ||
| 164 | ] | ||
| 165 | }, | ||
| 166 | 'TeaPart::Apply'; | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | delete $ifstatement->{else}; | ||
| 170 | $define_as_null = bless {}, 'TeaPart::arg_symbol'; | ||
| 171 | } | ||
| 172 | |||
| 173 | $node->{arg_symbol} = $name; | ||
| 174 | delete $node->{condition}; | ||
| 175 | delete $node->{then}; | ||
| 176 | delete $node->{else}; | ||
| 177 | $node->{context}{liner} = 0; | ||
| 178 | $node->{context}{rvalue} = 1; | ||
| 179 | bless $node, 'TeaPart::arg_symbol'; | ||
| 180 | |||
| 181 | # we know that a parent with a liner contains an entry called | ||
| 182 | # statement which is an array. So, let's find out which index | ||
| 183 | # is the walker in. | ||
| 184 | my $idx = 0; | ||
| 185 | my $arr; | ||
| 186 | $arr = $block->{arg_code}{statement} | ||
| 187 | if exists $block->{arg_code} | ||
| 188 | && ref $block->{arg_code}{statement} eq 'ARRAY'; | ||
| 189 | unless ($arr) { | ||
| 190 | $arr = $block->{statement}; | ||
| 191 | } | ||
| 192 | for ( @{$arr} ) { | ||
| 193 | if ( $_ == $walker ) { | ||
| 194 | last; | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | $idx++; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | splice @{$arr}, $idx, 0, ( | ||
| 201 | |||
| 202 | # insert a comment explaining what happened | ||
| 203 | ( | ||
| 204 | bless { | ||
| 205 | context => | ||
| 206 | { liner => 1, rvalue => 0, ireturn => 0 }, | ||
| 207 | info => { %{ $node->{info} } }, | ||
| 208 | comment_text => | ||
| 209 | "Refactored from a rvalue if condition" | ||
| 210 | }, | ||
| 211 | 'TeaPart::Comment' | ||
| 212 | ), | ||
| 213 | ( | ||
| 214 | bless { | ||
| 215 | context => | ||
| 216 | { liner => 1, rvalue => 0, ireturn => 0 }, | ||
| 217 | info => { %{ $node->{info} } }, | ||
| 218 | comment_text => | ||
| 219 | "at $node->{info}{file} line $node->{info}{line}" | ||
| 220 | }, | ||
| 221 | 'TeaPart::Comment' | ||
| 222 | ), | ||
| 223 | |||
| 224 | # define variable false | ||
| 225 | ( | ||
| 226 | bless { | ||
| 227 | context => | ||
| 228 | { liner => 1, rvalue => 0, ireturn => 0 }, | ||
| 229 | info => { %{ $node->{info} } }, | ||
| 230 | arg_symbol => $name, | ||
| 231 | }, | ||
| 232 | 'TeaPart::Define' | ||
| 233 | ), | ||
| 234 | |||
| 235 | # block | ||
| 236 | $ifstatement | ||
| 237 | ); | ||
| 238 | Language::Tea::NodeUpLinker::create_links($root); | ||
| 239 | Language::Tea::StatementContext::annotate_context( | ||
| 240 | $root); | ||
| 241 | refactor($root); | ||
| 242 | return $node; | ||
| 243 | } | ||
| 244 | elsif ( ref $node->{condition} eq 'TeaPart::arg_code' ) { | ||
| 245 | if ( | ||
| 246 | scalar @{ $node->{condition}{arg_code}{statement} } | ||
| 247 | == 0 ) | ||
| 248 | { | ||
| 249 | die | ||
| 250 | "Empty block in if condition at $node->{info}{file} line $node->{info}{line}\n"; | ||
| 251 | } | ||
| 252 | elsif ( | ||
| 253 | scalar @{ $node->{condition}{arg_code}{statement} } | ||
| 254 | == 1 ) | ||
| 255 | { | ||
| 256 | |||
| 257 | # We can refactor this case to a arg_do instead of | ||
| 258 | # a arg_code. | ||
| 259 | warn | ||
| 260 | "Refactoring if condition from code block to substitution at $node->{info}{file} line $node->{info}{line}\n"; | ||
| 261 | $node->{condition}{arg_do}{statement}[0] = | ||
| 262 | $node->{condition}{arg_code}{statement}[0]; | ||
| 263 | bless $node->{condition}, 'TeaPart::arg_do'; | ||
| 264 | $node->{condition}{arg_do}{statement}[0]{context} | ||
| 265 | {liner} = 0; | ||
| 266 | } | ||
| 267 | else { | ||
| 268 | |||
| 269 | # We can refactor this case to: | ||
| 270 | # a boolean variable declaration | ||
| 271 | # a code block wich sets that variable | ||
| 272 | # use that symbol as condition | ||
| 273 | warn | ||
| 274 | "Refactoring if condition using temporary variable and outside code block at $node->{info}{file} line $node->{info}{line}\n"; | ||
| 275 | my $name = | ||
| 276 | 'refactoring_variable_' . $REFACTOR_VARIABLE++; | ||
| 277 | $node->{condition}{arg_symbol} = $name; | ||
| 278 | my $condition = $node->{condition}{arg_code}; | ||
| 279 | delete $node->{condition}{arg_code}; | ||
| 280 | bless $node->{condition}, 'TeaPart::arg_symbol'; | ||
| 281 | |||
| 282 | my $ireturn = $condition->{statement}[-1]; | ||
| 283 | $ireturn->{context}{liner} = 0; | ||
| 284 | $ireturn->{context}{rvalue} = 1; | ||
| 285 | $condition->{statement}[-1] = bless { | ||
| 286 | info => { %{ $node->{info} } }, | ||
| 287 | context => | ||
| 288 | { liner => 1, rvalue => 0, ireturn => 0 }, | ||
| 289 | func => ( | ||
| 290 | bless { | ||
| 291 | info => { %{ $node->{info} } }, | ||
| 292 | context => { | ||
| 293 | liner => 0, | ||
| 294 | rvalue => 0, | ||
| 295 | ireturn => 0 | ||
| 296 | }, | ||
| 297 | arg_symbol => 'set!', | ||
| 298 | }, | ||
| 299 | 'TeaPart::arg_symbol' | ||
| 300 | ), | ||
| 301 | arg => [ | ||
| 302 | ( | ||
| 303 | bless { | ||
| 304 | info => { %{ $node->{info} } }, | ||
| 305 | context => { | ||
| 306 | liner => 0, | ||
| 307 | rvalue => 0, | ||
| 308 | ireturn => 0 | ||
| 309 | }, | ||
| 310 | arg_symbol => $name | ||
| 311 | }, | ||
| 312 | 'TeaPart::arg_symbol' | ||
| 313 | ), | ||
| 314 | ( | ||
| 315 | bless { | ||
| 316 | info => { %{ $node->{info} } }, | ||
| 317 | context => { | ||
| 318 | liner => 0, | ||
| 319 | rvalue => 1, | ||
| 320 | ireturn => 0 | ||
| 321 | }, | ||
| 322 | arg_do => | ||
| 323 | { statement => [$ireturn] }, | ||
| 324 | }, | ||
| 325 | 'TeaPart::arg_do' | ||
| 326 | ) | ||
| 327 | ] | ||
| 328 | }, | ||
| 329 | 'TeaPart::Apply'; | ||
| 330 | |||
| 331 | # Now let's get back to the parent, until we get a liner | ||
| 332 | # then get back one more, find where the liner is inside | ||
| 333 | # the parent, and then slice the array adding the new | ||
| 334 | # entries. | ||
| 335 | my $walker = $node; | ||
| 336 | while ( !$walker->{context}{liner} ) { | ||
| 337 | $walker = $walker->{__node_parent__}; | ||
| 338 | } | ||
| 339 | my $block = $walker->{__node_parent__}; | ||
| 340 | |||
| 341 | my $codeblock = bless { | ||
| 342 | __node_parent__ => $block, | ||
| 343 | context => | ||
| 344 | { liner => 1, rvalue => 0, ireturn => 0 }, | ||
| 345 | arg_code => $condition, | ||
| 346 | info => { %{ $node->{info} } }, | ||
| 347 | }, | ||
| 348 | 'TeaPart::arg_code'; | ||
| 349 | |||
| 350 | $condition->{__node_parent__} = $codeblock; | ||
| 351 | |||
| 352 | # we know that a parent with a liner contains an entry called | ||
| 353 | # statement which is an array. So, let's find out which index | ||
| 354 | # is the walker in. | ||
| 355 | my $arr; | ||
| 356 | $arr = $block->{arg_code}{statement} | ||
| 357 | if exists $block->{arg_code} | ||
| 358 | and ref $block->{arg_code}{statement} eq 'ARRAY'; | ||
| 359 | unless ($arr) { | ||
| 360 | $arr = $block->{statement}; | ||
| 361 | } | ||
| 362 | my $idx = 0; | ||
| 363 | for ( @{$arr} ) { | ||
| 364 | if ( $_ == $walker ) { | ||
| 365 | last; | ||
| 366 | } | ||
| 367 | else { | ||
| 368 | $idx++; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | |||
| 372 | # It's important to notice that this only works properly because | ||
| 373 | # of this offset. As the visit_prefix here is index-based, we need | ||
| 374 | # to make sure that it will get again to all of this and to this | ||
| 375 | # children. | ||
| 376 | # The comment here, besides helping the end user, makes sure the | ||
| 377 | # ASTRefactor goes through all items. | ||
| 378 | splice @{$arr}, $idx, 0, ( | ||
| 379 | |||
| 380 | # insert a comment explaining what happened | ||
| 381 | ( | ||
| 382 | bless { | ||
| 383 | context => { | ||
| 384 | liner => 1, | ||
| 385 | rvalue => 0, | ||
| 386 | ireturn => 0 | ||
| 387 | }, | ||
| 388 | info => { %{ $node->{info} } }, | ||
| 389 | comment_text => | ||
| 390 | "Refactored from a multi-statements if condition" | ||
| 391 | }, | ||
| 392 | 'TeaPart::Comment' | ||
| 393 | ), | ||
| 394 | ( | ||
| 395 | bless { | ||
| 396 | context => { | ||
| 397 | liner => 1, | ||
| 398 | rvalue => 0, | ||
| 399 | ireturn => 0 | ||
| 400 | }, | ||
| 401 | info => { %{ $node->{info} } }, | ||
| 402 | comment_text => | ||
| 403 | "at $node->{info}{file} line $node->{info}{line}" | ||
| 404 | }, | ||
| 405 | 'TeaPart::Comment' | ||
| 406 | ), | ||
| 407 | |||
| 408 | # define variable false | ||
| 409 | ( | ||
| 410 | bless { | ||
| 411 | context => { | ||
| 412 | liner => 1, | ||
| 413 | rvalue => 0, | ||
| 414 | ireturn => 0 | ||
| 415 | }, | ||
| 416 | info => { %{ $node->{info} } }, | ||
| 417 | type => 'Boolean', | ||
| 418 | arg_symbol => $name, | ||
| 419 | statement => [ { type => 'Boolean' } ], | ||
| 420 | }, | ||
| 421 | 'TeaPart::Define' | ||
| 422 | ), | ||
| 423 | |||
| 424 | # block | ||
| 425 | $codeblock | ||
| 426 | ); | ||
| 427 | |||
| 428 | Language::Tea::NodeUpLinker::create_links($root); | ||
| 429 | Language::Tea::StatementContext::annotate_context( | ||
| 430 | $root); | ||
| 431 | refactor($root); | ||
| 432 | return $node; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | last; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | return; | ||
| 439 | }, | ||
| 440 | undef | ||
| 441 | ); | ||
| 442 | } | ||
| 443 | |||
| 444 | 1; |