member_at starting to work
[scpubgit/DX.git] / lib / DX / Scope.pm
CommitLineData
9d759b64 1package DX::Scope;
2
3use DX::Class;
4
5has predicates => (is => 'ro', required => 1);
6
7has globals => (is => 'ro', required => 1);
8
9has locals => (is => 'ro', required => 1);
10
efad53c4 11#has known_facts => (is => 'ro', required => 1);
9d759b64 12
13sub lookup_predicate {
14 my ($self, $predicate) = @_;
15 return $self->predicates->{$predicate} || die "No such predicate: $predicate";
16}
17
18sub lookup {
19 my ($self, $name) = @_;
20 my $lookup_in = ($name =~ /^[_A-Z]/ ? $self->locals->[-1] : $self->globals);
efad53c4 21 return $lookup_in->get_member_at($name)
22 or die "No such name in scope: $name";
9d759b64 23}
24
25sub depth { $#{$_[0]->locals} }
26
27sub prune_to {
28 my ($self, $to) = @_;
29 $self->but(locals => [ @{$self->locals}[0..$to] ]);
30}
31
32sub get_member_at {
33 my ($self, $at) = @_;
34 if ($at =~ /^[0-9]+$/) {
35 return $self->locals->[$at];
36 }
37 return $self->globals->get_member_at($at);
38}
39
40sub with_member_at {
41 my ($self, $at, $value) = @_;
42 if ($at =~ /^[0-9]+$/) {
43 my @locals = @{$self->locals};
efad53c4 44 $locals[$at] = $value;
9d759b64 45 return $self->but(
46 locals => \@locals
47 );
48 }
49 return $self->but(
50 globals => $self->globals->with_member_at($at, $value)
51 );
52}
53
0498469a 54sub apply_updates {
55 my ($self, @updates) = @_;
56 my @events;
57 my $scope = $self;
58 ($scope, @events) = ($_->apply_to($scope), @events) for @updates;
59 return ($scope, @events);
60}
61
9d759b64 621;