Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[mm] implement my class {}
  • Loading branch information
sorear committed Sep 18, 2010
1 parent eb45fca commit 85f1192
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions src/Metamodel.pm
Expand Up @@ -33,11 +33,13 @@ use YAML::XS;
# We don't handle normal variables here, those exist only in the runtime
# package tree.
{
package Metamodel::Packageoid;
package Metamodel::Stash;
use Moose;

has zyg => (isa => 'HashRef[Metamodel::Packageoid]', is => 'ro',
has zyg => (isa => 'HashRef[Metamodel::Stash]', is => 'ro',
default => sub { +{} });
# undef here -> stub like my class Foo { ... }
has obj => (isa => 'Maybe[Metamodel::Package]', is => 'rw');

no Moose;
__PACKAGE__->meta->make_immutable;
Expand All @@ -46,7 +48,9 @@ use YAML::XS;
{
package Metamodel::Package;
use Moose;
extends 'Metamodel::Packageoid';

# an intrinsic name, even if anonymous
has name => (isa => 'Str', is => 'ro', default => 'ANON');

no Moose;
__PACKAGE__->meta->make_immutable;
Expand All @@ -55,7 +59,7 @@ use YAML::XS;
{
package Metamodel::Module;
use Moose;
extends 'Metamodel::Packageoid';
extends 'Metamodel::Package';

no Moose;
__PACKAGE__->meta->make_immutable;
Expand All @@ -64,7 +68,7 @@ use YAML::XS;
{
package Metamodel::Class;
use Moose;
extends 'Metamodel::Packageoid';
extends 'Metamodel::Module';

has attributes => (isa => 'ArrayRef[Str]', is => 'ro',
default => sub { [] });
Expand Down Expand Up @@ -116,7 +120,6 @@ use YAML::XS;
extends 'Metamodel::Lexical';

has body => (isa => 'Metamodel::StaticSub', is => 'ro');
has name => (isa => 'Str', is => 'ro');

no Moose;
__PACKAGE__->meta->make_immutable;
Expand All @@ -125,12 +128,11 @@ use YAML::XS;
# my class Foo { } or our class Foo { }; the difference is whether some
# package also holds a ref
{
package Metamodel::Lexical::Packageoid;
package Metamodel::Lexical::Stash;
use Moose;
extends 'Metamodel::Lexical';

has body => (isa => 'Metamodel::Packageoid', is => 'ro');
has name => (isa => 'Str', is => 'ro');
has referent => (isa => 'Metamodel::Stash', is => 'ro');

no Moose;
__PACKAGE__->meta->make_immutable;
Expand Down Expand Up @@ -161,11 +163,23 @@ use YAML::XS;
has initq => (isa => 'ArrayRef[Metamodel::StaticSub]', is => 'ro',
default => sub { [] });

has body_of => (isa => 'Maybe[Metamodel::Stash]', is => 'ro');

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

sub add_my_stash { my ($self, $slot, $stash) = @_;
$self->lexicals->{$slot} = Metamodel::Lexical::Stash->new(
referent => $stash);
}

sub add_my_sub { my ($self, $slot, $body) = @_;
$self->lexicals->{$slot} = Metamodel::Lexical::SubDef->new(
body => $body);
}

sub close { }

no Moose;
Expand All @@ -187,12 +201,15 @@ sub Unit::begin {

sub Body::begin {
my $self = shift;
my %args = @_;

my $top = @opensubs ? $opensubs[-1] : undef;

push @opensubs, Metamodel::StaticSub->new(
outer => $top,
outer => $top,
body_of => $args{body_of},
run_once => !defined($top));

$self->do->begin;

$opensubs[-1]->close;
Expand All @@ -217,6 +234,23 @@ sub Op::Lexical::begin {
}
}

sub Op::PackageDef::begin {
my $self = shift;
my $pclass = ref($self);
$pclass =~ s/Op::(.*)Def/Metamodel::$1/;

my $ns = Metamodel::Stash->new;
# XXX handle exports, ourpkg
$opensubs[-1]->add_my_stash($self->var, $ns);

if (!$self->stub) {
my $obj = $pclass->new(name => $self->name);
$ns->obj($obj);
my $body = $self->body->begin(body_of => $ns);
$opensubs[-1]->add_my_sub($self->bodyvar, $body);
}
}

### Code goes here to generate C# from the metamodel
#

Expand Down

0 comments on commit 85f1192

Please sign in to comment.