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