Skip to content

Commit

Permalink
RakuAST: add set-from-also/from-also flag to Is/Does trait
Browse files Browse the repository at this point in the history
So that we can mark the origin of the trait, needed for deparsing
and .raku handling
  • Loading branch information
lizmat committed Jul 29, 2023
1 parent f151aba commit 7114b4d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/Raku/ast/traits.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,20 @@ class RakuAST::Trait
self.mark-applied;
}
}

# traits are not from an "also" by default
method from-also() { False }
}

# The is trait.
class RakuAST::Trait::Is
is RakuAST::Trait
is RakuAST::BeginTime
{
has RakuAST::Name $.name;
has RakuAST::Circumfix $.argument;
has RakuAST::Name $.name;
has RakuAST::Circumfix $.argument;
has RakuAST::Term::Name $.resolved-name;
has Bool $.from-also;

method new(RakuAST::Name :$name!, RakuAST::Circumfix :$argument) {
my $obj := nqp::create(self);
Expand All @@ -126,6 +130,11 @@ class RakuAST::Trait::Is
$obj
}

method set-from-also($from-also) {
nqp::bindattr(self, RakuAST::Trait::Is, '$!from-also',
$from-also ?? True !! False);
}

method PERFORM-BEGIN(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
# See if the name resolves as a type and commit to that.
my $resolution := $resolver.resolve-name-constant($!name);
Expand Down Expand Up @@ -191,13 +200,19 @@ class RakuAST::Trait::Does
is RakuAST::Trait
{
has RakuAST::Type $.type;
has Bool $.from-also;

method new(RakuAST::Type $type) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Trait::Does, '$!type', $type);
$obj
}

method set-from-also($from-also) {
nqp::bindattr(self, RakuAST::Trait::Does, '$!from-also',
$from-also ?? True !! False);
}

method IMPL-TRAIT-NAME() { 'does' }

method IMPL-TRAIT-ARGS(RakuAST::Resolver $resolver, RakuAST::Node $target) {
Expand Down
3 changes: 2 additions & 1 deletion src/core.c/RakuAST/Deparse.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,8 @@ class RakuAST::Deparse {

if $ast.traits -> @traits {
for @traits -> $trait {
@parts.push(self.deparse($trait));
@parts.push(self.deparse($trait))
unless try $trait.from-also;
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/core.c/RakuAST/Raku.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,11 @@ augment class RakuAST::Node {
);
}

my @traits = self.traits.grep(!*.from-also);
self!add-WHY: $self!nameds:
<scope declarator name how repr traits body>,
<scope declarator name how repr>,
(traits => @traits if @traits),
'body',
(parameterization => $signature
if $signature && $signature.parameters.elems)
}
Expand Down

0 comments on commit 7114b4d

Please sign in to comment.