Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Yapsi] implemented sub declarations

  • Loading branch information...
commit 6d95f690f54158eb83081944ccc88424f3cdb44f 1 parent 28889de
@masak authored
Showing with 29 additions and 7 deletions.
  1. +25 −7 lib/Yapsi.pm
  2. +4 −0 t/compiler.t
View
32 lib/Yapsi.pm
@@ -48,7 +48,11 @@ grammar Yapsi::Perl6::Grammar {
token lvalue { <declaration> || <variable> || <increment> }
token value { <variable> || <literal> || <declaration> || <saycall>
|| <increment> }
- rule declaration { $<declarator>=['my'|'our'] <variable> }
+ rule declaration {
+ <subdecl>
+ || $<declarator>=['my'|'our'] [<subdecl> || <variable>]
+ }
+ rule subdecl { 'sub' $<subname>=[\w+] <block> }
token variable { '$' \w+ }
token literal { \d+ }
@@ -164,8 +168,10 @@ class Yapsi::Perl6::Actions {
my @vars;
my &find-declarations = sub ($m, $key) {
if $key eq "declaration" {
- push @vars, { :name(~$m<variable>),
- :our($m<declarator> eq 'our') };
+ my $name = $m<variable> ?? ~$m<variable>
+ !! '&' ~ $m<subdecl><subname>;
+ my $our = $m<declarator> eq 'our';
+ push @vars, { :$name, :$our };
}
};
@@ -254,11 +260,23 @@ class Yapsi::Perl6::Actions {
# .variable and .block methods.
method declaration($/) {
- if %!vars{~$<variable>} eq 'declaration?' {
- %!vars{~$<variable>} = @blockstack[*-1].name;
- }
+ if $<variable> {
+ if %!vars{~$<variable>} eq 'declaration?' {
+ %!vars{~$<variable>} = @blockstack[*-1].name;
+ }
- make $<variable>.ast;
+ make $<variable>.ast;
+ }
+ else { # subdecl
+ my $name = '&' ~ $<subdecl><subname>;
+ %!vars{$name} = @blockstack[*-1].name;
+ my $bind = FUTURE::Bind.new();
+ $bind.children.push(
+ FUTURE::Var.new(:$name),
+ $<subdecl><block>.ast
+ );
+ make $bind;
+ }
}
method variable($/) {
View
4 t/compiler.t
@@ -38,6 +38,9 @@ my @programs-that-compile =
'my $a; $a()',
'{ say 42 }()',
'my $a = 1; { say my $a = 2 }', # declare+use in block is fine
+ 'sub foo {}',
+ 'my sub foo {}',
+ 'our sub foo {}',
;
sub escape($string) { $string.subst("\n", "\\n", :g) }
@@ -71,6 +74,7 @@ my @programs-that-don't-compile = # '
'unless {}' => 'could not parse',
'unless a {}' => 'could not parse',
'my $a = 1; { say $a; my $a = 2 }' => 'reference to outer variable',
+ 'sub foo' => 'could not parse',
;
for @programs-that-don't-compile -> $pair { # '
Please sign in to comment.
Something went wrong with that request. Please try again.