-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added support for implicit block calls.
Example: f(a,b) {...}; # means: f(a, b, {...}); f {...}; # means: f({...}); This new syntax allows the user to extend the language freely, creating new constructs using blocks, classes, methods and functions. See: `scripts/block_implicit_call.sf` for examples. However, this new feature comes at a cost, which is that it will always require parentheses around if/while/for conditions. Example: if true { ... }; # this used to work if (true) { ... }; # this works Anyway, the parentheses make the code much easier to read and to reason about, so I think it's a good change of the syntax. It, also, makes the language more consistent.
- Loading branch information
trizen
committed
Sep 20, 2015
1 parent
f46fe9b
commit a0c1e81
Showing
37 changed files
with
335 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,41 @@ | ||
package Sidef::Types::Block::For { | ||
|
||
use 5.014; | ||
|
||
sub new { | ||
bless {}, __PACKAGE__; | ||
} | ||
|
||
sub for { | ||
my ($self, @args) = @_; | ||
$self->{arg} = \@args; | ||
$self; | ||
} | ||
|
||
sub foreach { | ||
my ($self, $arr) = @_; | ||
$self->{arg} = $arr; | ||
$self; | ||
if ( $#args == 3 | ||
and ref($args[0]) eq 'Sidef::Types::Block::Code' | ||
and ref($args[1]) eq 'Sidef::Types::Block::Code' | ||
and ref($args[2]) eq 'Sidef::Types::Block::Code') { | ||
my ($one, $two, $three) = @args[0 .. 2]; | ||
for ($one->_execute_expr ; $two->_execute_expr ; $three->_execute_expr) { | ||
if (defined(my $res = $args[3]->_run_code)) { | ||
return $res; | ||
} | ||
} | ||
$args[-1]; | ||
} | ||
elsif ($#args == 1 and $args[0]->can('each')) { | ||
$args[0]->each($args[1]); | ||
} | ||
else { | ||
my $block = pop @args; | ||
foreach my $item (@args) { | ||
if (defined(my $res = $block->_run_code($item))) { | ||
return $res; | ||
} | ||
} | ||
$block; | ||
} | ||
} | ||
|
||
sub do { | ||
my ($self, $code) = @_; | ||
ref($self->{arg}) eq 'ARRAY' | ||
? $code->for(@{$self->{arg}}) | ||
: $self->{arg}->each($code); | ||
} | ||
*foreach = \&for; | ||
}; | ||
|
||
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.