Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
basic support for open/print/close
  • Loading branch information
FROGGS committed May 10, 2013
1 parent d6448f8 commit eec4624
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Perl5/Actions.nqp
Expand Up @@ -3957,6 +3957,16 @@ class Perl5::Actions is HLL::Actions does STDActions {
make call_expr_or_topic( $/, 'chars' ) # TODO http://perldoc.perl.org/bytes.html
}

method term:sym<print>($/) {
$V5DEBUG && say("term:sym<print>($/)");
my @args := $<args> ?? $<args>.ast.list !! [ QAST::Var.new( :name('$_'), :scope('lexical') ) ];
my $past := QAST::Op.new( :op('call'), :name('&print'),
|@args
);
$past.unshift( QAST::Var.new( :named('fh'), :name(~$<fh>), :scope('lexical') ) ) if $<fh>;
make $past
}

method term:sym<rand>($/) {
$V5DEBUG && say("term:sym<rand>($/)");
make QAST::Op.new( :op('call'), :name('&rand'), :node($/) );
Expand Down
3 changes: 3 additions & 0 deletions lib/Perl5/Grammar.nqp
Expand Up @@ -3381,6 +3381,9 @@ grammar Perl5::Grammar is HLL::Grammar does STD5 {
# token term:sym<oct>
# { <sym> » <?before \s*> <.ws> <EXPR('q=')>? }

token term:sym<print>
{ <sym> » <?before \s*> <.ws> [ <fh=.EXPR('z=')> <?before \s+> <.ws> <args=.EXPR('g=')> | <args=.EXPR('g=')> ]? }

# token term:sym<prototype>
# { <sym> » <?before \s*> <.ws> <EXPR('q=')>? }

Expand Down
9 changes: 9 additions & 0 deletions lib/Perl5/Terms.pm
Expand Up @@ -28,6 +28,15 @@ multi sub chomp(*@s is rw) is export {
$so_chomped
}

multi sub open( $fh is rw, $expr ) is export { open( $fh, $expr.substr(0, 1), $expr.substr(1) ) }
multi sub open( $fh is rw, $m, $expr, *@list ) is export {
# ($path, :r(:$r), :w(:$w), :a(:$a), :p(:$p), :bin(:$bin), :chomp(:$chomp) = { ... }, :enc(:encoding(:$encoding)) = { ... })
$fh = $expr.IO.open( :r($m eq '<'), :w($m eq '>'), :a($m eq '>>'), :p($m eq '|'), :bin(0) );
}

multi sub print( :$fh, *@text ) is export { ($fh || $*OUT).print( @text.join('') ) }

sub close( IO::Handle $fh ) is export { $fh.close }

sub ref($o) is export {
$o.^name
Expand Down

0 comments on commit eec4624

Please sign in to comment.