Skip to content

Commit

Permalink
Add a dyanmic-scope lexical pragma
Browse files Browse the repository at this point in the history
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 871cbef
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Perl6/World.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 871cbef

Please sign in to comment.