| 1 | package Lox::Environment; | ||||||||||
| 2 | 260 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 260 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 260 | our $VERSION = 0.02; | |||||||||
| 5 | |||||||||||
| 6 | 885 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 7 | 885 | my ($class, $args) = @_; | |||||||||
| 8 | 885 | return bless { | |||||||||
| 9 | values => {}, | ||||||||||
| 10 | %$args, | ||||||||||
| 11 | }, $class; | ||||||||||
| 12 | } | ||||||||||
| 13 | |||||||||||
| 14 | 351 | ▶ | sub enclosing { $_[0]->{enclosing} } | ||||||||
|
Subroutine
enclosing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 15 | 3763 | ▶ | sub values { $_[0]->{values} } | ||||||||
|
Subroutine
values: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 16 | |||||||||||
| 17 | 1096 | ▶ | sub define { | ||||||||
|
Subroutine
define: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 18 | 1096 | my ($self, $name, $value) = @_; | |||||||||
| 19 | 1096 | $self->values->{$name} = $value; | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 1215 | ▶ | sub ancestor { | ||||||||
|
Subroutine
ancestor: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 23 | 1215 | my ($self, $distance) = @_; | |||||||||
| 24 | 1215 | my $environment = $self; | |||||||||
| 25 | 1215 | for (1..$distance) { | |||||||||
| 26 | 320 | $environment = $environment->enclosing; | |||||||||
| 27 | } | ||||||||||
| 28 | 1215 | return $environment; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 1193 | ▶ | sub get_at { | ||||||||
|
Subroutine
get_at: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 32 | 1193 | my ($self, $distance, $token_lexeme) = @_; | |||||||||
| 33 | 1193 | return $self->ancestor($distance)->values->{$token_lexeme}; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 22 | ▶ | sub assign_at { | ||||||||
|
Subroutine
assign_at: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | 22 | my ($self, $distance, $token, $value) = @_; | |||||||||
| 38 | 22 | $self->ancestor($distance)->values->{$token->lexeme} = $value; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 607 | ▶ | sub get { | ||||||||
|
Subroutine
get: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||
| 42 | 607 | my ($self, $token) = @_; | |||||||||
| 43 | 607 | ▶ | if (exists $self->values->{$token->lexeme}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 600 | my $v = $self->values->{$token->lexeme}; | |||||||||
| 45 | 600 | ▶ | return $v if defined $v; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 2 | Lox::runtime_error($token, sprintf 'Uninitialized variable \'%s\'', $token->lexeme); | |||||||||
| 47 | } | ||||||||||
| 48 | 7 | ▶ | if ($self->enclosing) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | return $self->enclosing->get($token); | |||||||||
| 50 | } | ||||||||||
| 51 | 7 | Lox::runtime_error($token, sprintf 'Undefined variable \'%s\'', $token->lexeme); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 123 | ▶ | sub assign { | ||||||||
|
Subroutine
assign: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||
| 55 | 123 | my ($self, $token, $value) = @_; | |||||||||
| 56 | 123 | ▶ | if (exists $self->values->{$token->lexeme}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 122 | $self->values->{$token->lexeme} = $value; | |||||||||
| 58 | 122 | return; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 1 | ▶ | if ($self->enclosing) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | return $self->enclosing->assign($token, $value); | |||||||||
| 63 | } | ||||||||||
| 64 | 1 | Lox::runtime_error($token, sprintf 'Undefined variable "%s"', $token->lexeme); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 260 | 1; | |||||||||