Skip to content

Commit

Permalink
Perlito5 - js - not() can have zero arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed May 11, 2015
1 parent d0ebfc9 commit 0ceb7b2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
8 changes: 6 additions & 2 deletions perlito5.pl
Expand Up @@ -1755,7 +1755,7 @@ sub Perlito5::Grammar::Expression::term_not {
}
}) && (')' eq substr($str, $MATCH->{'to'}, 1) && ($MATCH->{'to'} = 1 + $MATCH->{'to'})) && (do {
$MATCH->{'str'} = $str;
$MATCH->{'capture'} = ['term', Perlito5::AST::Apply->new('code' => 'prefix:<not>', 'arguments' => Perlito5::Match::flat($MATCH->{'paren_parse'}), 'namespace' => '')];
$MATCH->{'capture'} = ['term', Perlito5::AST::Apply->new('code' => 'prefix:<not>', 'arguments' => expand_list(Perlito5::Match::flat($MATCH->{'paren_parse'})), 'namespace' => '')];
1
})));
$tmp ? $MATCH : 0
Expand Down Expand Up @@ -9183,7 +9183,11 @@ package Perlito5::AST::Apply;
}, 'prefix:<not>' => sub {
my $self = shift;
my $level = shift;
'!( ' . Perlito5::Javascript2::to_bool($self->{'arguments'}->[0], $level) . ')'
my $arg = pop(@{$self->{'arguments'}});
if (!$arg) {
return 'true'
}
'!( ' . Perlito5::Javascript2::to_bool($arg, $level) . ')'
}, 'prefix:<~>' => sub {
my $self = $_[0];
'p5complement( ' . Perlito5::Javascript2::to_num($self->{'arguments'}->[0]) . ')'
Expand Down
2 changes: 1 addition & 1 deletion src5/lib/Perlito5/Grammar/Expression.pm
Expand Up @@ -401,7 +401,7 @@ token term_not {
$MATCH->{capture} = [ 'term',
Perlito5::AST::Apply->new(
code => 'prefix:<not>',
arguments => Perlito5::Match::flat($MATCH->{paren_parse}),
arguments => expand_list( Perlito5::Match::flat($MATCH->{paren_parse}) ),
namespace => '',
) ]
}
Expand Down
6 changes: 5 additions & 1 deletion src5/lib/Perlito5/Javascript2/Emitter.pm
Expand Up @@ -1715,7 +1715,11 @@ package Perlito5::AST::Apply;
'prefix:<not>' => sub {
my $self = shift;
my $level = shift;
'!( ' . Perlito5::Javascript2::to_bool( $self->{arguments}->[0], $level ) . ')';
my $arg = pop(@{$self->{arguments}});
if (!$arg) {
return 'true';
}
'!( ' . Perlito5::Javascript2::to_bool( $arg, $level ) . ')';
},
'prefix:<~>' => sub {
my $self = $_[0];
Expand Down
49 changes: 49 additions & 0 deletions t5/op/not.t
@@ -0,0 +1,49 @@
#!./perl -w

BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require './test.pl';
}

plan tests => 16;

# not() tests
pass() if not();
is(not(), 1);
is(not(), not(0));

# test not(..) and !
is(! 1, not 1);
is(! 0, not 0);
is(! (0, 0), not(0, 0));

# test the return of !
{
my $not0 = ! 0;
my $not1 = ! 1;

no warnings;
ok($not1 == undef);
ok($not1 == ());

use warnings;
ok($not1 eq '');
ok($not1 == 0);
ok($not0 == 1);
}

# test the return of not
{
my $not0 = not 0;
my $not1 = not 1;

no warnings;
ok($not1 == undef);
ok($not1 == ());

use warnings;
ok($not1 eq '');
ok($not1 == 0);
ok($not0 == 1);
}

0 comments on commit 0ceb7b2

Please sign in to comment.