Skip to content

Commit

Permalink
RakuAST: give RakuAST::Var::Lexical access to the fully parsed name
Browse files Browse the repository at this point in the history
  • Loading branch information
niner committed Apr 23, 2023
1 parent 2d656cd commit f12c859
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
38 changes: 20 additions & 18 deletions src/Raku/Actions.nqp
Expand Up @@ -1376,8 +1376,10 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
else {
my str $sigil := ~$<sigil>;
my str $twigil := $<twigil> ?? ~$<twigil> !! '';
my str $desigilname := ~$<desigilname>;
self.compile_variable_access($/, $sigil, $twigil, $desigilname, $<desigilname><longname>);
my $desigilname := $<desigilname><longname>
?? $<desigilname><longname>.ast
!! self.r('Name').from-identifier(~$<desigilname>);
self.compile_variable_access($/, $sigil, $twigil, $desigilname);
}
}

Expand All @@ -1400,24 +1402,24 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
]))
)
);
self.compile_variable_access($/, '&', '', $name.canonicalize, '');
self.compile_variable_access($/, '&', '', $name);
}
elsif $<desigilname><variable> {
self.contextualizer-for-sigil($/, ~$<sigil>, $<desigilname><variable>.ast);
}
else {
my str $sigil := ~$<sigil>;
my str $twigil := $<twigil> ?? ~$<twigil> !! '';
my str $desigilname := $<desigilname><longname>
?? $<desigilname><longname>.ast.canonicalize
!! ~$<desigilname>;
self.compile_variable_access($/, $sigil, $twigil, $desigilname, $<desigilname><longname>);
my $desigilname := $<desigilname><longname>
?? $<desigilname><longname>.ast
!! self.r('Name').from-identifier(~$<desigilname>);
self.compile_variable_access($/, $sigil, $twigil, $desigilname);
}
}

