| 1 | package Language::Tea::StatementContext; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Language::Tea::Traverse; | ||
| 6 | |||
| 7 | sub annotate_context { | ||
| 8 | my $root = shift; | ||
| 9 | Language::Tea::Traverse::visit_prefix( | ||
| 10 | $root, | ||
| 11 | sub { | ||
| 12 | my $node = shift; | ||
| 13 | for ( ref $node ) { | ||
| 14 | /^TeaProgram$/ && do { | ||
| 15 | $node->{context}{rvalue} = 0; | ||
| 16 | $node->{context}{ireturn} = 0; | ||
| 17 | for ( @{ $node->{statement} } ) { | ||
| 18 | $_->{context}{rvalue} = 0; | ||
| 19 | $_->{context}{ireturn} = 0; | ||
| 20 | $_->{context}{liner} = 1; | ||
| 21 | } | ||
| 22 | last; | ||
| 23 | }; | ||
| 24 | /^TeaPart::If$/ && do { | ||
| 25 | $node->{condition}{context}{rvalue} = 1; | ||
| 26 | $node->{condition}{context}{ireturn} = 0; | ||
| 27 | if ( $node->{context}{rvalue} || $node->{context}{ireturn} ) | ||
| 28 | { | ||
| 29 | $node->{then}{context}{rvalue} = | ||
| 30 | $node->{context}{rvalue}; | ||
| 31 | $node->{else}{context}{rvalue} = | ||
| 32 | $node->{context}{rvalue}; | ||
| 33 | $node->{then}{context}{ireturn} = | ||
| 34 | $node->{context}{ireturn}; | ||
| 35 | $node->{else}{context}{ireturn} = | ||
| 36 | $node->{context}{ireturn}; | ||
| 37 | } | ||
| 38 | last; | ||
| 39 | }; | ||
| 40 | /^TeaPart::arg_code$/ && do { | ||
| 41 | my $last_statement; | ||
| 42 | for ( @{ $node->{arg_code}{statement} } ) { | ||
| 43 | $_->{context}{rvalue} = 0; | ||
| 44 | $_->{context}{ireturn} = 0; | ||
| 45 | $_->{context}{liner} = 1; | ||
| 46 | $last_statement = $_; | ||
| 47 | } | ||
| 48 | if ( $node->{context}{rvalue} || $node->{context}{ireturn} ) | ||
| 49 | { | ||
| 50 | $last_statement->{context}{rvalue} = 0; | ||
| 51 | $last_statement->{context}{ireturn} = 1; | ||
| 52 | } | ||
| 53 | last; | ||
| 54 | }; | ||
| 55 | /^TeaPart::Define$/ && do { | ||
| 56 | $node->{statement}[0]{context}{rvalue} = 1; | ||
| 57 | last; | ||
| 58 | }; | ||
| 59 | /^TeaPart::Apply$/ && do { | ||
| 60 | for ( @{ $node->{arg} } ) { | ||
| 61 | $_->{context}{rvalue} = 1; | ||
| 62 | $_->{context}{ireturn} = 0; | ||
| 63 | } | ||
| 64 | last; | ||
| 65 | }; | ||
| 66 | /^TeaPart::New$/ && do { | ||
| 67 | unless ( $node->{context}{rvalue} | ||
| 68 | || $node->{context}{ireturn} ) | ||
| 69 | { | ||
| 70 | warn | ||
| 71 | "Useless use of new in void context at $node->{info}{file} line $node->{info}{line}.\n"; | ||
| 72 | } | ||
| 73 | for ( @{ $node->{arg} } ) { | ||
| 74 | $_->{context}{rvalue} = 1; | ||
| 75 | $_->{context}{ireturn} = 0; | ||
| 76 | } | ||
| 77 | last; | ||
| 78 | }; | ||
| 79 | /^TeaPart::arg_do$/ && do { | ||
| 80 | if ( $node->{context}{rvalue} || $node->{context}{ireturn} ) | ||
| 81 | { | ||
| 82 | $node->{arg_do}{statement}[0]{context}{rvalue} = | ||
| 83 | $node->{context}{rvalue}; | ||
| 84 | $node->{arg_do}{statement}[0]{context}{ireturn} = | ||
| 85 | $node->{context}{ireturn}; | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | warn | ||
| 89 | "Useless use of function substitution at $node->{info}{file} line $node->{info}{line}.\n"; | ||
| 90 | } | ||
| 91 | last; | ||
| 92 | }; | ||
| 93 | /^TeaPart::DefineFunc$/ && do { | ||
| 94 | $node->{context}{ireturn} = 1; | ||
| 95 | my $last_statement; | ||
| 96 | for ( @{ $node->{arg_code}{statement} } ) { | ||
| 97 | $_->{context}{rvalue} = 0; | ||
| 98 | $_->{context}{ireturn} = 0; | ||
| 99 | $_->{context}{liner} = 1; | ||
| 100 | $last_statement = $_; | ||
| 101 | } | ||
| 102 | $last_statement->{context}{ireturn} = 1; | ||
| 103 | last; | ||
| 104 | }; | ||
| 105 | /^TeaPart::Method$/ && do { | ||
| 106 | $node->{arg}[0]{context}{ireturn} = 1 if ( $node->{method}{arg_symbol} !~ /set.*/i && $node->{method}{arg_symbol} !~ /constructor/i); | ||
| 107 | }; | ||
| 108 | } | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; |