Skip to content

Commit

Permalink
RakuAST hanlding of implicit $!, $/, and $_
Browse files Browse the repository at this point in the history
At the compilation unit and routine level.
  • Loading branch information
jnthn committed Jul 7, 2020
1 parent bd890a8 commit 55e15db
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Raku/ast/code.rakumod
Expand Up @@ -163,7 +163,8 @@ class RakuAST::PointyBlock is RakuAST::Block {

# Done by all kinds of Routine.
class RakuAST::Routine is RakuAST::LexicalScope is RakuAST::Term is RakuAST::Code is RakuAST::Meta
is RakuAST::SinkBoundary is RakuAST::Declaration {
is RakuAST::SinkBoundary is RakuAST::Declaration
is RakuAST::ImplicitDeclarations {
has RakuAST::Name $.name;
has RakuAST::Signature $.signature;
has RakuAST::Blockoid $.body;
Expand Down Expand Up @@ -201,6 +202,14 @@ class RakuAST::Routine is RakuAST::LexicalScope is RakuAST::Term is RakuAST::Cod
$routine
}

method PRODUCE-IMPLICIT-DECLARATIONS() {
self.IMPL-WRAP-LIST([
RakuAST::VarDeclaration::Implicit::Special.new(:name('$/')),
RakuAST::VarDeclaration::Implicit::Special.new(:name('$!')),
RakuAST::VarDeclaration::Implicit::Special.new(:name('$_')),
])
}

method IMPL-QAST-FORM-BLOCK(RakuAST::IMPL::QASTContext $context) {
# TODO return handler
my $block := QAST::Block.new(
Expand Down
3 changes: 3 additions & 0 deletions src/Raku/ast/compunit.rakumod
Expand Up @@ -69,6 +69,9 @@ class RakuAST::CompUnit is RakuAST::LexicalScope is RakuAST::SinkBoundary
nqp::push(@decls, RakuAST::VarDeclaration::Implicit::Constant.new(
name => '$?PACKAGE', value => $global.compile-time-value
));
nqp::push(@decls, RakuAST::VarDeclaration::Implicit::Special.new(:name('$/')));
nqp::push(@decls, RakuAST::VarDeclaration::Implicit::Special.new(:name('$!')));
nqp::push(@decls, RakuAST::VarDeclaration::Implicit::Special.new(:name('$_')));
}
self.IMPL-WRAP-LIST(@decls)
}
Expand Down
37 changes: 36 additions & 1 deletion src/Raku/ast/variable-declaration.rakumod
Expand Up @@ -295,7 +295,7 @@ class RakuAST::VarDeclaration::Simple is RakuAST::Declaration is RakuAST::Implic
}
}

# An implicitly declared variable. Typically used for $/, $!, and $_.
# The commonalities for implicitly declared variables.
class RakuAST::VarDeclaration::Implicit is RakuAST::Declaration {
has str $.name;

Expand All @@ -314,6 +314,12 @@ class RakuAST::VarDeclaration::Implicit is RakuAST::Declaration {
'my'
}

method generate-lookup() {
my $lookup := RakuAST::Var::Lexical.new($!name);
$lookup.set-resolution(self);
$lookup
}

method IMPL-TO-QAST(RakuAST::IMPL::QASTContext $context) {
self.IMPL-LOOKUP-QAST($context)
}
Expand All @@ -323,6 +329,35 @@ class RakuAST::VarDeclaration::Implicit is RakuAST::Declaration {
}
}

# An implicitly declared special variable. Typically used for $/, $!, and $_.
class RakuAST::VarDeclaration::Implicit::Special is RakuAST::VarDeclaration::Implicit
is RakuAST::Meta {
method PRODUCE-META-OBJECT() {
# Reuse the container descriptor for the common cases that we expect
# to have.
my constant COMMON := nqp::hash(
'$_', ContainerDescriptor.new(:of(Mu), :default(Any), :!dynamic, :name('$_')),
'$/', ContainerDescriptor.new(:of(Mu), :default(Any), :dynamic, :name('$/')),
'$!', ContainerDescriptor.new(:of(Mu), :default(Any), :dynamic, :name('$!'))
);
my $cont-desc := COMMON{self.name} //
ContainerDescriptor.new(:of(Mu), :default(Any), :!dynamic, :name(self.name));
my $container := nqp::create(Scalar);
nqp::bindattr($container, Scalar, '$!descriptor', $cont-desc);
nqp::bindattr($container, Scalar, '$!value', Any);
$container
}

method IMPL-QAST-DECL(RakuAST::IMPL::QASTContext $context) {
my $container := self.meta-object;
$context.ensure-sc($container);
QAST::Var.new(
:scope('lexical'), :decl('contvar'), :name(self.name),
:value($container)
)
}
}

# An implicitly declared constant variable - that is, one with a value that is
# fixed at compile time. Used for $?PACKAGE and similar.
class RakuAST::VarDeclaration::Implicit::Constant is RakuAST::VarDeclaration::Implicit
Expand Down

0 comments on commit 55e15db

Please sign in to comment.