| 1 | use v5; | ||
| 2 | use Perlito5::AST; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | package Perlito5::AST::CompUnit; | ||
| 6 | { | ||
| 7 | sub emit_begin_scratchpad { | ||
| 8 | my $self = $_[0]; | ||
| 9 | return __PACKAGE__->new( | ||
| 10 | %$self, | ||
| 11 | body => [ map { defined($_) && $_->emit_begin_scratchpad() } @{$self->{body}} ], | ||
| 12 | ); | ||
| 13 | } | ||
| 14 | } | ||
| 15 | |||
| 16 | package Perlito5::AST::Int; | ||
| 17 | { | ||
| 18 | sub emit_begin_scratchpad { return $_[0] } | ||
| 19 | } | ||
| 20 | |||
| 21 | package Perlito5::AST::Num; | ||
| 22 | { | ||
| 23 | sub emit_begin_scratchpad { return $_[0] } | ||
| 24 | } | ||
| 25 | |||
| 26 | package Perlito5::AST::Buf; | ||
| 27 | { | ||
| 28 | sub emit_begin_scratchpad { return $_[0] } | ||
| 29 | } | ||
| 30 | |||
| 31 | package Perlito5::AST::Block; | ||
| 32 | { | ||
| 33 | sub emit_begin_scratchpad { | ||
| 34 | my $self = $_[0]; | ||
| 35 | return __PACKAGE__->new( | ||
| 36 | %$self, | ||
| 37 | stmts => [ map { defined($_) && $_->emit_begin_scratchpad() } @{$self->{stmts}} ], | ||
| 38 | ( $self->{continue} | ||
| 39 | ? ( continue => $self->{continue}->emit_begin_scratchpad() ) | ||
| 40 | : () ), | ||
| 41 | # continue => [ map { defined($_) && $_->emit_begin_scratchpad() } @{$self->{continue}} ], | ||
| 42 | ); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | package Perlito5::AST::Index; | ||
| 47 | { | ||
| 48 | sub emit_begin_scratchpad { | ||
| 49 | my $self = $_[0]; | ||
| 50 | return __PACKAGE__->new( | ||
| 51 | %$self, | ||
| 52 | obj => $self->{obj}->emit_begin_scratchpad(), | ||
| 53 | index_exp => $self->{index_exp}->emit_begin_scratchpad(), | ||
| 54 | ); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | package Perlito5::AST::Lookup; | ||
| 59 | { | ||
| 60 | sub emit_begin_scratchpad { | ||
| 61 | my $self = $_[0]; | ||
| 62 | return __PACKAGE__->new( | ||
| 63 | %$self, | ||
| 64 | obj => $self->{obj}->emit_begin_scratchpad(), | ||
| 65 | index_exp => $self->{index_exp}->emit_begin_scratchpad(), | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | package Perlito5::AST::Var; | ||
| 71 | { | ||
| 72 | sub emit_begin_scratchpad { | ||
| 73 | my $self = $_[0]; | ||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub is_begin_scratchpad { | ||
| 78 | my ($self) = @_; | ||
| 79 | if (!$self->{namespace} && $Perlito5::BEGIN_SCRATCHPAD{ $self->{_id} || "" }) { | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | return 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub to_begin_scratchpad { | ||
| 86 | my ($self) = @_; | ||
| 87 | if (!$self->{namespace} && $Perlito5::BEGIN_SCRATCHPAD{ $self->{_id} || "" }) { | ||
| 88 | # warn "rewrite sigil ", $self->{sigil}, " real-sigil ", $self->{_real_sigil}, "\n"; | ||
| 89 | $self = __PACKAGE__->new( | ||
| 90 | %$self, | ||
| 91 | _decl => "global", | ||
| 92 | namespace => "Perlito5::BEGIN", | ||
| 93 | name => "_" . $self->{_id} . "_" . $self->{name}, | ||
| 94 | ); | ||
| 95 | # warn "rewritten [ @{[ %$self ]} ]\n"; | ||
| 96 | } | ||
| 97 | return $self; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | package Perlito5::AST::Call; | ||
| 102 | { | ||
| 103 | sub emit_begin_scratchpad { | ||
| 104 | my $self = $_[0]; | ||
| 105 | my $invocant = $self->{invocant}->emit_begin_scratchpad(); | ||
| 106 | my $arguments; | ||
| 107 | if ( $self->{method} eq 'postcircumfix:<[ ]>' ) { | ||
| 108 | $arguments = $self->{arguments}->emit_begin_scratchpad(); | ||
| 109 | } | ||
| 110 | elsif ( $self->{method} eq 'postcircumfix:<{ }>' ) { | ||
| 111 | $arguments = $self->{arguments}->emit_begin_scratchpad(); | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | $arguments = [ map { $_->emit_begin_scratchpad() } @{$self->{arguments}} ]; | ||
| 115 | } | ||
| 116 | my $meth = $self->{method}; | ||
| 117 | if ( ref($meth) eq 'Perlito5::AST::Var' ) { | ||
| 118 | $meth = $meth->emit_begin_scratchpad(); | ||
| 119 | } | ||
| 120 | return __PACKAGE__->new( | ||
| 121 | %$self, | ||
| 122 | method => $meth, | ||
| 123 | invocant => $invocant, | ||
| 124 | arguments => $arguments, | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | package Perlito5::AST::Apply; | ||
| 130 | { | ||
| 131 | sub emit_begin_scratchpad_args { | ||
| 132 | my $self = $_[0]; | ||
| 133 | return () if !$self->{arguments}; | ||
| 134 | return map { $_->emit_begin_scratchpad() } @{$self->{arguments}}; | ||
| 135 | } | ||
| 136 | sub emit_begin_scratchpad { | ||
| 137 | my $self = $_[0]; | ||
| 138 | my $code; | ||
| 139 | if (ref $self->{code}) { | ||
| 140 | $code = $self->{code}->emit_begin_scratchpad(); | ||
| 141 | } | ||
| 142 | else { | ||
| 143 | $code = $self->{code}; | ||
| 144 | } | ||
| 145 | my $arguments; | ||
| 146 | if (ref $self->{arguments}) { | ||
| 147 | $arguments = [ map { $_->emit_begin_scratchpad() } @{$self->{arguments}} ]; | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | $arguments = $self->{arguments}; | ||
| 151 | } | ||
| 152 | |||
| 153 | if ($code eq 'eval' && $self->{_scope}) {; | ||
| 154 | $self->{_scope}{block} = [ | ||
| 155 | grep { $_->{_decl} ne 'global' } | ||
| 156 | map { $_->emit_begin_scratchpad() } | ||
| 157 | @{ $self->{_scope}{block} } | ||
| 158 | ]; | ||
| 159 | } | ||
| 160 | |||
| 161 | if ($code eq 'my') { | ||
| 162 | my @arg; | ||
| 163 | for my $var (@$arguments) { | ||
| 164 | if ($var->{namespace} && $var->{namespace} eq 'Perlito5::BEGIN') { | ||
| 165 | push @arg, $var; | ||
| 166 | } | ||
| 167 | else { | ||
| 168 | push @arg, __PACKAGE__->new( code => $code, arguments => [$var] ); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | return __PACKAGE__->new( code => 'circumfix:<( )>', arguments => \@arg ); | ||
| 172 | } | ||
| 173 | |||
| 174 | return __PACKAGE__->new( | ||
| 175 | %$self, | ||
| 176 | code => $code, | ||
| 177 | arguments => $arguments, | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | package Perlito5::AST::If; | ||
| 183 | { | ||
| 184 | sub emit_begin_scratchpad { | ||
| 185 | my $self = $_[0]; | ||
| 186 | return __PACKAGE__->new( | ||
| 187 | %$self, | ||
| 188 | cond => $self->{cond}->emit_begin_scratchpad(), | ||
| 189 | body => $self->{body}->emit_begin_scratchpad(), | ||
| 190 | otherwise => $self->{otherwise}->emit_begin_scratchpad(), | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | package Perlito5::AST::When; | ||
| 196 | { | ||
| 197 | sub emit_begin_scratchpad { | ||
| 198 | my $self = $_[0]; | ||
| 199 | return __PACKAGE__->new( | ||
| 200 | %$self, | ||
| 201 | cond => $self->{cond}->emit_begin_scratchpad(), | ||
| 202 | body => $self->{body}->emit_begin_scratchpad(), | ||
| 203 | ); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | package Perlito5::AST::While; | ||
| 209 | { | ||
| 210 | sub emit_begin_scratchpad { | ||
| 211 | my $self = $_[0]; | ||
| 212 | return __PACKAGE__->new( | ||
| 213 | %$self, | ||
| 214 | cond => $self->{cond}->emit_begin_scratchpad(), | ||
| 215 | body => $self->{body}->emit_begin_scratchpad(), | ||
| 216 | ); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | package Perlito5::AST::For; | ||
| 221 | { | ||
| 222 | sub emit_begin_scratchpad { | ||
| 223 | my $self = $_[0]; | ||
| 224 | my $cond; | ||
| 225 | if (ref($self->{cond}) eq 'ARRAY') { | ||
| 226 | # C-style for | ||
| 227 | $cond = [ map { defined($_) ? $_->emit_begin_scratchpad() : $_ } @{$self->{cond}} ]; | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | $cond = $self->{cond}->emit_begin_scratchpad(), | ||
| 231 | } | ||
| 232 | return __PACKAGE__->new( | ||
| 233 | %$self, | ||
| 234 | cond => $cond, | ||
| 235 | body => $self->{body}->emit_begin_scratchpad(), | ||
| 236 | ( $self->{continue} | ||
| 237 | ? ( continue => $self->{continue}->emit_begin_scratchpad() ) | ||
| 238 | : () ), | ||
| 239 | ); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | package Perlito5::AST::Decl; | ||
| 244 | { | ||
| 245 | sub emit_begin_scratchpad { | ||
| 246 | my $self = $_[0]; | ||
| 247 | my $var = $self->{var}->emit_begin_scratchpad(); | ||
| 248 | if ($var->{namespace} && $var->{namespace} eq 'Perlito5::BEGIN' ) { | ||
| 249 | return $var; | ||
| 250 | } | ||
| 251 | return __PACKAGE__->new( | ||
| 252 | %$self, | ||
| 253 | var => $var, | ||
| 254 | ); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | package Perlito5::AST::Sub; | ||
| 259 | { | ||
| 260 | sub emit_begin_scratchpad { | ||
| 261 | my $self = $_[0]; | ||
| 262 | my @stmts; | ||
| 263 | if (defined $self->{block}) { | ||
| 264 | @stmts = @{$self->{block}{stmts}}; | ||
| 265 | @stmts = map { $_->emit_begin_scratchpad() } @stmts; | ||
| 266 | $self = __PACKAGE__->new( | ||
| 267 | %$self, | ||
| 268 | ( $self->{block} | ||
| 269 | ? ( block => Perlito5::AST::Block->new( | ||
| 270 | %{$self->{block}}, | ||
| 271 | stmts => [ @stmts ], | ||
| 272 | ) ) | ||
| 273 | : () ), | ||
| 274 | ); | ||
| 275 | } | ||
| 276 | return $self; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | 1; | ||
| 281 | |||
| 282 | =begin | ||
| 283 | |||
| 284 | =head1 NAME | ||
| 285 | |||
| 286 | Perlito5::AST::CompileTime - Code generator for Perlito5-in-CompileTime | ||
| 287 | |||
| 288 | =head1 SYNOPSIS | ||
| 289 | |||
| 290 | $program->emit_begin_scratchpad() # generated CompileTime code | ||
| 291 | |||
| 292 | =head1 DESCRIPTION | ||
| 293 | |||
| 294 | This module generates CompileTime code for the Perlito compiler. | ||
| 295 | CompileTime code is the code that runs in BEGIN blocks. | ||
| 296 | |||
| 297 | =head1 AUTHORS | ||
| 298 | |||
| 299 | Flavio Soibelmann Glock <fglock@gmail.com>. | ||
| 300 | |||
| 301 | =head1 COPYRIGHT | ||
| 302 | |||
| 303 | Copyright 2016 by Flavio Soibelmann Glock. | ||
| 304 | |||
| 305 | This program is free software; you can redistribute it and/or modify it | ||
| 306 | under the same terms as Perl itself. | ||
| 307 | |||
| 308 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 309 | |||
| 310 | =end |