Skip to content

Commit

Permalink
[mm] Implement state variables
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Sep 19, 2010
1 parent 51c12fe commit 26f8301
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/Metamodel.pm
Expand Up @@ -166,6 +166,19 @@ our $global;
__PACKAGE__->meta->make_immutable;
}

# mostly for state
{
package Metamodel::Lexical::Alias;
use Moose;
extends 'Metamodel::Lexical';

has to => (isa => 'Str', is => 'ro', required => 1);
sub BUILDARGS { +{ to => $_[1] } }

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

# sub foo { ... }
{
package Metamodel::Lexical::SubDef;
Expand Down Expand Up @@ -250,14 +263,25 @@ our $global;
}

sub find_lex { my ($self, $name) = @_;
return $self->lexicals->{$name} //
($self->outer ? $self->outer->find_lex($name) : undef);
my $l = $self->lexicals->{$name};
if ($l) {
return $l->isa('Metamodel::Lexical::Alias') ?
$self->find_lex($l->to) : $l;
}
return ($self->outer ? $self->outer->find_lex($name) : undef);
}

sub add_my_name { my ($self, $slot, @ops) = @_;
$self->lexicals->{$slot} = Metamodel::Lexical::Simple->new(@ops);
}

sub add_state_name { my ($self, $slot, $back, @ops) = @_;
# outermost sub isn't cloned so a fallback to my is safe
my $up = $self->outer // $self;
$up->lexicals->{$back} = Metamodel::Lexical::Simple->new(@ops);
$self->lexicals->{$slot} = Metamodel::Lexical::Alias->new($back);
}

sub add_my_stash { my ($self, $slot, $stash) = @_;
$self->lexicals->{$slot} = Metamodel::Lexical::Stash->new(
referent => $stash);
Expand Down Expand Up @@ -354,10 +378,10 @@ sub Op::Lexical::begin {

if ($self->state_backing) {
$opensubs[-1]->add_state_name($self->name, $self->state_backing,
$self->list, $self->hash);
list => $self->list, hash => $self->hash);
} elsif ($self->declaring) {
$opensubs[-1]->add_my_name($self->name, $self->list,
$self->hash);
$opensubs[-1]->add_my_name($self->name, list => $self->list,
hash =>$self->hash);
}
}

Expand Down

0 comments on commit 26f8301

Please sign in to comment.