Skip to content

Instantly share code, notes, and snippets.

Created June 26, 2011 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1048058 to your computer and use it in GitHub Desktop.
Save anonymous/1048058 to your computer and use it in GitHub Desktop.
sub whatever_curry($/, $past, $upto_arity) {
my $curried := $past.isa(PAST::Op)
&& ($past<pasttype> ne 'call' || pir::index($past.name, '&infix:') == 0)
&& %curried{$past.name};
my $i := 0;
my $whatevers := 0;
while $curried && $i < $upto_arity {
$whatevers += $past[$i].returns eq 'WhateverCode'
|| $curried > 1 && $past[$1].returns eq 'Whatever';
}
if $whatevers {
my $i = 0;
my @params;
my $block := PAST::Block.new(PAST::Stmts.new(), $past);
$*ST.cur_lexpad()[0].push($block);
while $i < $upto_arity {
my $old := $past[$i];
if $old.returns eq 'WhateverCode' {
my $new := PAST::Op.new( :pasttype<call>, :node($/), $old);
my $acount := 0;
while $acount < $old.arity {
my $pname := '$x' ~ (+@params);
@params.push(hash(
:variable_name($pname),
:nominal_type($*ST.find_symbol(['Mu'])),
:is_parcel(1),
));
$block[0].push(PAST::Var.new(:name($pname), :scope<lexical_6model>, :isdecl(1)));
$new.push(PAST::Var.new(:name($pname), :scope<lexical_6model>));
}
$past[$i] := $new;
}
elsif $curried > 1 && $old.returns eq 'Whatever' {
my $pname := '$x' ~ (+@params);
@params.push(hash(
:variable_name($pname),
:nominal_type($*ST.find_symbol(['Mu'])),
:is_parcel(1),
));
$block[0].push(PAST::Var.new(:name($pname), :scope<lexical_6model>, :isdecl(1)));
$past[$i] := PAST::Var.new(:name($pname), :scope<lexical_6model>);
}
$i++;
}
my $signature := create_signature_object(@params, $block);
add_signature_binding_code($block, $signature);
my $code := $*ST.create_code_object($block, 'WhateverCode', $signature);
$past := block_closure(reference_to_code_object($code, $block));
}
$past
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment