| 1 | use v5; | ||
| 2 | |||
| 3 | |||
| 4 | package Perlito5::AST::CompUnit; | ||
| 5 | sub new { | ||
| 6 | my $class = shift; | ||
| 7 | my %args = @_; | ||
| 8 | if ($args{body}) { | ||
| 9 | my @body; | ||
| 10 | for my $stmt ( @{ $args{body} } ) { | ||
| 11 | next if !defined($stmt); | ||
| 12 | next if ref($stmt) eq 'Perlito5::AST::Apply' && $stmt->{namespace} eq 'Perlito5' && $stmt->{code} eq 'nop'; | ||
| 13 | push @body, $stmt; | ||
| 14 | } | ||
| 15 | @body = Perlito5::Macro::split_code_too_large(@body) | ||
| 16 | if $Perlito5::CODE_TOO_LARGE; | ||
| 17 | $args{body} = \@body; | ||
| 18 | } | ||
| 19 | bless \%args, $class; | ||
| 20 | } | ||
| 21 | sub name { $_[0]->{name} } | ||
| 22 | sub body { $_[0]->{body} } | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | package Perlito5::AST::Block; | ||
| 27 | sub new { | ||
| 28 | my $class = shift; | ||
| 29 | my %args = @_; | ||
| 30 | if ($args{stmts}) { | ||
| 31 | my @stmts; | ||
| 32 | for my $stmt ( @{ $args{stmts} } ) { | ||
| 33 | next if !defined($stmt); | ||
| 34 | next if ref($stmt) eq 'Perlito5::AST::Apply' && $stmt->{namespace} eq 'Perlito5' && $stmt->{code} eq 'nop'; | ||
| 35 | push @stmts, $stmt; | ||
| 36 | } | ||
| 37 | @stmts = Perlito5::Macro::split_code_too_large(@stmts) | ||
| 38 | if $Perlito5::CODE_TOO_LARGE; | ||
| 39 | $args{stmts} = \@stmts; | ||
| 40 | } | ||
| 41 | bless \%args, $class; | ||
| 42 | } | ||
| 43 | sub sig { $_[0]->{sig} } | ||
| 44 | sub stmts { $_[0]->{stmts} } | ||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | package Perlito5::AST::Int; | ||
| 49 | sub new { my $class = shift; bless {@_}, $class } | ||
| 50 | sub int { $_[0]->{int} } | ||
| 51 | sub value { $_[0]->{int} } | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | package Perlito5::AST::Num; | ||
| 56 | sub new { my $class = shift; bless {@_}, $class } | ||
| 57 | sub num { $_[0]->{num} } | ||
| 58 | sub value { $_[0]->{num} } | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | package Perlito5::AST::Buf; | ||
| 63 | sub new { my $class = shift; bless {@_}, $class } | ||
| 64 | sub buf { $_[0]->{buf} } | ||
| 65 | sub value { $_[0]->{buf} } | ||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | package Perlito5::AST::Index; | ||
| 70 | sub new { my $class = shift; bless {@_}, $class } | ||
| 71 | sub obj { $_[0]->{obj} } | ||
| 72 | sub index_exp { $_[0]->{index_exp} } | ||
| 73 | |||
| 74 | sub INDEX { | ||
| 75 | my ($term, $index) = @_; | ||
| 76 | if (ref($term) eq 'Perlito5::AST::Var' && $term->{sigil} eq '@') { | ||
| 77 | return Perlito5::AST::Index->new( | ||
| 78 | obj => Perlito5::AST::Var->new( %$term, _real_sigil => $term->{sigil}, sigil => '$' ), | ||
| 79 | index_exp => $index, | ||
| 80 | ); | ||
| 81 | } | ||
| 82 | return Perlito5::AST::Call->new( | ||
| 83 | method => 'postcircumfix:<[ ]>', | ||
| 84 | invocant => $term, | ||
| 85 | arguments => $index, | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | package Perlito5::AST::Lookup; | ||
| 91 | sub new { my $class = shift; bless {@_}, $class } | ||
| 92 | sub obj { $_[0]->{obj} } | ||
| 93 | sub index_exp { $_[0]->{index_exp} } | ||
| 94 | |||
| 95 | sub autoquote { | ||
| 96 | my $self = shift; | ||
| 97 | my $index = shift; | ||
| 98 | |||
| 99 | # ok ' sub x () { 123 } $v{x()} = 12; use Data::Dumper; print Dumper \%v ' # '123' => 12 | ||
| 100 | # ok ' sub x () { 123 } $v{x} = 12; use Data::Dumper; print Dumper \%v ' # 'x' => 12 | ||
| 101 | # TODO ' sub x () { 123 } $v{main::x} = 12; use Data::Dumper; print Dumper \%v ' # '123' => 12 | ||
| 102 | # ok ' $v{main::x} = 12; use Data::Dumper; print Dumper \%v ' # 'main::x' => 12 | ||
| 103 | |||
| 104 | if ((ref($index) eq 'Perlito5::AST::Apply') | ||
| 105 | && $index->{bareword} | ||
| 106 | ) | ||
| 107 | { | ||
| 108 | my $full_name = ($index->{namespace} ? $index->{namespace} . '::' : "") . $index->{code}; | ||
| 109 | if ( !exists $Perlito5::PROTO->{$full_name} ) { | ||
| 110 | return Perlito5::AST::Buf->new( buf => $full_name ); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | elsif ( (ref($index) eq 'Perlito5::AST::Apply') | ||
| 114 | && ($index->code eq 'prefix:<->' || $index->code eq 'prefix:<+>') | ||
| 115 | ) | ||
| 116 | { | ||
| 117 | my $arg = $index->arguments->[0]; | ||
| 118 | return Perlito5::AST::Apply->new( | ||
| 119 | code => $index->code, | ||
| 120 | namespace => $index->namespace, | ||
| 121 | arguments => [ $self->autoquote($arg) ], | ||
| 122 | ) | ||
| 123 | if $arg; | ||
| 124 | } | ||
| 125 | elsif ( (ref($index) eq 'Perlito5::AST::Apply') | ||
| 126 | && ($index->code eq 'list:<,>') | ||
| 127 | ) | ||
| 128 | { | ||
| 129 | |||
| 130 | my $obj = $self->obj; | ||
| 131 | if ($obj->{sigil} eq '@') { | ||
| 132 | # @v{ $a, $b, $c } | ||
| 133 | return $index; | ||
| 134 | } | ||
| 135 | |||
| 136 | # $v{ $a, $b, $c } | ||
| 137 | my $args = $index->arguments; | ||
| 138 | return Perlito5::AST::Apply->new( | ||
| 139 | code => 'join', | ||
| 140 | namespace => '', | ||
| 141 | arguments => [ | ||
| 142 | Perlito5::AST::Var->new( name => ';', namespace => '', sigil => '$' ), | ||
| 143 | map { defined($_) ? $_ | ||
| 144 | : Perlito5::AST::Buf->new( buf => '' ) | ||
| 145 | } | ||
| 146 | @$args | ||
| 147 | ], | ||
| 148 | ); | ||
| 149 | } | ||
| 150 | |||
| 151 | $index; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub LOOKUP { | ||
| 155 | my ($term, $index) = @_; | ||
| 156 | if (ref($term) eq 'Perlito5::AST::Var' && $term->{sigil} eq '%') { | ||
| 157 | return Perlito5::AST::Lookup->new( | ||
| 158 | obj => Perlito5::AST::Var->new( %$term, _real_sigil => $term->{sigil}, sigil => '$' ), | ||
| 159 | index_exp => $index, | ||
| 160 | ); | ||
| 161 | } | ||
| 162 | return Perlito5::AST::Call->new( | ||
| 163 | method => 'postcircumfix:<{ }>', | ||
| 164 | invocant => $term, | ||
| 165 | arguments => $index, | ||
| 166 | ); | ||
| 167 | } | ||
| 168 | |||
| 169 | package Perlito5::AST::Var; | ||
| 170 | sub new { | ||
| 171 | my ($class, %args) = @_; | ||
| 172 | my $var = bless \%args, $class; | ||
| 173 | push @Perlito5::SCOPE_STMT, $var; | ||
| 174 | return $var; | ||
| 175 | } | ||
| 176 | sub sigil { $_[0]->{sigil} } | ||
| 177 | sub namespace { $_[0]->{namespace} } | ||
| 178 | sub name { $_[0]->{name} } | ||
| 179 | |||
| 180 | sub clone { | ||
| 181 | my $self = shift; | ||
| 182 | return bless { %$self }, ref($self); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub plain_name { | ||
| 186 | my $self = shift; | ||
| 187 | if ($self->namespace) { | ||
| 188 | return $self->namespace . '::' . $self->name | ||
| 189 | } | ||
| 190 | return $self->name | ||
| 191 | } | ||
| 192 | |||
| 193 | our %Special_var = ( | ||
| 194 | ARGV => 1, | ||
| 195 | INC => 1, | ||
| 196 | ENV => 1, | ||
| 197 | SIG => 1, | ||
| 198 | _ => 1, | ||
| 199 | ); | ||
| 200 | our %NonSpecial_var = map { $_ => 1 } ( | ||
| 201 | 'A'..'Z', '_', 'a'..'z' | ||
| 202 | ); | ||
| 203 | sub is_special_var { | ||
| 204 | # $1 %ENV $_ $/ | ||
| 205 | my $self = shift; | ||
| 206 | my $c = substr($self->{name}, 0, 1); | ||
| 207 | if ( $Special_var{ $self->{name} } || !$NonSpecial_var{$c} ) { | ||
| 208 | return 1; | ||
| 209 | } | ||
| 210 | 0; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub SCALAR_ARG { | ||
| 214 | Perlito5::AST::Var->new( | ||
| 215 | sigil => '$', | ||
| 216 | namespace => '', | ||
| 217 | name => '_', | ||
| 218 | '_decl' => 'global', | ||
| 219 | '_namespace' => 'main', | ||
| 220 | ); | ||
| 221 | } | ||
| 222 | |||
| 223 | sub LIST_ARG { | ||
| 224 | Perlito5::AST::Var->new( | ||
| 225 | sigil => '@', | ||
| 226 | namespace => '', | ||
| 227 | name => '_', | ||
| 228 | '_decl' => 'global', | ||
| 229 | '_namespace' => 'main', | ||
| 230 | ); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub LIST_ARG_INDEX { | ||
| 234 | my $index = shift; | ||
| 235 | Perlito5::AST::Index::INDEX( LIST_ARG(), Perlito5::AST::Int->new( int => $index ) ); | ||
| 236 | } | ||
| 237 | |||
| 238 | package Perlito5::AST::Call; | ||
| 239 | sub new { my $class = shift; bless {@_}, $class } | ||
| 240 | sub invocant { $_[0]->{invocant} } | ||
| 241 | sub method { $_[0]->{method} } | ||
| 242 | sub arguments { $_[0]->{arguments} } | ||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | package Perlito5::AST::Apply; | ||
| 247 | sub new { my $class = shift; bless {@_}, $class } | ||
| 248 | sub code { $_[0]->{code} } # print | ||
| 249 | sub special_arg { $_[0]->{special_arg} } # STDOUT | ||
| 250 | sub arguments { $_[0]->{arguments} } # 1,2,3 | ||
| 251 | sub namespace { $_[0]->{namespace} } # CORE | ||
| 252 | # ignore_proto # &mysub | ||
| 253 | |||
| 254 | sub PUSH { | ||
| 255 | my ($var, $value) = @_; | ||
| 256 | if (ref($var) eq 'Perlito5::AST::Var' && $var->{sigil} eq '@') { | ||
| 257 | return Perlito5::AST::Apply->new( | ||
| 258 | code => 'push', | ||
| 259 | arguments => [$var, $value], | ||
| 260 | ); | ||
| 261 | } | ||
| 262 | return Perlito5::AST::Apply->new( | ||
| 263 | code => 'push', | ||
| 264 | arguments => [ | ||
| 265 | Perlito5::AST::Apply->new( | ||
| 266 | code => 'prefix:<@>', | ||
| 267 | arguments => [$var], | ||
| 268 | ), | ||
| 269 | $value, | ||
| 270 | ], | ||
| 271 | ); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub UNDEF { | ||
| 275 | return Perlito5::AST::Apply->new( | ||
| 276 | 'arguments' => [], | ||
| 277 | 'code' => 'undef', | ||
| 278 | 'namespace' => '', | ||
| 279 | ); | ||
| 280 | } | ||
| 281 | |||
| 282 | sub value { | ||
| 283 | my $self = shift; | ||
| 284 | my $ref = ref($self); | ||
| 285 | return undef if $ref eq 'Perlito5::AST::Apply' && $self->{code} eq 'undef'; | ||
| 286 | die "can't take the Constant value() of Perlito5::AST::Apply '" . $self->{code} . "'"; | ||
| 287 | } | ||
| 288 | |||
| 289 | package Perlito5::AST::If; | ||
| 290 | sub new { my $class = shift; bless {@_}, $class } | ||
| 291 | sub cond { $_[0]->{cond} } | ||
| 292 | sub body { $_[0]->{body} } | ||
| 293 | sub otherwise { $_[0]->{otherwise} } | ||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | package Perlito5::AST::When; | ||
| 298 | sub new { my $class = shift; bless {@_}, $class } | ||
| 299 | sub cond { $_[0]->{cond} } | ||
| 300 | sub body { $_[0]->{body} } | ||
| 301 | |||
| 302 | |||
| 303 | |||
| 304 | package Perlito5::AST::While; | ||
| 305 | sub new { my $class = shift; bless {@_}, $class } | ||
| 306 | sub init { $_[0]->{init} } | ||
| 307 | sub cond { $_[0]->{cond} } | ||
| 308 | sub continue { $_[0]->{continue} } | ||
| 309 | sub body { $_[0]->{body} } | ||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | package Perlito5::AST::For; | ||
| 314 | sub new { my $class = shift; bless {@_}, $class } | ||
| 315 | sub cond { $_[0]->{cond} } | ||
| 316 | sub continue { $_[0]->{continue} } | ||
| 317 | sub body { $_[0]->{body} } | ||
| 318 | sub topic { $_[0]->{topic} } | ||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | package Perlito5::AST::Given; | ||
| 323 | sub new { my $class = shift; bless {@_}, $class } | ||
| 324 | sub cond { $_[0]->{cond} } | ||
| 325 | sub body { $_[0]->{body} } | ||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | package Perlito5::AST::Decl; | ||
| 330 | sub new { my $class = shift; bless {@_}, $class } | ||
| 331 | sub decl { $_[0]->{decl} } | ||
| 332 | sub type { $_[0]->{type} } | ||
| 333 | sub var { $_[0]->{var} } | ||
| 334 | sub attributes { $_[0]->{attributes} } | ||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | package Perlito5::AST::Sub; | ||
| 339 | sub new { my $class = shift; bless {@_}, $class } | ||
| 340 | sub name { $_[0]->{name} } | ||
| 341 | sub sig { $_[0]->{sig} } | ||
| 342 | sub block { $_[0]->{block} } | ||
| 343 | sub attributes { $_[0]->{attributes} } | ||
| 344 | |||
| 345 | sub is_named_sub { | ||
| 346 | my $self = shift; | ||
| 347 | (ref($self) eq 'Perlito5::AST::Sub') && $self->{name} | ||
| 348 | } | ||
| 349 | sub is_anon_sub { | ||
| 350 | my $self = shift; | ||
| 351 | (ref($self) eq 'Perlito5::AST::Sub') && !$self->{name} | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | 1; | ||
| 356 | |||
| 357 | =begin | ||
| 358 | |||
| 359 | =head1 NAME | ||
| 360 | |||
| 361 | Perlito5::AST - Base class for Perlito Perl5 AST nodes | ||
| 362 | |||
| 363 | =head1 DESCRIPTION | ||
| 364 | |||
| 365 | This module provides AST node class declarations for the Perlito Perl 5 compiler. | ||
| 366 | |||
| 367 | =head1 AUTHORS | ||
| 368 | |||
| 369 | Flavio Soibelmann Glock <fglock@gmail.com>. | ||
| 370 | |||
| 371 | =head1 COPYRIGHT | ||
| 372 | |||
| 373 | Copyright 2011, 2012 by Flavio Soibelmann Glock and others. | ||
| 374 | |||
| 375 | This program is free software; you can redistribute it and/or modify it | ||
| 376 | under the same terms as Perl itself. | ||
| 377 | |||
| 378 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 379 | |||
| 380 | =end |