| 1 | use v5; | ||
| 2 | use Perlito5::AST; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | package Perlito5::AST::CompUnit; | ||
| 6 | { | ||
| 7 | sub emit_compile_time { | ||
| 8 | my $self = $_[0]; | ||
| 9 | return __PACKAGE__->new( | ||
| 10 | %$self, | ||
| 11 | body => [ map { defined($_) && $_->emit_compile_time() } @{$self->{body}} ], | ||
| 12 | ); | ||
| 13 | } | ||
| 14 | } | ||
| 15 | |||
| 16 | package Perlito5::AST::Int; | ||
| 17 | { | ||
| 18 | sub emit_compile_time { return $_[0] } | ||
| 19 | } | ||
| 20 | |||
| 21 | package Perlito5::AST::Num; | ||
| 22 | { | ||
| 23 | sub emit_compile_time { return $_[0] } | ||
| 24 | } | ||
| 25 | |||
| 26 | package Perlito5::AST::Buf; | ||
| 27 | { | ||
| 28 | sub emit_compile_time { return $_[0] } | ||
| 29 | } | ||
| 30 | |||
| 31 | package Perlito5::AST::Block; | ||
| 32 | { | ||
| 33 | sub emit_compile_time { | ||
| 34 | my $self = $_[0]; | ||
| 35 | return __PACKAGE__->new( | ||
| 36 | %$self, | ||
| 37 | stmts => [ map { defined($_) && $_->emit_compile_time() } @{$self->{stmts}} ], | ||
| 38 | ( $self->{continue} | ||
| 39 | ? ( continue => $self->{continue}->emit_compile_time() ) | ||
| 40 | : () ), | ||
| 41 | # continue => [ map { defined($_) && $_->emit_compile_time() } @{$self->{continue}} ], | ||
| 42 | ); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | package Perlito5::AST::Index; | ||
| 47 | { | ||
| 48 | sub emit_compile_time { | ||
| 49 | my $self = $_[0]; | ||
| 50 | return __PACKAGE__->new( | ||
| 51 | %$self, | ||
| 52 | obj => $self->{obj}->emit_compile_time(), | ||
| 53 | index_exp => $self->{index_exp}->emit_compile_time(), | ||
| 54 | ); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | package Perlito5::AST::Lookup; | ||
| 59 | { | ||
| 60 | sub emit_compile_time { | ||
| 61 | my $self = $_[0]; | ||
| 62 | return __PACKAGE__->new( | ||
| 63 | %$self, | ||
| 64 | obj => $self->{obj}->emit_compile_time(), | ||
| 65 | index_exp => $self->{index_exp}->emit_compile_time(), | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | package Perlito5::AST::Var; | ||
| 71 | { | ||
| 72 | sub emit_compile_time { | ||
| 73 | my $self = $_[0]; | ||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | package Perlito5::AST::Call; | ||
| 79 | { | ||
| 80 | sub emit_compile_time { | ||
| 81 | my $self = $_[0]; | ||
| 82 | my $invocant = $self->{invocant}->emit_compile_time(); | ||
| 83 | my $arguments; | ||
| 84 | if ( $self->{method} eq 'postcircumfix:<[ ]>' ) { | ||
| 85 | $arguments = $self->{arguments}->emit_compile_time(); | ||
| 86 | } | ||
| 87 | elsif ( $self->{method} eq 'postcircumfix:<{ }>' ) { | ||
| 88 | $arguments = $self->{arguments}->emit_compile_time(); | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | $arguments = [ map { $_->emit_compile_time() } @{$self->{arguments}} ]; | ||
| 92 | } | ||
| 93 | my $meth = $self->{method}; | ||
| 94 | if ( ref($meth) eq 'Perlito5::AST::Var' ) { | ||
| 95 | $meth = $meth->emit_compile_time(); | ||
| 96 | } | ||
| 97 | return __PACKAGE__->new( | ||
| 98 | %$self, | ||
| 99 | method => $meth, | ||
| 100 | invocant => $invocant, | ||
| 101 | arguments => $arguments, | ||
| 102 | ); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | package Perlito5::AST::Apply; | ||
| 107 | { | ||
| 108 | sub emit_compile_time_args { | ||
| 109 | my $self = $_[0]; | ||
| 110 | return () if !$self->{arguments}; | ||
| 111 | return map { $_->emit_compile_time() } @{$self->{arguments}}; | ||
| 112 | } | ||
| 113 | sub emit_compile_time { | ||
| 114 | my $self = $_[0]; | ||
| 115 | my $code; | ||
| 116 | if (ref $self->{code}) { | ||
| 117 | $code = $self->{code}->emit_compile_time(); | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | $code = $self->{code}; | ||
| 121 | } | ||
| 122 | my $arguments; | ||
| 123 | if (ref $self->{arguments}) { | ||
| 124 | $arguments = [ map { $_->emit_compile_time() } @{$self->{arguments}} ]; | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | $arguments = $self->{arguments}; | ||
| 128 | } | ||
| 129 | my $special_arg; | ||
| 130 | if (ref $self->{special_arg}) { | ||
| 131 | $special_arg = $self->{special_arg}->emit_compile_time(); | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | $special_arg = $self->{special_arg}; | ||
| 135 | } | ||
| 136 | |||
| 137 | # allow this code to generate subs: | ||
| 138 | # $ perl perlito5.pl -Isrc5/lib -Ccompile_time -e ' BEGIN { for my $v ("a" .. "c") { *{$v} = sub { *{$v} = \123; return shift() . $v } } } ' | ||
| 139 | # | ||
| 140 | # add the extra instrumentation code: | ||
| 141 | # *name = sub {...} | ||
| 142 | # to: | ||
| 143 | # Perlito5::Grammar::Scope::compile_time_glob_set() | ||
| 144 | if ($self->{code} eq 'infix:<=>') { | ||
| 145 | # print STDERR "# assign in BEGIN block\n"; | ||
| 146 | my $arg = $self->{arguments}->[0]; | ||
| 147 | if (ref($arg) eq 'Perlito5::AST::Apply' && $arg->{code} eq 'prefix:<*>') { | ||
| 148 | # print STDERR "# set GLOB in BEGIN block\n"; | ||
| 149 | return Perlito5::AST::Apply->new( | ||
| 150 | code => 'compile_time_glob_set', | ||
| 151 | namespace => 'Perlito5::Grammar::Scope', | ||
| 152 | arguments => [ | ||
| 153 | $arg->{arguments}->[0]->emit_compile_time(), | ||
| 154 | $self->{arguments}[1]->emit_compile_time(), | ||
| 155 | Perlito5::AST::Buf->new( buf => $Perlito5::PKG_NAME ), | ||
| 156 | ] | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | elsif (ref($arg) eq 'Perlito5::AST::Var' && $arg->{sigil} eq '*') { | ||
| 160 | return Perlito5::AST::Apply->new( | ||
| 161 | code => 'compile_time_glob_set', | ||
| 162 | namespace => 'Perlito5::Grammar::Scope', | ||
| 163 | arguments => [ | ||
| 164 | Perlito5::AST::Buf->new( buf => ($arg->{namespace} || $arg->{_namespace}) . '::' . $arg->{name} ), | ||
| 165 | $self->{arguments}[1]->emit_compile_time(), | ||
| 166 | Perlito5::AST::Buf->new( buf => $Perlito5::PKG_NAME ), | ||
| 167 | ] | ||
| 168 | ); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | if ($self->{code} eq 'eval') { | ||
| 173 | my $args = $self->{arguments}; | ||
| 174 | if (@$args && !$args->[0]->isa( "Perlito5::AST::Block" )) { | ||
| 175 | # eval-string inside BEGIN block | ||
| 176 | ## TODO | ||
| 177 | ## return Perlito5::AST::Apply->new( | ||
| 178 | ## %$self, | ||
| 179 | ## arguments => [ | ||
| 180 | ## Perlito5::AST::Apply->new( | ||
| 181 | ## code => 'generate_eval_string', | ||
| 182 | ## namespace => 'Perlito5::CompileTime::Dumper', | ||
| 183 | ## arguments => [ | ||
| 184 | ## @$args, | ||
| 185 | ## ], | ||
| 186 | ## ), | ||
| 187 | ## ], | ||
| 188 | ## ); | ||
| 189 | return $self; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | if ( $self->{code} eq 'require' && !$self->{namespace} ) { | ||
| 194 | # use the compile-time "require" command inside BEGIN blocks | ||
| 195 | return Perlito5::AST::Apply->new( | ||
| 196 | %$self, | ||
| 197 | namespace => 'Perlito5::Grammar::Use', | ||
| 198 | ); | ||
| 199 | } | ||
| 200 | |||
| 201 | return __PACKAGE__->new( | ||
| 202 | %$self, | ||
| 203 | code => $code, | ||
| 204 | arguments => $arguments, | ||
| 205 | ( $special_arg ? ( special_arg => $special_arg ) : () ), | ||
| 206 | ); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | package Perlito5::AST::If; | ||
| 211 | { | ||
| 212 | sub emit_compile_time { | ||
| 213 | my $self = $_[0]; | ||
| 214 | return __PACKAGE__->new( | ||
| 215 | %$self, | ||
| 216 | cond => $self->{cond}->emit_compile_time(), | ||
| 217 | body => $self->{body}->emit_compile_time(), | ||
| 218 | otherwise => $self->{otherwise}->emit_compile_time(), | ||
| 219 | ); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | package Perlito5::AST::When; | ||
| 224 | { | ||
| 225 | sub emit_compile_time { | ||
| 226 | my $self = $_[0]; | ||
| 227 | return __PACKAGE__->new( | ||
| 228 | %$self, | ||
| 229 | cond => $self->{cond}->emit_compile_time(), | ||
| 230 | body => $self->{body}->emit_compile_time(), | ||
| 231 | ); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | package Perlito5::AST::While; | ||
| 237 | { | ||
| 238 | sub emit_compile_time { | ||
| 239 | my $self = $_[0]; | ||
| 240 | return __PACKAGE__->new( | ||
| 241 | %$self, | ||
| 242 | cond => $self->{cond}->emit_compile_time(), | ||
| 243 | body => $self->{body}->emit_compile_time(), | ||
| 244 | ); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | package Perlito5::AST::For; | ||
| 249 | { | ||
| 250 | sub emit_compile_time { | ||
| 251 | my $self = $_[0]; | ||
| 252 | my $cond; | ||
| 253 | if (ref($self->{cond}) eq 'ARRAY') { | ||
| 254 | # C-style for | ||
| 255 | $cond = [ map { defined($_) ? $_->emit_compile_time() : $_ } @{$self->{cond}} ]; | ||
| 256 | } | ||
| 257 | else { | ||
| 258 | $cond = $self->{cond}->emit_compile_time(), | ||
| 259 | } | ||
| 260 | return __PACKAGE__->new( | ||
| 261 | %$self, | ||
| 262 | cond => $cond, | ||
| 263 | body => $self->{body}->emit_compile_time(), | ||
| 264 | ( $self->{continue} | ||
| 265 | ? ( continue => $self->{continue}->emit_compile_time() ) | ||
| 266 | : () ), | ||
| 267 | ); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | package Perlito5::AST::Decl; | ||
| 272 | { | ||
| 273 | sub emit_compile_time { | ||
| 274 | my $self = $_[0]; | ||
| 275 | return __PACKAGE__->new( | ||
| 276 | %$self, | ||
| 277 | var => $self->{var}->emit_compile_time(), | ||
| 278 | ); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | package Perlito5::AST::Sub; | ||
| 283 | { | ||
| 284 | sub emit_compile_time { | ||
| 285 | my $self = $_[0]; | ||
| 286 | my @stmts; | ||
| 287 | if (defined $self->{block}) { | ||
| 288 | # this is not a pre-declaration | ||
| 289 | |||
| 290 | if ($self->{name}) { | ||
| 291 | # transform into anonymous sub | ||
| 292 | local $Perlito5::PKG_NAME = $self->{namespace}; | ||
| 293 | return Perlito5::AST::Apply->new( | ||
| 294 | 'code' => 'infix:<=>', | ||
| 295 | 'namespace' => '', | ||
| 296 | 'arguments' => [ | ||
| 297 | Perlito5::AST::Var->new( | ||
| 298 | 'sigil' => '*', | ||
| 299 | '_decl' => 'global', | ||
| 300 | 'namespace' => $self->{namespace}, | ||
| 301 | 'name' => $self->{name}, | ||
| 302 | ), | ||
| 303 | Perlito5::AST::Sub->new( | ||
| 304 | %$self, | ||
| 305 | 'namespace' => undef, | ||
| 306 | 'name' => undef, | ||
| 307 | )->emit_compile_time(), | ||
| 308 | ], | ||
| 309 | ); | ||
| 310 | } | ||
| 311 | |||
| 312 | |||
| 313 | @stmts = @{$self->{block}{stmts}}; | ||
| 314 | @stmts = map { $_->emit_compile_time() } @stmts; | ||
| 315 | |||
| 316 | { | ||
| 317 | # at compile-time only: | ||
| 318 | # we are compiling - maybe inside a BEGIN block | ||
| 319 | # add extra instrumentation code | ||
| 320 | # provide a way to dump this closure | ||
| 321 | |||
| 322 | # get list of captured variables, including inner blocks | ||
| 323 | my @captured; | ||
| 324 | for my $stmt (@{$self->{block}{stmts}}) { | ||
| 325 | push @captured, $stmt->get_captures(); | ||
| 326 | } | ||
| 327 | my %dont_capture = map { $_->{dont} ? ( $_->{dont} => 1 ) : () } @captured; | ||
| 328 | my %capture = map { $_->{dont} ? () | ||
| 329 | : $dont_capture{ $_->{_id} } ? () | ||
| 330 | : ($_->{_decl} eq 'local' || $_->{_decl} eq 'global' || $_->{_decl} eq '') ? () | ||
| 331 | : ( $_->{_id} => $_ ) | ||
| 332 | } @captured; | ||
| 333 | |||
| 334 | # return a hash with { "variable name" => \"variable value" } | ||
| 335 | # with all captured variables | ||
| 336 | # @_ && ref($_[0]) eq "Perlito5::dump" && return { '$x' => \$x } | ||
| 337 | |||
| 338 | |||
| 339 | # save the unprocessed AST for this sub | ||
| 340 | my $code = __PACKAGE__->new( | ||
| 341 | %$self, | ||
| 342 | ); | ||
| 343 | my $id = Perlito5::get_label(); | ||
| 344 | $Perlito5::BEGIN_SUBS{$id} = $code; | ||
| 345 | # warn "BEGIN_SUBS: ", Perlito5::Dumper::Dumper( $code ); | ||
| 346 | $Perlito5::BEGIN_LEXICALS{$_} = $capture{$_} for keys %capture; | ||
| 347 | |||
| 348 | if (!@stmts) { | ||
| 349 | unshift @stmts, | ||
| 350 | Perlito5::AST::Apply->new( | ||
| 351 | code => 'return', | ||
| 352 | arguments => [], | ||
| 353 | ); | ||
| 354 | } | ||
| 355 | |||
| 356 | unshift @stmts, | ||
| 357 | Perlito5::AST::Apply->new( | ||
| 358 | code => 'infix:<&&>', | ||
| 359 | arguments => [ | ||
| 360 | Perlito5::AST::Var::LIST_ARG(), | ||
| 361 | Perlito5::AST::Apply->new( | ||
| 362 | code => 'infix:<&&>', | ||
| 363 | arguments => [ | ||
| 364 | Perlito5::AST::Apply->new( | ||
| 365 | code => 'infix:<eq>', | ||
| 366 | arguments => [ | ||
| 367 | Perlito5::AST::Apply->new( | ||
| 368 | 'arguments' => [ | ||
| 369 | Perlito5::AST::Var::LIST_ARG_INDEX(0), | ||
| 370 | ], | ||
| 371 | 'code' => 'ref', | ||
| 372 | ), | ||
| 373 | Perlito5::AST::Buf->new( 'buf' => 'Perlito5::dump' ), | ||
| 374 | ], | ||
| 375 | ), | ||
| 376 | Perlito5::AST::Apply->new( | ||
| 377 | code => 'return', | ||
| 378 | arguments => [ | ||
| 379 | Perlito5::AST::Apply::->new( | ||
| 380 | code => 'circumfix:<{ }>', | ||
| 381 | arguments => [ | ||
| 382 | Perlito5::AST::Buf->new(buf => '__SUB__'), | ||
| 383 | Perlito5::AST::Buf->new(buf => $id), | ||
| 384 | Perlito5::AST::Buf->new(buf => '__PKG__'), | ||
| 385 | Perlito5::AST::Buf->new(buf => $Perlito5::PKG_NAME), | ||
| 386 | map {( | ||
| 387 | Perlito5::AST::Buf->new( | ||
| 388 | buf => $_, | ||
| 389 | ), | ||
| 390 | Perlito5::AST::Apply->new( | ||
| 391 | code => 'prefix:<\\>', | ||
| 392 | arguments => [ | ||
| 393 | $capture{$_}, | ||
| 394 | ], | ||
| 395 | ), | ||
| 396 | )} sort keys %capture | ||
| 397 | ], | ||
| 398 | ), | ||
| 399 | ], | ||
| 400 | ), | ||
| 401 | ] | ||
| 402 | ), | ||
| 403 | ], | ||
| 404 | ); | ||
| 405 | } | ||
| 406 | |||
| 407 | $self = __PACKAGE__->new( | ||
| 408 | %$self, | ||
| 409 | ( $self->{block} | ||
| 410 | ? ( block => Perlito5::AST::Block->new( | ||
| 411 | %{$self->{block}}, | ||
| 412 | stmts => [ @stmts ], | ||
| 413 | ) ) | ||
| 414 | : () ), | ||
| 415 | ); | ||
| 416 | } | ||
| 417 | |||
| 418 | return $self; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | 1; | ||
| 423 | |||
| 424 | =begin | ||
| 425 | |||
| 426 | =head1 NAME | ||
| 427 | |||
| 428 | Perlito5::AST::CompileTime - Code generator for Perlito5-in-CompileTime | ||
| 429 | |||
| 430 | =head1 SYNOPSIS | ||
| 431 | |||
| 432 | $program->emit_compile_time() # generated CompileTime code | ||
| 433 | |||
| 434 | =head1 DESCRIPTION | ||
| 435 | |||
| 436 | This module generates CompileTime code for the Perlito compiler. | ||
| 437 | CompileTime code is the code that runs in BEGIN blocks. | ||
| 438 | |||
| 439 | =head1 AUTHORS | ||
| 440 | |||
| 441 | Flavio Soibelmann Glock <fglock@gmail.com>. | ||
| 442 | |||
| 443 | =head1 COPYRIGHT | ||
| 444 | |||
| 445 | Copyright 2016 by Flavio Soibelmann Glock. | ||
| 446 | |||
| 447 | This program is free software; you can redistribute it and/or modify it | ||
| 448 | under the same terms as Perl itself. | ||
| 449 | |||
| 450 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 451 | |||
| 452 | =end |