| 1 | package Lox::Function; | ||||||||||
| 2 | 260 | ▶ | use parent 'Lox::Callable'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 260 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 260 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 260 | ▶ | use Lox::Bool; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 260 | ▶ | use Lox::Environment; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | use overload | ||||||||||
| 8 | 3 | ▶ | '""' => sub { sprintf '<fn %s>', $_[0]->declaration->can('name') | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 9 | ? $_[0]->declaration->name->lexeme : 'lambda' }, | ||||||||||
| 10 | 1 | ▶ | '!' => sub { $False }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 183 | ▶ | 'bool' => sub { $True }, # only false and nil are untrue in Lox | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 260 | fallback => 0; | |||||||||
| 13 | |||||||||||
| 14 | 260 | our $VERSION = 0.02; | |||||||||
| 15 | |||||||||||
| 16 | 258 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 17 | 258 | my ($class, $args) = @_; | |||||||||
| 18 | 258 | return bless { %$args }, $class; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 2251 | ▶ | sub declaration { $_[0]->{declaration} } | ||||||||
|
Subroutine
declaration: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 22 | 526 | ▶ | sub closure { $_[0]->{closure} } | ||||||||
|
Subroutine
closure: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 23 | 456 | ▶ | sub arity { scalar $_[0]->params->@* } | ||||||||
|
Subroutine
arity: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | 1732 | ▶ | sub params { $_[0]->declaration->params } | ||||||||
|
Subroutine
params: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | 436 | ▶ | sub body { $_[0]->declaration->body } | ||||||||
|
Subroutine
body: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 26 | |||||||||||
| 27 | 436 | ▶ | sub call { | ||||||||
|
Subroutine
call: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 28 | 436 | my ($self, $interpreter, $args) = @_; | |||||||||
| 29 | 436 | my $environment = Lox::Environment->new({ enclosing => $self->closure }); | |||||||||
| 30 | 436 | ▶ | for (my $i = 0; $i < $self->params->@*; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 420 | $environment->define($self->params->[$i]->lexeme,$args->[$i]); | |||||||||
| 32 | } | ||||||||||
| 33 | 436 | ▶ | my $sub = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 436 | $interpreter->execute_block($self->body, $environment); | |||||||||
| 35 | 436 | }; | |||||||||
| 36 | 436 | my $retval = $self->call_catch_return($interpreter, $sub); | |||||||||
| 37 | 430 | ▶ | return $self->{is_initializer} ? $self->closure->get_at(0, 'this') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | : $retval; | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 78 | ▶ | sub bind { | ||||||||
|
Subroutine
bind: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 42 | 78 | my ($self, $instance) = @_; | |||||||||
| 43 | 78 | my $environment = Lox::Environment->new({ enclosing => $self->closure }); | |||||||||
| 44 | 78 | $environment->define('this', $instance); | |||||||||
| 45 | return Lox::Function->new({ | ||||||||||
| 46 | is_initializer => $self->{is_initializer}, | ||||||||||
| 47 | 78 | declaration => $self->declaration, | |||||||||
| 48 | closure => $environment, | ||||||||||
| 49 | }); | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 260 | 1; | |||||||||