Skip to content

Commit

Permalink
introduce sub terms
Browse files Browse the repository at this point in the history
Closes #66.
  • Loading branch information
Carl Masak committed Jan 7, 2016
1 parent 2ae8541 commit 5865342
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/_007/Parser/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,21 @@ class _007::Parser::Actions {
die "Got something in a quasi that we didn't expect: {$/.keys}"; # should never happen
}

method term:sub ($/) {
my $parameterlist = $<parameterlist>.ast;
my $traitlist = $<traitlist>.ast;
my $statementlist = $<blockoid>.ast;

my $block = Q::Block.new(:$parameterlist, :$statementlist);
my %static-lexpad = $*runtime.current-frame.pad;
self.finish-block($block);

my $outer-frame = $*runtime.current-frame;
my $val;
make Q::Term::Sub.new(:$traitlist, :$block);
$val = Val::Sub.new(:name("(anon)"), :$parameterlist, :$statementlist, :$outer-frame, :%static-lexpad);
}

method unquote ($/) {
make Q::Unquote.new(:expr($<EXPR>.ast));
}
Expand Down
9 changes: 9 additions & 0 deletions lib/_007/Parser/Syntax.pm
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ grammar _007::Parser::Syntax {
}
}
}
token term:sub {
sub <.ws>
:my $*insub = True;
<.newpad>
'(' ~ ')' <parameterlist>
<traitlist>
<blockoid>:!s
<.finishpad>
}

token propertylist { [<.ws> <property>]* % [\h* ','] <.ws> }

Expand Down
16 changes: 16 additions & 0 deletions lib/_007/Q.pm
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ class Q::PropertyList does Q {
has Val::Array $.properties = Val::Array.new;
}

class Q::Term::Sub does Q::Term {
has $.traitlist = Q::TraitList.new;
has $.block;

method attribute-order { <traitlist block> }

method eval($runtime) {
return Val::Sub.new(
:name("(anon)"),
:parameterlist($.block.parameterlist),
:statementlist($.block.statementlist),
:outer-frame($runtime.current-frame),
);
}
}

class Q::Block does Q {
has $.parameterlist;
has $.statementlist;
Expand Down
1 change: 1 addition & 0 deletions lib/_007/Runtime/Builtins.pm
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ class _007::Runtime::Builtins {
Q::Term::Array,
Q::Term::Object,
Q::Term::Quasi,
Q::Term::Sub,
Q::Trait,
Q::TraitList,
Q::Unquote,
Expand Down
11 changes: 11 additions & 0 deletions t/features/subs.t
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,15 @@ use _007::Test;
"can assign to a parameter which hides a subroutine";
}

{
my $program = q:to/./;
my f = sub (x) { say(x) };
f("Mr Bond");
.

outputs $program,
"Mr Bond\n",
"expression subs work";
}

done-testing;

0 comments on commit 5865342

Please sign in to comment.