Skip to content

Commit

Permalink
Implement "our" variables
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jul 22, 2010
1 parent 3bf633b commit d4c0960
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
35 changes: 32 additions & 3 deletions Decl.pm
Expand Up @@ -106,8 +106,9 @@ use CgOp;
use Moose;
extends 'Decl';

has slot => (isa => 'Str', is => 'ro', required => 1);
has list => (isa => 'Bool', is => 'ro', default => 0);
has slot => (isa => 'Str', is => 'ro', required => 1);
has list => (isa => 'Bool', is => 'ro', default => 0);
has shared => (isa => 'Bool', is => 'ro', default => 0);

sub used_slots {
$_[0]->slot, 'Variable';
Expand All @@ -128,7 +129,7 @@ use CgOp;
sub enter_code {
my ($self, $body) = @_;

$body->mainline ?
($body->mainline || $self->shared) ?
CgOp::share_lex($self->slot) :
CgOp::copy_lex($self->slot, $self->list);
}
Expand All @@ -141,6 +142,34 @@ use CgOp;
no Moose;
}

{
package Decl::PackageAlias;
use Moose;
extends 'Decl';

has slot => (isa => 'Str', is => 'ro', required => 1);

sub used_slots { }

sub preinit_code {
my ($self, $body) = @_;

CgOp::setindex($self->slot,
CgOp::unwrap('Dictionary<string,Variable>',
CgOp::fetch(CgOp::letvar('pkg'))),
CgOp::scopedlex($self->slot));
}

sub enter_code { }

sub write {
my ($self, $body) = @_;
}

__PACKAGE__->meta->make_immutable;
no Moose;
}

{
package Decl::StateVar;
use Moose;
Expand Down
5 changes: 3 additions & 2 deletions Niecza/Actions.pm
Expand Up @@ -787,7 +787,7 @@ sub variable_declarator { my ($cl, $M) = @_;
return;
}

if ($scope eq 'has' || $scope eq 'our') {
if ($scope eq 'has') {
$M->sorry("Unsupported scope $scope for simple variable");
return;
}
Expand All @@ -802,7 +802,8 @@ sub variable_declarator { my ($cl, $M) = @_;
list => scalar ($M->{variable}->Str =~ /^\@/));
} else {
$M->{_ast} = Op::Lexical->new(name => $slot, declaring => 1,
list => scalar ($M->{variable}->Str =~ /^\@/));
list => scalar ($M->{variable}->Str =~ /^\@/),
package_too => ($scope eq 'our'));
}
}

Expand Down
21 changes: 14 additions & 7 deletions Op.pm
Expand Up @@ -562,16 +562,23 @@ use CgOp;
has list => (isa => 'Bool', is => 'ro');

has state_backing => (isa => 'Str', is => 'ro');
has package_too => (isa => 'Bool', is => 'ro', default => 1);

sub local_decls {
my ($self) = @_;
$self->declaring ?
($self->state_backing ?
(Decl::StateVar->new(slot => $self->name,
backing => $self->state_backing, list => $self->list)) :
(Decl::SimpleVar->new(slot => $self->name,
list => $self->list))) :
();
return () unless $self->declaring;

if ($self->state_backing) {
return Decl::StateVar->new(slot => $self->name,
backing => $self->state_backing, list => $self->list);
} elsif ($self->package_too) {
return Decl::SimpleVar->new(slot => $self->name,
list => $self->list, shared => 1),
Decl::PackageAlias->new(slot => $self->name);
} else {
return Decl::SimpleVar->new(slot => $self->name,
list => $self->list);
}
}

sub paren {
Expand Down

0 comments on commit d4c0960

Please sign in to comment.