Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement ~~ topicalization
  • Loading branch information
sorear committed Feb 5, 2011
1 parent de2b3f3 commit 1900c4a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/NieczaPassBegin.pm6
Expand Up @@ -73,6 +73,8 @@ augment class Body { method begin(:$once = False, :$itop, :$body_of, :$cur_pkg,
if $type eq 'regex' {
$metabody.add_my_name('$*/');
}
$metabody.add_my_name('$_') unless $.transparent ||
($metabody.lexicals<$_>:exists);

pop @*opensubs if $.transparent;

Expand Down
35 changes: 35 additions & 0 deletions src/niecza
Expand Up @@ -17,8 +17,43 @@ use NieczaCompiler;
use MONKEY_TYPING;

use NieczaActions;
use Operator;
sub node($M) { { line => $M.cursor.lineof($M.to) } }

sub mklet($value, $body) {
my $var = ::GLOBAL::NieczaActions.gensym;
::Op::Let.new(var => $var, to => $value,
in => $body(::Op::LetVar.new(name => $var)));
}

sub mklex($/, $name) { ::Op::Lexical.new(|node($/), :$name); }

sub mkcall($/, $name, *@positionals) {
::Op::CallSub.new(|node($/),
invocant => ::Op::Lexical.new(|node($/), :$name), :@positionals);
}

augment class NieczaActions {
method keyspace($/) { }
method infix:sym<~~> ($/) { make ::Operator::SmartMatch.new }
}

augment class Operator {
class SmartMatch is Operator {
method as_function($/) { mklex($/, '&infix:<~~>') }
method with_args($/, *@args) {
mklet(mklex($/, '$_'), -> $old_ {
::Op::StatementList.new(|node($/), children => [
# XXX should be a raw bind
::Op::Bind.new(:!readonly, lhs => mklex($/, '$_'), rhs => @args[0]),
mklet(::Op::CallMethod.new(receiver => @args[1], name => 'ACCEPTS', args => [ mklex($/, '$_') ]), -> $result {
::Op::StatementList.new(children => [
# XXX should be a raw bind
::Op::Bind.new(:!readonly, lhs => mklex($/, '$_'),
rhs => $old_),
$result]) }) ]) });
}
}
}

sub GetOptions(*@pairs, :$permute = True, :onerror($onerror_), :onarg($onarg_)) {
Expand Down
4 changes: 4 additions & 0 deletions test2.pl
Expand Up @@ -21,6 +21,10 @@
my $foo = [5];
for $foo { .shift }
is +$foo, 0, ".method works";

my $x = 5;
$x ~~ .++;
is $x, 6, "~~ topicalization works";
}

#is $?FILE, 'test.pl', '$?FILE works';
Expand Down

0 comments on commit 1900c4a

Please sign in to comment.