method compile_variable_access($/, $sigil, $twigil, $desigilname, $longname) {
my str $name := $sigil ~ $twigil ~ $desigilname;
if $name eq $sigil {
method compile_variable_access($/, $sigil, $twigil, $desigilname) {
my str $name := $sigil ~ $twigil ~ $desigilname.canonicalize;
if $twigil eq '' && $desigilname.is-empty {
# Generate an anonymous state variable.
self.attach: $/, self.r('VarDeclaration', 'Anonymous').new(:$sigil, :scope('state'));
}
Expand All @@ -1432,9 +1434,9 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
self.attach: $/, $decl;
}
elsif $twigil eq '' {
if !$longname || $longname<name>.ast.is-identifier {
if $desigilname.is-identifier {
if $*LANG.pragma("strict") || $*R.resolve-lexical($name) {
self.attach: $/, self.r('Var', 'Lexical').new($name);
self.attach: $/, self.r('Var', 'Lexical').new(:$sigil, :$desigilname);
}
else {
my $decl := self.r('VarDeclaration','Auto').new(
Expand All @@ -1446,7 +1448,7 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
}
else { # package variable
self.attach: $/, self.r('Var', 'Package').new(
:name($longname<name>.ast),
:name($desigilname),
:$sigil
);
}
Expand Down Expand Up @@ -1479,13 +1481,13 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
}
elsif $twigil eq '^' {
my $decl := self.r('VarDeclaration', 'Placeholder', 'Positional').new:
$sigil ~ $desigilname;
$sigil ~ $desigilname.canonicalize;
$*R.declare-lexical($decl);
self.attach: $/, $decl;
}
elsif $twigil eq ':' {
my $decl := self.r('VarDeclaration', 'Placeholder', 'Named').new:
$sigil ~ $desigilname;
$sigil ~ $desigilname.canonicalize;
$*R.declare-lexical($decl);
self.attach: $/, $decl;
}
Expand All @@ -1511,16 +1513,16 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
self.r('ApplyPostfix').new:
:postfix(
self.r('Call', 'Method').new(
:name(self.r('Name').from-identifier($desigilname)),
:name($desigilname),
:args($<arglist> ?? $<arglist>.ast !! self.r('ArgList').new),
)),
:operand(
self.r('Term', 'Self').new
));
}
elsif $twigil eq '~' {
my $grammar := $/.slang_grammar($desigilname);
my $actions := $/.slang_actions($desigilname);
my $grammar := $/.slang_grammar($desigilname.canonicalize);
my $actions := $/.slang_actions($desigilname.canonicalize);
self.attach: $/, self.r('Var', 'Slang').new(:$grammar, :$actions);
}
else {
Expand Down
4 changes: 3 additions & 1 deletion src/Raku/ast/literals.rakumod
Expand Up @@ -202,7 +202,9 @@ class RakuAST::QuotedString
if nqp::elems($!processors) {
for $!processors {
if $_ eq 'val' {
nqp::push(@needed, RakuAST::Var::Lexical::Setting.new('&val'));
nqp::push(@needed, RakuAST::Var::Lexical::Setting.new(
:sigil<&>,
:desigilname(RakuAST::Name.from-identifier('val'))));
last;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Raku/ast/name.rakumod
Expand Up @@ -45,7 +45,7 @@ class RakuAST::Name
}

method is-empty() {
nqp::elems($!parts) ?? False !! True
(!nqp::elems($!parts) || self.is-identifier && $!parts[0].name eq '') ?? True !! False
}

method is-simple() {
Expand Down
32 changes: 24 additions & 8 deletions src/Raku/ast/variable-access.rakumod
Expand Up @@ -10,31 +10,45 @@ class RakuAST::Var::Lexical
is RakuAST::Var
is RakuAST::Lookup
{
has str $.name;
has str $.sigil;
has str $.twigil;
has RakuAST::Name $.desigilname;

method new(str $name) {
method new(str $name?, Str :$sigil, Str :$twigil, RakuAST::Name :$desigilname) {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::Var::Lexical, '$!name', $name);
if $name {
nqp::bindattr_s($obj, RakuAST::Var::Lexical, '$!sigil', nqp::substr($name, 0, 1));
nqp::bindattr_s($obj, RakuAST::Var::Lexical, '$!twigil', '');
nqp::bindattr($obj, RakuAST::Var::Lexical, '$!desigilname',
RakuAST::Name.from-identifier(nqp::substr($name, 1)))
}
else {
nqp::bindattr_s($obj, RakuAST::Var::Lexical, '$!sigil', $sigil);
nqp::bindattr_s($obj, RakuAST::Var::Lexical, '$!twigil', $twigil);
nqp::bindattr($obj, RakuAST::Var::Lexical, '$!desigilname', $desigilname);
}
$obj
}

method sigil() { nqp::substr($!name, 0, 1) }
method name() {
($!sigil // '') ~ ($!twigil // '') ~ $!desigilname.canonicalize
}

method can-be-bound-to() {
self.is-resolved ?? self.resolution.can-be-bound-to !! False
}

method resolve-with(RakuAST::Resolver $resolver) {
my $resolved := $resolver.resolve-lexical($!name);
my $resolved := $resolver.resolve-lexical(self.name);
if $resolved {
self.set-resolution($resolved);
}
Nil
}

method undeclared-symbol-details() {
self.sigil eq '&'
?? RakuAST::UndeclaredSymbolDescription::Routine.new($!name)
$!sigil eq '&'
?? RakuAST::UndeclaredSymbolDescription::Routine.new(self.name)
!! Nil
}

Expand Down Expand Up @@ -330,7 +344,9 @@ class RakuAST::Var::Compiler::Routine
{
method new() {
my $obj := nqp::create(self);
nqp::bindattr_s($obj, RakuAST::Var::Lexical, '$!name', '&?ROUTINE');
nqp::bindattr_s($obj, RakuAST::Var::Lexical, '$!sigil', '&');
nqp::bindattr_s($obj, RakuAST::Var::Lexical, '$!twigil', '?');
nqp::bindattr($obj, RakuAST::Var::Lexical, '$!desigilname', RakuAST::Name.from-identifier('ROUTINE'));
$obj
}

Expand Down

0 comments on commit f12c859

Please sign in to comment.