Skip to content

Commit

Permalink
- Extented the syntax for prefix method calls even further. Now a met…
Browse files Browse the repository at this point in the history
…hod followed by a Block of code is also supported. This simplified the parser a little bit, by removing the `loop` and `try` built-in keywords.

Example:
	"loop {...}" is really parsed as "{...}.loop"

This is true for all block objects.

Example:
	do { ... };	# equivalent with: {...}.do;

This syntax now works:

	var i = 1
	do {
	    say i++;
	} while {i <= 10};

More examples:

	try { ... } catch { ... };	# is parsed as: { ... }.try.catch{ ... };
	fork { ... }.wait;		# is parsed as: { ... }.fork.wait;
  • Loading branch information
trizen committed Jul 21, 2015
1 parent a2aecd6 commit 3828759
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 44 deletions.
1 change: 0 additions & 1 deletion MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 0 additions & 2 deletions META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 7 additions & 5 deletions bin/sidef
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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) {
Expand Down Expand Up @@ -779,7 +781,7 @@ HEAD

close $fh;
}
}
}
} => $path
);

Expand Down
23 changes: 18 additions & 5 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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} => [
{
Expand Down
18 changes: 18 additions & 0 deletions lib/Sidef/Types/Block/Code.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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) = @_;
Expand Down
12 changes: 0 additions & 12 deletions lib/Sidef/Types/Block/Loop.pm

This file was deleted.

16 changes: 0 additions & 16 deletions lib/Sidef/Types/Block/Try.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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) = @_;

Expand Down

0 comments on commit 3828759

Please sign in to comment.