Skip to content
Permalink
Browse files

Add a `dyanmic-scope` lexical pragma

This allows for specifying that:

* Without args, all variables declared in the scope the pragma applies
  to will be automatically `is dynamic`
* With args, the named variables declared in the scope the pragma
  applies to will be automatically be `is dynamic`

This pragma is primarily intended for use with a future pragma export
mechanism, so that modules like P5tie can specify that they need access
to all variables in order to function correctly. This will also (though
it needs more work yet) provide a way to request `$_` also be dynamic.
  • Loading branch information...
jnthn committed Jan 18, 2019
1 parent f71ca0d commit 871cbefc6f526d0424acb0c4707c3c11075a1a5c
Showing with 30 additions and 1 deletion.
  1. +30 −1 src/Perl6/World.nqp
@@ -1209,6 +1209,23 @@ class Perl6::World is HLL::World {
$*LANG.set_pragma($_.value, $on) for %isms;
}
}
elsif $name eq 'dynamic-scope' {
unless $on {
self.throw($/, 'X::Pragma::CannotWhat', :what<no>, :$name);
}
if nqp::islist($arglist) && nqp::elems($arglist) {
# Just some variables.
my %dyn;
for $arglist {
%dyn{$_} := 1;
}
$*LANG.set_pragma('dynamic-scope', -> $var { %dyn{$var} || 0 });
}
else {
# All variables.
$*LANG.set_pragma('dynamic-scope', -> $var { 1 });
}
}
else {
$RMD(" '$name' is not a valid pragma") if $RMD;
return 0; # go try module
@@ -1693,13 +1710,25 @@ class Perl6::World is HLL::World {
}

# Creates a new container descriptor and adds it to the SC.
method create_container_descriptor($of, $name, $default = $of, $dynamic = nqp::chars($name) > 2 && nqp::eqat($name, '*', 1)) {
method create_container_descriptor($of, $name, $default = $of, $dynamic = is_dynamic($name)) {
my $cd_type := self.find_symbol(['ContainerDescriptor'], :setting-only);
my $cd := $cd_type.new( :$of, :$name, :$default, :$dynamic );
self.add_object_if_no_sc($cd);
$cd
}

sub is_dynamic($name) {
# Dynamic if has the * twigil.
if nqp::chars($name) > 2 && nqp::eqat($name, '*', 1) {
1
}
# Otherwise, check pragma.
else {
my $dynprag := $*LANG.pragma('dynamic-scope');
$dynprag ?? $dynprag($name) !! 0
}
}

# Builds a container.
method build_container(%cont_info, $descriptor) {
my $cont;

0 comments on commit 871cbef

Please sign in to comment.
You can’t perform that action at this time.