Skip to content

Commit

Permalink
* Body.pm, CgOp.pm and Decl.pm are now in MooseX::Declare syntax.
Browse files Browse the repository at this point in the history
* Fix a tiny bug: Decl::Class now extends Decl.
  • Loading branch information
Audrey Tang committed Jul 20, 2010
1 parent efce6ed commit 4dc75bb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 211 deletions.
27 changes: 7 additions & 20 deletions Body.pm
@@ -1,13 +1,9 @@
use strict;
use warnings;
use 5.010;
use MooseX::Declare;
use CodeGen ();
use CgOp ();

{
package Body;
use Moose;

class Body {
has name => (isa => 'Str', is => 'rw', default => "anon");
has do => (isa => 'Op', is => 'rw');
has enter => (isa => 'ArrayRef[Op]', is => 'ro',
Expand All @@ -24,9 +20,7 @@ use CgOp ();
# also '' for incorrectly contextualized {p,x,}block, blast
has type => (isa => 'Str', is => 'rw');

sub is_mainline {
my $self = shift;

method is_mainline () {
if ($self->type && $self->type eq 'mainline') {
return 1;
}
Expand All @@ -42,16 +36,14 @@ use CgOp ();
}
}

sub gen_code {
my ($self) = @_;
method gen_code () {
# TODO: Bind a return value here to catch non-ro sub use
CodeGen->new(name => $self->name, body => $self,
ops => CgOp::prog($self->enter_code,
CgOp::return($self->do->code($self))));
}

sub enter_code {
my ($self) = @_;
method enter_code () {
my @p;
push @p, CgOp::lextypes(map { $_, 'Variable' }
keys %{ $self->lexical });
Expand All @@ -61,19 +53,14 @@ use CgOp ();
CgOp::prog(@p);
}

sub write {
my ($self) = @_;
method write () {
$self->code->write;
$_->write($self) for (@{ $self->decls });
}

sub preinit_code {
my ($self) = @_;
method preinit_code () {
CgOp::prog(map { $_->preinit_code($self) } @{ $self->decls });
}

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

1;
72 changes: 12 additions & 60 deletions CgOp.pm
@@ -1,43 +1,22 @@
use 5.010;
use strict;
use warnings;


{
package CgOp;
use Moose;
use MooseX::Declare;

class CgOp {
has zyg => (isa => 'ArrayRef', is => 'ro', default => sub { [] });

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

{
package CgOp::Seq;
use Moose;
extends 'CgOp';

sub var_cg {
my ($self, $cg) = @_;
class CgOp::Seq extends CgOp {
method var_cg ($cg) {
for (@{ $self->zyg }) {
$_->var_cg($cg);
}
}

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

{
package CgOp::Primitive;
use Moose;
extends 'CgOp';

class CgOp::Primitive extends CgOp {
has op => (isa => 'ArrayRef', is => 'ro', required => 1);

sub var_cg {
my ($self, $cg) = @_;
method var_cg ($cg) {
for (@{ $self->zyg }) {
$_->var_cg($cg);
}
Expand All @@ -47,18 +26,10 @@ use warnings;
}
$cg->$c(@o);
}

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

{
package CgOp::Ternary;
use Moose;
extends 'CgOp';

sub var_cg {
my ($self, $cg) = @_;
class CgOp::Ternary extends CgOp {
method var_cg ($cg) {
my ($check, $true, $false) = @{ $self->zyg };
my $l1 = $cg->label;
my $l2 = $cg->label;
Expand All @@ -71,21 +42,13 @@ use warnings;
$false->var_cg($cg);
$cg->labelhere($l2);
}

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

{
package CgOp::While;
use Moose;
extends 'CgOp';

class CgOp::While extends CgOp {
has once => (is => 'ro', isa => 'Bool');
has until => (is => 'ro', isa => 'Bool');

sub var_cg {
my ($self, $cg) = @_;
method var_cg ($cg) {
my ($check, $body) = @{ $self->zyg };
my $lagain = $cg->label;
my $lcheck = $self->once ? 0 : $cg->label;
Expand All @@ -103,21 +66,13 @@ use warnings;
$cg->cgoto($lagain);
}
}

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

{
package CgOp::Let;
use Moose;
extends 'CgOp';

class CgOp::Let extends CgOp {
has var => (is => 'ro', isa => 'Str', required => 1);
has type => (is => 'ro', isa => 'Str', required => 1);

sub var_cg {
my ($self, $cg) = @_;
method var_cg ($cg) {

$cg->lextypes($self->var, $self->type);
$self->zyg->[0]->var_cg($cg);
Expand All @@ -126,9 +81,6 @@ use warnings;
$cg->push_null($self->type);
$cg->rawlexput($self->var, 0);
}

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

# just a bunch of smart constructors
Expand Down

0 comments on commit 4dc75bb

Please sign in to comment.