diff --git a/MANIFEST b/MANIFEST index 7f681ff16..6a39910fa 100644 --- a/MANIFEST +++ b/MANIFEST @@ -57,7 +57,6 @@ lib/Sidef/Types/Block/Fork.pm lib/Sidef/Types/Block/Fork.pod lib/Sidef/Types/Block/Given.pm lib/Sidef/Types/Block/Given.pod -lib/Sidef/Types/Block/Loop.pm lib/Sidef/Types/Block/Next.pm lib/Sidef/Types/Block/Next.pod lib/Sidef/Types/Block/Return.pm diff --git a/META.json b/META.json index 14e83cbc1..6d0416e6c 100644 --- a/META.json +++ b/META.json @@ -158,9 +158,6 @@ "Sidef::Types::Block::Given" : { "file" : "lib/Sidef/Types/Block/Given.pm" }, - "Sidef::Types::Block::Loop" : { - "file" : "lib/Sidef/Types/Block/Loop.pm" - }, "Sidef::Types::Block::Next" : { "file" : "lib/Sidef/Types/Block/Next.pm" }, diff --git a/META.yml b/META.yml index e4223dfd9..5abe1ff2d 100644 --- a/META.yml +++ b/META.yml @@ -84,8 +84,6 @@ provides: file: lib/Sidef/Types/Block/Fork.pm Sidef::Types::Block::Given: file: lib/Sidef/Types/Block/Given.pm - Sidef::Types::Block::Loop: - file: lib/Sidef/Types/Block/Loop.pm Sidef::Types::Block::Next: file: lib/Sidef/Types/Block/Next.pm Sidef::Types::Block::PerlCode: diff --git a/bin/sidef b/bin/sidef index f01e66420..24a078863 100755 --- a/bin/sidef +++ b/bin/sidef @@ -109,9 +109,11 @@ if (defined $args{t}) { my $parser = new_parser(name => $script_name); my $struct = eval { parse_code($parser, $code) }; + my $slept = 0; if ($@) { - warn "[ERROR] Can't parse the script `$script_name`: $@\n"; + warn "[ERROR] Can't parse the script `$script_name`: $@"; sleep 2; + $slept = 1; } else { if (ref($struct) eq 'HASH') { @@ -122,9 +124,9 @@ if (defined $args{t}) { } } - if ($@ or (ref($struct) ne 'HASH' and $?)) { - warn "[ERROR] Error encountered on script `$script_name': $@\n"; - sleep 2; + if (not($slept) and ($@ or (ref($struct) ne 'HASH' and $?))) { + warn "[ERROR] Error encountered on script `$script_name': $@"; + sleep(2); } if (@argv) { @@ -779,7 +781,7 @@ HEAD close $fh; } - } + } } => $path ); diff --git a/lib/Sidef/Parser.pm b/lib/Sidef/Parser.pm index 70eeb69aa..0fb04636f 100644 --- a/lib/Sidef/Parser.pm +++ b/lib/Sidef/Parser.pm @@ -101,11 +101,9 @@ package Sidef::Parser { | return\b (?{ Sidef::Types::Block::Return->new }) | next\b (?{ Sidef::Types::Block::Next->new }) | break\b (?{ Sidef::Types::Block::Break->new }) - | try\b (?{ Sidef::Types::Block::Try->new }) | (?:given|switch)\b (?{ Sidef::Types::Block::Given->new }) | f?require\b (?{ state $x = Sidef::Module::Require->new }) | (?:(?:print(?:ln)?+|say|read)\b|>>?) (?{ state $x = Sidef::Sys::Sys->new }) - | loop\b (?{ state $x = Sidef::Types::Block::Loop->new }) | (?:[*\\&]|\+\+|--|lvalue\b) (?{ Sidef::Variable::Ref->new }) | [?√+~!-] (?{ state $x = Sidef::Object::Unary->new }) | : (?{ state $x = Sidef::Types::Hash::Hash->new }) @@ -1682,10 +1680,25 @@ package Sidef::Parser { } # Method call in functional style - if (not $self->{_want_name} and ($class eq $self->{class} or $class eq 'CORE') and /\G(?=\()/) { - my $arg = $self->parse_arguments(code => $opt{code}); + if (not $self->{_want_name} and ($class eq $self->{class} or $class eq 'CORE') and /\G\h*(?=[({])/gc) { + my $arg = ( + /\G(?=\()/ + ? $self->parse_arguments(code => $opt{code}) + : $self->parse_block(code => $opt{code}) + ); - if (exists $arg->{$self->{class}}) { + if (ref($arg) eq 'Sidef::Types::Block::Code') { + return + scalar { + $self->{class} => [ + { + self => $arg, + call => [{method => $name}] + } + ] + }; + } + elsif (exists $arg->{$self->{class}}) { return scalar { $self->{class} => [ { diff --git a/lib/Sidef/Types/Block/Code.pm b/lib/Sidef/Types/Block/Code.pm index 825cd7fac..9c7334ede 100644 --- a/lib/Sidef/Types/Block/Code.pm +++ b/lib/Sidef/Types/Block/Code.pm @@ -173,6 +173,24 @@ package Sidef::Types::Block::Code { $self; } + sub try { + my ($self) = @_; + + my $try = Sidef::Types::Block::Try->new(); + + my $error = 0; + local $SIG{__WARN__} = sub { $try->{type} = 'warning'; $try->{msg} = $_[0]; $error = 1 }; + local $SIG{__DIE__} = sub { $try->{type} = 'error'; $try->{msg} = $_[0]; $error = 1 }; + + $try->{val} = eval { $self->run }; + + if ($@ || $error) { + $try->{catch} = 1; + } + + $try; + } + { my $check_type = sub { my ($var, $value) = @_; diff --git a/lib/Sidef/Types/Block/Loop.pm b/lib/Sidef/Types/Block/Loop.pm deleted file mode 100644 index 9a454a84e..000000000 --- a/lib/Sidef/Types/Block/Loop.pm +++ /dev/null @@ -1,12 +0,0 @@ -package Sidef::Types::Block::Loop { - - sub new { - bless {}, __PACKAGE__; - } - - sub loop { - $_[1]->loop; - } -}; - -1 diff --git a/lib/Sidef/Types/Block/Try.pm b/lib/Sidef/Types/Block/Try.pm index 110a8591d..f2a684cd8 100644 --- a/lib/Sidef/Types/Block/Try.pm +++ b/lib/Sidef/Types/Block/Try.pm @@ -7,22 +7,6 @@ package Sidef::Types::Block::Try { bless {catch => 0}, __PACKAGE__; } - sub try { - my ($self, $code) = @_; - - my $error = 0; - local $SIG{__WARN__} = sub { $self->{type} = 'warning'; $self->{msg} = $_[0]; $error = 1 }; - local $SIG{__DIE__} = sub { $self->{type} = 'error'; $self->{msg} = $_[0]; $error = 1 }; - - $self->{val} = eval { $code->run }; - - if ($@ || $error) { - $self->{catch} = 1; - } - - $self; - } - sub catch { my ($self, $code) = @_;