Skip to content

Commit

Permalink
Support specifying :tags when doing a use, which will import things w…
Browse files Browse the repository at this point in the history
…ith those tags. Default to :DEFAULT if none specified. Also always import :MANDATORY no matter what tags are specified.
  • Loading branch information
jnthn committed Mar 25, 2009
1 parent 0f50d4f commit bb22e02
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
39 changes: 33 additions & 6 deletions src/builtins/eval.pir
Expand Up @@ -175,12 +175,36 @@ itself can be found in src/builtins/control.pir.
import_ns = $P0.'get_namespace'()
got_import_ns:

# Look up symbols to import by default.
.local pmc export_ns
$P0 = compiler_obj.'parse_name'(module)
push $P0, 'EXPORT'
export_ns = get_hll_global $P0, 'DEFAULT'
if null export_ns goto done_import
# Get list of symbols to import.
.local pmc tag_hash, tags
tag_hash = options['tags']
tags = new 'ResizableStringArray'
if null tag_hash goto default_tag
$P0 = iter tag_hash
th_it_loop:
unless $P0 goto have_tags
$S0 = shift $P0
push tags, $S0
goto th_it_loop
default_tag:
push tags, 'DEFAULT'
have_tags:

# Always need to import MANDATORY stuff.
push tags, 'MANDATORY'

# Look up symbols to import and import them by tag.
.local pmc export_ns, export_root_nsarray, tag_it
export_root_nsarray = compiler_obj.'parse_name'(module)
push export_root_nsarray, 'EXPORT'
tag_it = iter tags
tag_it_loop:

# Find symbols to be imported from this tag.
unless tag_it goto tag_it_loop_end
$S0 = shift tag_it
export_ns = get_hll_global export_root_nsarray, $S0
if null export_ns goto tag_it_loop

# Iterate over them and import.
.local pmc it
Expand All @@ -193,6 +217,9 @@ itself can be found in src/builtins/control.pir.
goto it_loop
it_loop_end:

goto tag_it_loop
tag_it_loop_end:

done_import:
.return (retval)
.end
Expand Down
48 changes: 38 additions & 10 deletions src/parser/actions.pm
Expand Up @@ -373,21 +373,49 @@ method use_statement($/) {
my $name := ~$<name>;
my $past;
if $name ne 'v6' && $name ne 'lib' {
## Handle tags.
my $tags;
if $<EXPR> {
$tags := $( $<EXPR>[0] );
if !($tags.isa(PAST::Op) && $tags.name() eq 'infix:,') {
$tags := PAST::Op.new( $tags );
}
for @($tags) {
if $_.returns() ne 'Pair' {
$/.panic("Unknown import list expression in use");
}
}
$tags.name('hash');
$tags.pasttype('call');
}

## Create a loadinit node so the use module is loaded
## when this module is loaded...
our @?BLOCK;
@?BLOCK[0].loadinit().push(
PAST::Op.new(
PAST::Val.new( :value($name) ),
:name('use'),
:pasttype('call'),
:node( $/ )
)
my $use_call := PAST::Op.new(
PAST::Val.new( :value($name) ),
:name('use'),
:pasttype('call'),
:node( $/ )
);
## ...and load it immediately to get its BEGIN semantics
## and symbols for the current compilation.
if $tags {
$tags.named('tags');
$use_call.push($tags);
}
@?BLOCK[0].loadinit().push($use_call);

## ...and load it immediately to get its BEGIN semantics and
## symbols for the current compilation.
## XXX Need to handle tags here too, and creating needed lexical
## slots.
our @?NS;
use($name, :import_to(@?NS ?? @?NS[0] !! ''));
if $tags {
my %tag_hash;
for @($tags) { %tag_hash{$_[0].value()} := 1 }
use($name, :import_to(@?NS ?? @?NS[0] !! ''), :tags(%tag_hash));
} else {
use($name, :import_to(@?NS ?? @?NS[0] !! ''));
}
}
$past := PAST::Stmts.new( :node($/) );
make $past;
Expand Down
3 changes: 2 additions & 1 deletion t/spectest.data
Expand Up @@ -10,6 +10,7 @@
#
# S03-operators/binding-arrays.t - regressed to allow slices
# S03-operators/binding-hashes.t - regressed to allow slices
# S10-packages/import.t - regressed when implementing importing; think it's bogus anyway

integration/99problems-01-to-10.t
integration/99problems-11-to-20.t
Expand Down Expand Up @@ -216,10 +217,10 @@ S06-traits/is-copy.t
S06-traits/is-rw.t
S06-traits/misc.t
S09-subscript_slice/slice.t
S10-packages/import.t
S10-packages/use-with-class.t
S11-modules/export.t
S11-modules/import.t
S11-modules/import-tag.t
S11-modules/nested.t
S12-attributes/class2.t
S12-attributes/class.t
Expand Down

0 comments on commit bb22e02

Please sign in to comment.