Skip to content

Commit 2669862

Browse files
committed
Perlito5 - perl6 - class definition in a block
1 parent 107ff94 commit 2669862

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

perlito5.pl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12217,9 +12217,26 @@ package Perlito5::AST::CompUnit;
1221712217
}
1221812218
sub Perlito5::AST::CompUnit::emit_perl6_program {
1221912219
my $comp_units = $_[0];
12220-
return(map {
12220+
my @body = @{$comp_units};
12221+
my @out;
12222+
my $pkg = {'name' => 'main', 'body' => []};
12223+
for my $stmt (@body) {
12224+
if (ref($stmt) eq 'Perlito5::AST::Apply' && $stmt->{'code'} eq 'package') {
12225+
push(@out, ['stmt' => ['keyword' => 'class'], ['bareword' => $pkg->{'name'}], ['block' => map {
12226+
$_->emit_perl6()
12227+
} @{$pkg->{'body'}}]])
12228+
if @{$pkg->{'body'}};
12229+
$pkg = {'name' => $stmt->{'namespace'}, 'body' => []}
12230+
}
12231+
else {
12232+
push(@{$pkg->{'body'}}, $stmt)
12233+
}
12234+
}
12235+
push(@out, ['stmt' => ['keyword' => 'class'], ['bareword' => $pkg->{'name'}], ['block' => map {
1222112236
$_->emit_perl6()
12222-
} @{$comp_units})
12237+
} @{$pkg->{'body'}}]])
12238+
if @{$pkg->{'body'}};
12239+
return(@out)
1222312240
}
1222412241
}
1222512242
package Perlito5::AST::Val::Int;

src5/lib/Perlito5/Perl6/Emitter.pm

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,26 @@ package Perlito5::AST::CompUnit;
9999
}
100100
sub emit_perl6_program {
101101
my $comp_units = $_[0];
102-
return map { $_->emit_perl6() } @$comp_units;
102+
my @body = @$comp_units;
103+
my @out;
104+
my $pkg = { name => 'main', body => [] };
105+
for my $stmt (@body) {
106+
if (ref($stmt) eq 'Perlito5::AST::Apply' && $stmt->{code} eq 'package') {
107+
push @out, [ stmt => [ keyword => 'class'], [ bareword => $pkg->{name} ],
108+
[ block => map { $_->emit_perl6() } @{ $pkg->{body} } ]
109+
]
110+
if @{ $pkg->{body} };
111+
$pkg = { name => $stmt->{namespace}, body => [] };
112+
}
113+
else {
114+
push @{ $pkg->{body} }, $stmt;
115+
}
116+
}
117+
push @out, [ stmt => [ keyword => 'class'], [ bareword => $pkg->{name} ],
118+
[ block => map { $_->emit_perl6() } @{ $pkg->{body} } ]
119+
]
120+
if @{ $pkg->{body} };
121+
return @out;
103122
}
104123
}
105124

0 commit comments

Comments
 (0)