Skip to content

Commit

Permalink
Refactor IO::ArgFiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Jun 30, 2010
1 parent 8e7bf30 commit 7df2c27
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
59 changes: 16 additions & 43 deletions src/core/IO/ArgFiles.pm
@@ -1,53 +1,26 @@
class IO::ArgFiles is IO {
has @!filenames;
has $!filename;
has $!current_file;
has $!ins;

submethod BUILD {
if @*ARGS {
@!filenames = @*ARGS.eager;
} else {
@!filenames = '-';
}
}
has $!args;
has $.filename;

method eof() {
$.next_file;
$!current_file.eof && !@!filenames.elems;
}

method getc() {
...
! $!args && $!filename.defined && (!pir::istrue($!PIO) || ?$!PIO.eof);
}

method get() {
$.next_file;
$!ins++;
$!current_file.get;
}

method lines() {
gather while !$.eof {
my $line = $.get;
take $line if defined $line;
unless $!PIO {
$!filename = $!args ?? $!args.shift !! '-';
self.open($!filename, :r) ||
fail "Unable to open file '$!filename'";
}
}

method filename() {
$!filename;
}

method ins() {
$!ins;
}

method next_file() {
if (!defined $!current_file) || ($!current_file.eof) {
$!current_file.close if $!current_file && $!filename ne '-';
fail if @!filenames.elems == 0;
$!filename = @!filenames.shift;
$!current_file = $!filename eq '-' ?? $*IN !! open($!filename);
my $x = $!PIO.readline;
if $x eq '' && ?$!PIO.eof {
self.close;
$!PIO = Nil;
$!args ?? self.get !! fail "End of argfiles reached"
}
else {
$!ins++;
$x.chomp;
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/glue/run.pir
Expand Up @@ -87,6 +87,11 @@ of the compilation unit.
set_hll_global '@ARGS', $P2
setprop $P2, "rw", true

## set up $*ARGFILES
$P3 = get_hll_global ['IO'], 'ArgFiles'
$P3 = $P3.'new'('args'=>$P2)
set_hll_global '$ARGFILES', $P3

## set up %*VM
load_bytecode 'config.pbc'
.local pmc vm, interp, config
Expand Down

0 comments on commit 7df2c27

Please sign in to comment.