Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Add @xyz::foo and $GLOBAL::bar variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Nov 17, 2009
1 parent 2046e24 commit d7b50d4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/NQP/Actions.pm
Expand Up @@ -207,7 +207,15 @@ method variable($/) {
$past.unshift( PAST::Var.new( :name('$/') ) );
}
else {
$past := PAST::Var.new( :name(~$/) );
my @name := NQP::Compiler.parse_name(~$/);
$past := PAST::Var.new( :name(~@name.pop) );
if (@name) {
if @name[0] eq 'GLOBAL' { @name.shift; }
$past.namespace(@name);
$past.scope('package');
$past.viviself( sigiltype( $<sigil> ) );
$past.lvalue(1);
}
if $<twigil>[0] eq '*' {
$past.scope('contextual');
$past.viviself( PAST::Op.new( 'Contextual ' ~ ~$/ ~ ' not found',
Expand Down
2 changes: 1 addition & 1 deletion src/NQP/Compiler.pir
Expand Up @@ -21,7 +21,7 @@ NQP::Compiler - NQP compiler
.sub '' :anon :load :init
.local pmc p6meta, nqpproto
p6meta = get_hll_global 'P6metaclass'
nqpproto = p6meta.'new_class'('Regex::P6Grammar::Compiler', 'parent'=>'HLL::Compiler')
nqpproto = p6meta.'new_class'('NQP::Compiler', 'parent'=>'HLL::Compiler')
nqpproto.'language'('NQP-rx')
$P0 = get_hll_global ['NQP'], 'Grammar'
nqpproto.'parsegrammar'($P0)
Expand Down
2 changes: 1 addition & 1 deletion src/NQP/Grammar.pm
Expand Up @@ -204,7 +204,7 @@ token colonpair {
}

token variable {
| <sigil> <twigil>? <desigilname=.ident>
| <sigil> <twigil>? <desigilname=.name>
| <sigil> <?[<[]> <postcircumfix>
| $<sigil>=['$'] $<desigilname>=[<[/_!]>]
}
Expand Down
36 changes: 36 additions & 0 deletions src/cheats/hll-compiler.pir
Expand Up @@ -75,3 +75,39 @@
.return ($S0)
.end
.sub 'parse_name' :method
.param string name
# split name on ::
.local pmc ns
ns = split '::', name
# move any leading sigil to the last item
.local string sigil
$S0 = ns[0]
sigil = substr $S0, 0, 1
$I0 = index '$@%&', sigil
if $I0 < 0 goto sigil_done
substr $S0, 0, 1, ''
ns[0] = $S0
$S0 = ns[-1]
$S0 = concat sigil, $S0
ns[-1] = $S0
sigil_done:
# remove any empty items from the list
.local pmc ns_it
ns_it = iter ns
ns = new ['ResizablePMCArray']
ns_loop:
unless ns_it goto ns_done
$S0 = shift ns_it
unless $S0 > '' goto ns_loop
push ns, $S0
goto ns_loop
ns_done:
# return the result
.return (ns)
.end
24 changes: 24 additions & 0 deletions t/nqp/43-package-var.t
@@ -0,0 +1,24 @@
#! nqp.pbc

# Accessing package variables directly

plan(3);

our $var;

$GLOBAL::var := 1;
$ABC::def := 2;
@XYZ::ghi[0] := 3;

ok( $var == 1, '$GLOBAL::var works');


module ABC {
our $def;
ok( $def == 2, '$ABC::def works');
}

module XYZ {
our @ghi;
ok( @ghi[0] == 3, '@XYZ::ghi works');
}

0 comments on commit d7b50d4

Please sign in to comment.