| 1 | package Perlito5::Perl6::TreeGrammar; | ||
| 2 | use Perlito5::Dumper; | ||
| 3 | use strict; | ||
| 4 | use Perlito5::TreeGrammar; | ||
| 5 | |||
| 6 | sub refactor_range_operator { | ||
| 7 | my ($class, $in) = @_; | ||
| 8 | Perlito5::TreeGrammar::render( | ||
| 9 | [ And => [ Lookup => 'code', [ Value => 'infix:<..>' ] ], | ||
| 10 | [ Lookup => 'arguments', | ||
| 11 | [ And => [ Index => 0, [ And => [ Ref => 'Perlito5::AST::Int' ], | ||
| 12 | [ Lookup => 'int', [ Value => 0 ] ] | ||
| 13 | ] | ||
| 14 | ], # first argument is 0 | ||
| 15 | [ Index => 1, [ Or => | ||
| 16 | # 0 .. 9 ==> ^10 | ||
| 17 | [ And => [ Ref => 'Perlito5::AST::Int' ], | ||
| 18 | [ Action => sub { | ||
| 19 | $in->{code} = 'p6_prefix:<^>'; | ||
| 20 | $_[0]{int}++; | ||
| 21 | shift @{ $in->{arguments} }; | ||
| 22 | } | ||
| 23 | ], | ||
| 24 | ], | ||
| 25 | # 0..$#$num to $num.keys | ||
| 26 | [ And => [ Ref => 'Perlito5::AST::Apply' ], | ||
| 27 | [ Lookup => 'code', [ Value => 'prefix:<$#>' ] ], | ||
| 28 | [ Action => sub { | ||
| 29 | bless $in, 'Perlito5::AST::Call'; | ||
| 30 | delete $in->{code}; | ||
| 31 | $in->{method} = 'keys'; | ||
| 32 | shift @{ $in->{arguments} }; | ||
| 33 | my $invocant = shift @{ $in->{arguments} }; | ||
| 34 | $in->{invocant} = $invocant->{arguments}[0]; | ||
| 35 | } | ||
| 36 | ], | ||
| 37 | ], | ||
| 38 | # 0..$#num to @num.keys | ||
| 39 | [ And => [ Ref => 'Perlito5::AST::Var' ], | ||
| 40 | [ Lookup => 'sigil', [ Value => '$#' ] ], | ||
| 41 | [ Action => sub { | ||
| 42 | bless $in, 'Perlito5::AST::Call'; | ||
| 43 | delete $in->{code}; | ||
| 44 | $in->{method} = 'keys'; | ||
| 45 | $in->{arguments} = []; | ||
| 46 | my $invocant = $_[0]; | ||
| 47 | $invocant->{sigil} = '@'; | ||
| 48 | $in->{invocant} = $invocant; | ||
| 49 | } | ||
| 50 | ], | ||
| 51 | ], | ||
| 52 | ] | ||
| 53 | ], | ||
| 54 | ] | ||
| 55 | ] | ||
| 56 | ], | ||
| 57 | $in | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub refactor_while_glob { | ||
| 62 | my ($class, $in) = @_; | ||
| 63 | Perlito5::TreeGrammar::render( | ||
| 64 | [ Ref => 'Perlito5::AST::While', | ||
| 65 | [ Lookup => 'cond', | ||
| 66 | [ And => [ Ref => 'Perlito5::AST::Apply' ], | ||
| 67 | [ Lookup => 'code', [ Value => 'readline' ] ], | ||
| 68 | [ Action => sub { bless $in, 'Perlito5::AST::For' } ] | ||
| 69 | ] | ||
| 70 | ] | ||
| 71 | ], | ||
| 72 | $in | ||
| 73 | ); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub refactor_sub_arguments { | ||
| 77 | my ($class, $in) = @_; | ||
| 78 | my ( $rule, $result ); | ||
| 79 | |||
| 80 | $rule = Perlito5::Perl6::TreeGrammar::is_sub( | ||
| 81 | [ Action => sub { | ||
| 82 | my $sub = $_[0]; | ||
| 83 | my $stmts; | ||
| 84 | my $var; | ||
| 85 | Perlito5::TreeGrammar::render( | ||
| 86 | [ Lookup => 'block', | ||
| 87 | [ Progn => [ Action => sub { $stmts = $_[0] } ], | ||
| 88 | [ Star => [ | ||
| 89 | Index => 0, | ||
| 90 | Perlito5::Perl6::TreeGrammar::operator_eq( 'infix:<=>', | ||
| 91 | [ Lookup => 'arguments', | ||
| 92 | [ And => [ Index => 0, | ||
| 93 | Perlito5::Perl6::TreeGrammar::my_var( | ||
| 94 | [ Action => sub { | ||
| 95 | $var = $_[0]->{var}; | ||
| 96 | } | ||
| 97 | ] | ||
| 98 | ) | ||
| 99 | ], | ||
| 100 | [ Index => 1, | ||
| 101 | Perlito5::Perl6::TreeGrammar::shift_arg() | ||
| 102 | ], | ||
| 103 | [ Action => sub { | ||
| 104 | push @{ $sub->{args} }, $var; | ||
| 105 | shift @$stmts; | ||
| 106 | } | ||
| 107 | ] | ||
| 108 | ] | ||
| 109 | ] | ||
| 110 | ) | ||
| 111 | ] | ||
| 112 | ] | ||
| 113 | ] | ||
| 114 | ], | ||
| 115 | $sub | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | ] | ||
| 119 | ); | ||
| 120 | |||
| 121 | $result = Perlito5::TreeGrammar::scan( $rule, $in ); | ||
| 122 | # print "result $result\n"; | ||
| 123 | # print Dumper $in; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub is_sub { | ||
| 127 | [ Ref => 'Perlito5::AST::Sub', | ||
| 128 | ( @_ ? [ Progn => @_ ] : () ) | ||
| 129 | ]; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub named_sub { | ||
| 133 | [ Ref => 'Perlito5::AST::Sub', | ||
| 134 | [ And => [ Lookup => 'name', [ Not => [ Value => '' ] ] ], | ||
| 135 | ( @_ ? [ Progn => @_ ] : () ) | ||
| 136 | ] | ||
| 137 | ]; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub operator_eq { | ||
| 141 | my $name = shift; | ||
| 142 | [ Ref => 'Perlito5::AST::Apply', | ||
| 143 | [ And => [ Lookup => 'code', [ Value => $name ] ], | ||
| 144 | ( @_ ? [ Progn => @_ ] : () ) | ||
| 145 | ] | ||
| 146 | ]; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub my_var { | ||
| 150 | [ Ref => 'Perlito5::AST::Decl', | ||
| 151 | [ And => [ Lookup => 'decl', [ Value => 'my' ] ], | ||
| 152 | ( @_ ? [ Progn => @_ ] : () ) | ||
| 153 | ] | ||
| 154 | ]; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub var_is_at { | ||
| 158 | [ Ref => 'Perlito5::AST::Var', | ||
| 159 | [ And => [ Lookup => 'sigil', [ Value => '@' ] ], | ||
| 160 | [ Lookup => 'name', [ Value => '_' ] ], | ||
| 161 | ( @_ ? [ Progn => @_ ] : () ) | ||
| 162 | ] | ||
| 163 | ]; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub shift_arg { | ||
| 167 | [ Ref => 'Perlito5::AST::Apply', | ||
| 168 | [ And => [ Lookup => 'code', [ Value => 'shift' ] ], | ||
| 169 | [ Or => [ Lookup => 'arguments', | ||
| 170 | [ Not => [ Index => 0 ] ] # arg list is empty | ||
| 171 | ], | ||
| 172 | [ Lookup => 'arguments', | ||
| 173 | [ Index => 0, | ||
| 174 | var_is_at() # arg is @_ | ||
| 175 | ] | ||
| 176 | ], | ||
| 177 | ], | ||
| 178 | ( @_ ? [ Progn => @_ ] : () ) | ||
| 179 | ] | ||
| 180 | ]; | ||
| 181 | } | ||
| 182 | |||
| 183 | 1; | ||
| 184 |