Skip to content

Commit

Permalink
Add direct support for sub types
Browse files Browse the repository at this point in the history
-10K testsuite; reward will be greater when more types than Sub and Regex exist.
  • Loading branch information
sorear committed Aug 17, 2010
1 parent eba4446 commit 8e00559
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
4 changes: 4 additions & 0 deletions Body.pm
Expand Up @@ -29,6 +29,10 @@ use CgOp ();
has file => (isa => 'Str', is => 'ro');
has text => (isa => 'Str', is => 'ro');

# metadata for runtime inspection
has class => (isa => 'Str', is => 'rw', default => 'Sub');
has ltm => (is => 'rw');

sub is_mainline { $_[0]->scopetree->{'?is_mainline'} }

sub extract_scopes {
Expand Down
8 changes: 4 additions & 4 deletions CgOp.pm
Expand Up @@ -578,14 +578,14 @@ use warnings;

# must only be called during to_cgop phase!
sub protosub {
my ($body, $ltm) = @_;
my ($body) = @_;
prog(
CgOp::Primitive->new(op => [ 'open_protopad', $body ]),
$body->to_cgop,
(!$ltm ? () : (
(!$body->ltm ? () : (
CgOp::Primitive->new(op => [ 'set_ltm', $body->csname ],
zyg => [ $ltm ]))),
CgOp::Primitive->new(op => [ 'close_sub', $body ]));
zyg => [ $body->ltm ]))),
CgOp::Primitive->new(op => [ 'close_sub', $body, ($body->class ne 'Sub') ], zyg => ($body->class eq 'Sub' ? [] : [ fetch(scopedlex($body->class)) ])))
}

sub ann {
Expand Down
6 changes: 4 additions & 2 deletions CodeGen.pm
Expand Up @@ -607,7 +607,7 @@ use 5.010;
}

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

$self->_emit($body->csname . "_info.PutHint(\"?file\", " . qm($body->file) . ")") if $body->type eq 'mainline';
my $ob = $self->bodies->[-2];
Expand All @@ -619,8 +619,10 @@ use 5.010;
pop @{ $self->bodies };
$self->peek_let('protopad');
my ($pp, $op) = $self->_popn(2);
my ($cl) = $self->_popn(1) if $withclass;
$self->_emit($body->csname . "_info.proto = $pp");
$self->_push('IP6', "Kernel.MakeSub(" . $body->csname . "_info, $op)");
$self->_push('IP6', "Kernel.MakeSub(" . $body->csname . "_info, $op" .
($withclass ? ", $cl)" : ")"));
}

sub proto_var {
Expand Down
13 changes: 2 additions & 11 deletions Decl.pm
Expand Up @@ -52,9 +52,7 @@ use CgOp;
extends 'Decl';

has var => (isa => 'Str', is => 'ro', required => 1);
has class => (isa => 'Str', is => 'ro', default => 'Sub');
has code => (isa => 'Body', is => 'ro', required => 1);
has ltm => (isa => 'Maybe[CgOp]', is => 'ro', default => undef);

sub bodies { $_[0]->code }

Expand All @@ -65,15 +63,8 @@ use CgOp;
sub preinit_code {
my ($self, $body) = @_;

if ($self->class eq 'Sub') {
CgOp::proto_var($self->var, CgOp::newscalar(
CgOp::protosub($self->code)));
} else {
CgOp::proto_var($self->var,
CgOp::methodcall(
CgOp::scopedlex($self->class), 'bless',
CgOp::newscalar(CgOp::protosub($self->code, $self->ltm))));
}
CgOp::proto_var($self->var, CgOp::newscalar(
CgOp::protosub($self->code)));
}

sub enter_code {
Expand Down
7 changes: 7 additions & 0 deletions Kernel.cs
Expand Up @@ -612,6 +612,13 @@ public class Kernel {
return n;
}

public static IP6 MakeSub(SubInfo info, Frame outer, IP6 proto) {
DynObject n = new DynObject(((DynObject)proto).klass);
n.slots["outer"] = outer;
n.slots["info"] = info;
return n;
}

public static Variable BoxAny(object v, IP6 proto) {
DynObject n = new DynObject(((DynObject)proto).klass);
n.slots["value"] = v;
Expand Down
7 changes: 4 additions & 3 deletions Niecza/Actions.pm
Expand Up @@ -360,12 +360,13 @@ sub regex_def { my ($cl, $M) = @_;

my ($cn, $op) = $ast->term_rx;
$M->{_ast} = Op::SubDef->new(
var => $var, class => 'Regex',
var => $var,
method_too => ($scope eq 'has' ? $name : undef),
proto_too => ($scope eq 'has' ? $unsymtext : undef),
ltm => $ast->lad,
body => Body->new(
type => 'regex',
ltm => $ast->lad,
class => 'Regex',
type => 'regex',
signature => $sig->for_regex($cn),
do => $op));
}
Expand Down
5 changes: 1 addition & 4 deletions Op.pm
Expand Up @@ -784,8 +784,6 @@ use CgOp;

has var => (isa => 'Str', is => 'ro', required => 1);
has body => (isa => 'Body', is => 'ro', required => 1);
has class => (isa => 'Str', is => 'ro', default => 'Sub');
has ltm => (isa => 'Maybe[CgOp]', is => 'ro', default => undef);
has method_too => (isa => 'Maybe[Str]', is => 'ro', required => 0);
has proto_too => (isa => 'Maybe[Str]', is => 'ro', required => 0);
has exports => (isa => 'ArrayRef[Str]', is => 'ro', default => sub { [] });
Expand All @@ -796,8 +794,7 @@ use CgOp;
sub lift_decls {
my ($self) = @_;
my @r;
push @r, Decl::Sub->new(var => $self->var, class => $self->class,
code => $self->body, ltm => $self->ltm);
push @r, Decl::Sub->new(var => $self->var, code => $self->body);
push @r, Decl::HasMethod->new(name => $self->method_too,
var => $self->var) if defined($self->method_too);
push @r, Decl::HasMultiRx->new(name => $self->proto_too,
Expand Down
7 changes: 4 additions & 3 deletions RxOp.pm
Expand Up @@ -17,9 +17,10 @@ use CgOp;

sub _close {
my ($self, $type, $parms, $op) = @_;
Op::SubDef->new(var => Niecza::Actions->gensym, class => ucfirst($type),
Op::SubDef->new(var => Niecza::Actions->gensym,
once => 1, body => Body->new(
type => $type,
class => ucfirst($type),
signature => Sig->simple(@$parms),
do => $op));
}
Expand Down Expand Up @@ -123,7 +124,7 @@ use CgOp;
invocant => Op::Lexical->new(name => $lpn),
positionals => [Op::Lexical->new(name => $zzcn)]));
$cn, Op::CallSub->new(
invocant => Op::SubDef->new(var => $lpn, class => 'Sub',
invocant => Op::SubDef->new(var => $lpn,
once => 1, body =>
Body->new(type => 'sub', signature => Sig->simple($zcn), do =>
Op::StatementList->new(children => [ $zcont,
Expand All @@ -145,7 +146,7 @@ use CgOp;
$cont
]));
$cn, Op::CallSub->new(
invocant => Op::SubDef->new(var => $lpn, class => 'Sub', once => 1,
invocant => Op::SubDef->new(var => $lpn, once => 1,
body => Body->new(type => 'sub', signature =>
Sig->simple($zcn), do => $zcont)),
positionals => [Op::Lexical->new(name => $cn)]);
Expand Down

0 comments on commit 8e00559

Please sign in to comment.