Skip to content

Commit

Permalink
Correct our hanlding of package blocks somewhat. They're now immediat…
Browse files Browse the repository at this point in the history
…e blocks, as per spec. This does, along the way, also fix the lexicals and classes bugs.
  • Loading branch information
jnthn committed Oct 19, 2009
1 parent 7c28f95 commit e0c6910
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
51 changes: 37 additions & 14 deletions src/parser/actions.pm
Expand Up @@ -869,13 +869,19 @@ method method_def($/) {
our $?METACLASS;
our @?BLOCK;
$block.pirflags(~$block.pirflags() ~ ' :anon ');
@?BLOCK[0][0].push(PAST::Op.new(
my $add_meta := PAST::Op.new(
:pasttype('call'),
:name('!add_metaclass_method'),
$?METACLASS,
$block.name,
PAST::Op.new( :inline(' .const "Sub" %r = "' ~ $block.subid ~ '"') )
));
);
if @?BLOCK[0]<pkgdecl> eq 'role' || @?BLOCK[0]<anon> {
@?BLOCK[0][0].push($add_meta);
}
else {
@?BLOCK[0].loadinit().push($add_meta);
}
}

make $block;
Expand Down Expand Up @@ -1511,7 +1517,8 @@ method package_declarator($/, $key) {
unless $pkgdecl eq 'class' || $pkgdecl eq 'role' || $pkgdecl eq 'grammar' {
$/.panic("Cannot use does package declarator outside of class, role, or grammar");
}
$block[0].push(PAST::Op.new(
my $pushee := $pkgdecl eq 'role' ?? $block[0] !! $block.loadinit();
$pushee.push(PAST::Op.new(
:name('trait_mod:does'),
$?METACLASS,
$<typename>.ast
Expand Down Expand Up @@ -1541,6 +1548,9 @@ method package_def($/, $key) {
# Also attach traits to the node.
our $?BLOCK_OPEN;
$?BLOCK_OPEN<traits> := $<trait>;
if $add eq '' {
$?BLOCK_OPEN<anon> := 1;
}

return 0;
}
Expand All @@ -1549,7 +1559,6 @@ method package_def($/, $key) {
}

my $block := $/{$key}.ast;
$block.lexical(0);
declare_implicit_routine_vars($block);

my $modulename;
Expand Down Expand Up @@ -1592,9 +1601,8 @@ method package_def($/, $key) {
$block[0].push(bind_signature_op());
}
elsif $key eq 'block' {
# A normal block acts like a BEGIN and is executed ASAP.
$block.blocktype('declaration');
$block.pirflags(':load :init');
# A normal block runs inline.
$block.blocktype('immediate');
}
elsif $key eq 'statement_block' {
# file-level blocks have their contents as the compunit mainline
Expand All @@ -1609,9 +1617,15 @@ method package_def($/, $key) {
}

# Create a node at the beginning of the block's initializer
# for package initializations
# for package initializations if it's a role, or loadinit if
# it's anything else.
my $init := PAST::Stmts.new();
$block[0].unshift( $init );
if $?PKGDECL eq 'role' || $is_anon {
$block[0].unshift( $init );
}
else {
$block.loadinit().unshift( $init );
}

# Set is also flag.
$block<isalso> := has_compiler_trait_with_val($<trait>, 'trait_mod:is', 'also');
Expand Down Expand Up @@ -1671,13 +1685,12 @@ method package_def($/, $key) {
));
$block.push(PAST::Var.new(:name('proto_store'), :scope('register')));
$block.blocktype('immediate');
$block.pirflags('');
}
elsif !$block<isalso> {
$block[0].push( PAST::Op.new( :name('!meta_compose'), $?METACLASS) );
$block.loadinit().push( PAST::Op.new( :name('!meta_compose'), $?METACLASS) );
}
else {
$block[0].push( PAST::Op.new( :name('!setup_invoke_vtable'), $?METACLASS) );
$block.loadinit().push( PAST::Op.new( :name('!setup_invoke_vtable'), $?METACLASS) );
}

make $block;
Expand Down Expand Up @@ -1829,7 +1842,12 @@ method scope_declarator($/) {
$has.push($trait_block);
}
}
$block[0].push( $has );
if $pkgdecl eq 'role' || $block<anon> {
$block[0].push( $has );
}
else {
$block.loadinit().push( $has );
}
}
else {
# $scope eq 'package' | 'lexical' | 'state'
Expand Down Expand Up @@ -2677,7 +2695,12 @@ method EXPR($/, $key) {
$?METACLASS, $lhs[0].name(), $rhs
);
our @?BLOCK;
@?BLOCK[0][0].push($past);
if @?BLOCK[0]<pkgdecl> eq 'role' || @?BLOCK[0]<anon> {
@?BLOCK[0][0].push($past);
}
else {
@?BLOCK[0].loadinit().push($past);
}
$past := PAST::Stmts.new();
}
elsif $lhs<scopedecl> eq 'constant' {
Expand Down
2 changes: 1 addition & 1 deletion src/setting/Num.pm
Expand Up @@ -250,7 +250,7 @@ class Num is also {
~self
}
my sub _modf($num) { my $q = $num.Int; $num - $q, $q; }
sub _modf($num) { my $q = $num.Int; $num - $q, $q; }
multi method Rat($epsilon = 1.0e-6) {
my $num = +self;
Expand Down
3 changes: 2 additions & 1 deletion src/setting/Rat.pm
Expand Up @@ -2,7 +2,8 @@ class Rat {
has $.numerator;
has $.denominator;

my sub gcd(Int $a is copy, Int $b is copy) {
# XXX TODO: should be lexical sub, but can't do those subs in setting yet.
sub gcd(Int $a is copy, Int $b is copy) {
$a = -$a if ($a < 0);
$b = -$b if ($b < 0);
while $a > 0 && $b > 0 {
Expand Down

0 comments on commit e0c6910

Please sign in to comment.