move return stack code back to test
[scpubgit/DKit.git] / lib / DX / State.pm
CommitLineData
60cda014 1package DX::State;
2
5622b4df 3use Return::MultiLevel qw(with_return);
4use DX::Op::FromCode;
60cda014 5use Moo;
6
7has next_op => (is => 'ro', required => 1);
8
9has return_stack => (is => 'ro', required => 1);
10
11has by_id => (is => 'ro', required => 1);
12
13has scope => (is => 'ro', required => 1);
14
15has last_choice => (is => 'ro', required => 1);
16
17sub scope_var {
18 my ($self, $name) = @_;
19 $self->by_id->{$self->scope->{$name}};
20}
21
22sub bind_stream_then {
23 my ($self, $var, $stream, $then) = @_;
24 warn "Binding ".$var->id." to $stream";
25 my $bound = $var->with_stream($stream);
26 $self->new(%$self,
27 by_id => { %{$self->by_id}, $var->id => $bound },
28 next_op => $then
29 )->mark_choice($bound);
30}
31
32sub mark_choice {
33 my ($self, $var) = @_;
34 $self->new(%$self,
35 last_choice => [ $self, $var ]
36 );
37}
38
39sub backtrack {
40 my ($self) = @_;
94565614 41 my ($state, $var) = ($self);
42 while (($state, $var) = @{$state->last_choice}) {
60cda014 43 $var->bound_value; $var->clear_bound_value;
94565614 44 return $state->mark_choice($var) unless $var->bound_stream->is_exhausted;
60cda014 45 }
5622b4df 46 if (our $No_Options_Handler) {
47 $No_Options_Handler->(undef);
48 }
60cda014 49 die "Out of options";
50}
51
52sub then {
53 my ($self, $then) = @_;
54 $self->new(%$self, next_op => $then);
55}
56
94565614 57sub run {
58 my ($state) = @_;
5622b4df 59 with_return {
60 my ($return) = @_;
61 local our $No_Options_Handler = $return;
54817920 62 while (my $op = $state->next_op) {
5622b4df 63 $state = $op->run($state);
64 }
65 return $state;
94565614 66 }
5622b4df 67}
68
69sub push_backtrack {
70 $_[0]->then(DX::Op::FromCode->new(code => sub { $_[1]->backtrack }));
94565614 71}
72
60cda014 731;