Skip to content

Commit

Permalink
RakuAST: remove ::Package::Augmented
Browse files Browse the repository at this point in the history
Instead, add a $!augmented attribute to ::Package and adapt the
necessary logic to cope with being an augmented class.

This should make it possible to subclass ::Package for specific
types of packages, such as classes and roles.
  • Loading branch information
lizmat committed Oct 29, 2023
1 parent c727c2a commit 4a5e018
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
32 changes: 14 additions & 18 deletions src/Raku/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -1936,25 +1936,21 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
!! $/.panic("Cannot resolve meta-object for $declarator");

# Stub the package AST node.
my str $scope := $*SCOPE // 'our';
my str $scope := $*SCOPE // 'our';
my $augmented := $scope eq 'augment';
$/.typed-panic('X::Syntax::Augment::WithoutMonkeyTyping')
if $augmented && !$*LANG.pragma('MONKEY-TYPING');

my $name-match := $*PACKAGE-NAME;
my $name := $name-match ?? $name-match.ast !! Nodify('Name');
my $package;
if $scope eq 'augment' {
$/.typed-panic('X::Syntax::Augment::WithoutMonkeyTyping')
unless $*LANG.pragma('MONKEY-TYPING');

$package := Nodify('Package','Augmented').new(
:$declarator, :$how, :$name, :$scope
);
$package.IMPL-CHECK($*R, $*CU.context, 1);
}
else {
$package := Nodify('Package').new(
:$declarator, :$how, :$name, :$scope
);
$package.resolve-with($*R);
}
my $name := $name-match ?? $name-match.ast !! Nodify('Name');
my $package := Nodify('Package').new(
:$declarator, :$how, :$name, :$scope, :$augmented
);

$augmented
?? $package.IMPL-CHECK($*R, $*CU.context, 1)
!! $package.resolve-with($*R);

self.set-declarand($/, $*PACKAGE := $package);
}

Expand Down
29 changes: 11 additions & 18 deletions src/Raku/ast/package.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RakuAST::Package
has Mu $.attribute-type;
has Mu $.how;
has Str $.repr;
has Bool $.augmented;

has Mu $!role-group;
has Mu $!block-semantics-applied;
Expand All @@ -39,6 +40,7 @@ class RakuAST::Package
Mu :$attribute-type,
Mu :$how,
Str :$repr,
Bool :$augmented,
RakuAST::Doc::Declarator :$WHY
) {
my $obj := nqp::create(self);
Expand All @@ -50,6 +52,7 @@ class RakuAST::Package
nqp::bindattr($obj, RakuAST::Package, '$!how',
nqp::eqaddr($how,NQPMu) ?? $obj.default-how !! $how);
nqp::bindattr($obj, RakuAST::Package, '$!repr', $repr // Str);
nqp::bindattr($obj, RakuAST::Package, '$!augmented',$augmented // False);

$obj.set-traits($traits) if $traits;
$obj.replace-body($body, $parameterization);
Expand Down Expand Up @@ -103,7 +106,13 @@ class RakuAST::Package
}

method resolve-with(RakuAST::Resolver $resolver) {
if $!name {
if $!augmented {
my $resolved := $resolver.resolve-name(self.name);
if $resolved {
self.set-resolution($resolved);
}
}
elsif $!name {
my $resolved := $resolver.resolve-name-constant($!name);
if $resolved {
my $meta-object := $resolved.compile-time-value;
Expand Down Expand Up @@ -296,7 +305,7 @@ class RakuAST::Package
}

method PRODUCE-STUBBED-META-OBJECT() {
if self.is-resolved {
if $!augmented || self.is-resolved {
self.resolution.compile-time-value;
}
else {
Expand Down Expand Up @@ -440,19 +449,3 @@ class RakuAST::Package

method needs-sink-call() { False }
}

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

method PRODUCE-STUBBED-META-OBJECT() {
self.resolution.compile-time-value
}
}

0 comments on commit 4a5e018

Please sign in to comment.