Skip to content

Commit

Permalink
RakuAST: Support array shapes in variable declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
niner committed Oct 24, 2022
1 parent 119ea0c commit 0bd2deb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Raku/Actions.nqp
Expand Up @@ -1257,8 +1257,9 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
my $decl;
if $<desigilname> {
my str $name := $<sigil> ~ ($<twigil> || '') ~ $<desigilname>;
my $shape := $<semilist> ?? $<semilist>[0].ast !! self.r('SemiList');
$decl := self.r('VarDeclaration', 'Simple').new:
:$scope, :$type, :$name, :$initializer;
:$scope, :$type, :$name, :$initializer, :$shape;
if $scope eq 'my' || $scope eq 'state' || $scope eq 'our' {
$*R.declare-lexical($decl);
}
Expand Down
35 changes: 35 additions & 0 deletions src/Raku/Grammar.nqp
Expand Up @@ -1916,6 +1916,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
token variable_declarator {
:my $*IN_DECL := 'variable';
:my $sigil;
[
| <sigil> <twigil>? <desigilname>?
| $<sigil>=['$'] $<desigilname>=[<[/_!¢]>]
Expand All @@ -1924,7 +1925,41 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
{
$*IN_DECL := '';
$*LEFTSIGIL := nqp::substr(self.orig(), self.from, 1) unless $*LEFTSIGIL;
$sigil := $<sigil>.Str;
}
[
<.unsp>?
$<shape>=[
| '(' ~ ')' <signature>
{
if $sigil eq '&' {
self.typed_sorry('X::Syntax::Reserved',
reserved => '() shape syntax in routine declarations',
instead => ' (maybe use :() to declare a longname?)'
);
}
elsif $sigil eq '@' {
self.typed_sorry('X::Syntax::Reserved',
reserved => '() shape syntax in array declarations');
}
elsif $sigil eq '%' {
self.typed_sorry('X::Syntax::Reserved',
reserved => '() shape syntax in hash declarations');
}
else {
self.typed_sorry('X::Syntax::Reserved',
reserved => '() shape syntax in variable declarations');
}
}
| :dba('shape definition') '[' ~ ']' <semilist>
{ $sigil ne '@' && self.typed_sorry('X::Syntax::Reserved',
reserved => '[] shape syntax with the ' ~ $sigil ~ ' sigil') }
| :dba('shape definition') '{' ~ '}' <semilist>
{ $sigil ne '%' && self.typed_sorry('X::Syntax::Reserved',
reserved => '{} shape syntax with the ' ~ $sigil ~ ' sigil') }
| <?[<]> <postcircumfix> <.NYI: "Shaped variable declarations">
]+
]?
[ <.ws> <trait>+ ]?
[<.ws> <initializer>]?
}
Expand Down
11 changes: 9 additions & 2 deletions src/Raku/ast/variable-declaration.rakumod
Expand Up @@ -164,18 +164,20 @@ class RakuAST::VarDeclaration::Simple is RakuAST::Declaration is RakuAST::Implic
has str $.name;
has str $!storage-name;
has RakuAST::Initializer $.initializer;
has RakuAST::SemiList $.shape;
has RakuAST::Package $!attribute-package;
has Mu $!package;

method new(str :$name!, RakuAST::Type :$type, RakuAST::Initializer :$initializer,
str :$scope) {
str :$scope, RakuAST::SemiList :$shape) {
my $obj := nqp::create(self);
if nqp::chars($name) < 2 {
nqp::die('Cannot use RakuAST::VarDeclaration::Simple to declare an anonymous varialbe; use RakuAST::VarDeclaration::Anonymous');
}
nqp::bindattr_s($obj, RakuAST::VarDeclaration::Simple, '$!name', $name);
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', $scope);
nqp::bindattr($obj, RakuAST::VarDeclaration::Simple, '$!type', $type // RakuAST::Type);
nqp::bindattr($obj, RakuAST::VarDeclaration::Simple, '$!shape', $shape // RakuAST::SemiList);
nqp::bindattr($obj, RakuAST::VarDeclaration::Simple, '$!initializer',
$initializer // RakuAST::Initializer);
$obj
Expand Down Expand Up @@ -391,11 +393,16 @@ class RakuAST::VarDeclaration::Simple is RakuAST::Declaration is RakuAST::Implic
:scope('lexical'), :decl('contvar'), :name($!name),
:value($container)
);
if self.IMPL-HAS-CONTAINER-BASE-TYPE {
if $!shape || self.IMPL-HAS-CONTAINER-BASE-TYPE {
$qast := QAST::Op.new( :op('bind'), $qast, QAST::Op.new(
:op('callmethod'), :name('new'),
QAST::WVal.new( :value(self.IMPL-CONTAINER-BASE-TYPE) )
) );
if $!shape {
my $shape_ast := $!shape.IMPL-TO-QAST($context);
$shape_ast.named('shape');
$qast[1].push($shape_ast);
}
}
$qast
}
Expand Down

0 comments on commit 0bd2deb

Please sign in to comment.