Skip to content

Commit

Permalink
first shot at "lazy" statement prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed May 16, 2012
1 parent f4e6701 commit 18f9e41
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Perl6/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,10 @@ class Perl6::Actions is HLL::Actions {
method statement_prefix:sym<PRE>($/) { make $*W.add_phaser($/, 'PRE', ($<blorst>.ast)<code_object>, ($<blorst>.ast)<past_block>); }
method statement_prefix:sym<POST>($/) { make $*W.add_phaser($/, 'POST', ($<blorst>.ast)<code_object>, ($<blorst>.ast)<past_block>); }

method statement_prefix:sym<lazy>($/) {
make $*W.create_lazy($/, $<blorst>.ast()<code_object>);
}

method statement_prefix:sym<DOC>($/) {
$*W.add_phaser($/, ~$<phase>, ($<blorst>.ast)<code_object>)
if %*COMPILING<%?OPTIONS><doc>;
Expand Down
1 change: 1 addition & 0 deletions src/Perl6/Grammar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ grammar Perl6::Grammar is HLL::Grammar {
token statement_prefix:sym<try> { <sym> <blorst> }
token statement_prefix:sym<gather>{ <sym> <blorst> }
token statement_prefix:sym<do> { <sym> <blorst> }
token statement_prefix:sym<lazy> { <sym> <blorst> }
token statement_prefix:sym<DOC> {
<sym> \s <.ws> $<phase>=['BEGIN' || 'CHECK' || 'INIT']
<blorst>
Expand Down
7 changes: 7 additions & 0 deletions src/Perl6/World.pm
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,13 @@ class Perl6::World is HLL::World {
self.finish_code_object($code, $code_past, $is_dispatcher, :yada($yada));
$code
}

method create_lazy($/, $code) {
my $type := self.find_symbol(['LazyScalar']);
my $container := $type.new($code);
self.add_object($container);
self.get_ref($container);
}

# Stubs a code object of the specified type.
method stub_code_object($type) {
Expand Down
18 changes: 18 additions & 0 deletions src/core/LazyScalar.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
my class LazyScalar is Proxy {
method new($code) {
my int $reified = 0;
my Mu $payload;
self.Proxy::new(
FETCH => sub ($) {
unless $reified {
$payload := $code();
$reified = 1;
}
$payload;
},
STORE => sub ($, Mu \$new) {
$payload := $new;
}
)
}
}
1 change: 1 addition & 0 deletions tools/build/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ CORE_SOURCES = \
src/core/operators.pm \
src/core/metaops.pm \
src/core/terms.pm \
src/core/LazyScalar.pm \
src/core/NYI.pm \
src/core/you_are_here.pm \

Expand Down

0 comments on commit 18f9e41

Please sign in to comment.