Skip to content

Commit

Permalink
- Improved the next loop controller. Now it can take an argument th…
Browse files Browse the repository at this point in the history
…at represent the depth of nexting (default: 1).

Example:
	for (1..5) { |i|
	    for (1..5) { |j|
	        say "#{i} #{j}";
	        next(2);
	    };
	    say i;      # never get's printed
	}

Output:
1 1
2 1
3 1
4 1
5 1
  • Loading branch information
trizen committed Jun 20, 2015
1 parent 99d875c commit b9b242e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ package Sidef::Parser {
nil\b (?{ state $x = Sidef::Types::Nil::Nil->new })
| true\b (?{ state $x = Sidef::Types::Bool::Bool->true })
| (?:false|Bool)\b (?{ state $x = Sidef::Types::Bool::Bool->false })
| next\b (?{ state $x = Sidef::Types::Block::Next->new })
| continue\b (?{ state $x = Sidef::Types::Block::Continue->new })
| BlackHole\b (?{ state $x = Sidef::Types::Black::Hole->new })
| Block\b (?{ state $x = Sidef::Types::Block::Code->new })
Expand Down Expand Up @@ -100,6 +99,7 @@ package Sidef::Parser {
| while\b (?{ Sidef::Types::Bool::While->new })
| for(?:each)?+\b (?{ Sidef::Types::Block::For->new })
| 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 })
Expand Down
3 changes: 3 additions & 0 deletions lib/Sidef/Types/Block/Code.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ package Sidef::Types::Block::Code {
: ref($result) eq 'Sidef::Types::Block::Break' ? --$result->{depth} <= 0
? $self
: $result
: ref($result) eq 'Sidef::Types::Block::Next' ? --$result->{depth} <= 0
? ()
: $result
: ();
}

Expand Down
6 changes: 4 additions & 2 deletions lib/Sidef/Types/Block/Next.pm
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package Sidef::Types::Block::Next {

sub new {
bless {}, __PACKAGE__;
bless {depth => 1}, __PACKAGE__;
}

sub next {
$_[0];
my ($self, $depth) = @_;
$self->{depth} = ref($depth) ? $depth->get_value : 1;
$self;
}

}
Expand Down
5 changes: 1 addition & 4 deletions scripts/Expensive/universal_turing_machine.sf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func run_utm(state="", blank="", rules=[], tape=[], halt="", pos=0) {
break;
}

var ok = false;
rules.each { |rule|
var (s0, v0, v1, dir, s1) = rule...;
if ((s0 != state) || (tape[pos] != v0)) {
Expand All @@ -50,11 +49,9 @@ func run_utm(state="", blank="", rules=[], tape=[], halt="", pos=0) {
};

state = s1;
ok = true;
break;
next(2);
};

ok && next;
die 'No matching rules';
}
}
Expand Down

0 comments on commit b9b242e

Please sign in to comment.