File Coverage

blib/lib/Lox/Class.pm
Criterion Covered Total %
statement 18 40 45.0
branch 0 8 0.0
condition n/a
subroutine 6 16 37.5
pod 0 7 0.0
total 24 71 33.8


line stmt bran cond sub pod time code
1             package Lox::Class;
2 1     1   5 use Lox::Bool;
  1         2  
  1         112  
3             use overload (
4 0     0   0 '""' => sub { $_[0]->name },
5 0     0   0 'bool' => sub { $True },
6 0     0   0 '!' => sub { $False },
7 1         7 fallback => 0,
8 1     1   6 );
  1         1  
9 1     1   84 use parent 'Lox::Callable';
  1         2  
  1         3  
10 1     1   41 use strict;
  1         1  
  1         26  
11 1     1   5 use warnings;
  1         1  
  1         27  
12 1     1   360 use Lox::Instance;
  1         2  
  1         272  
13             our $VERSION = 0.02;
14              
15             sub new {
16 0     0 0   my ($class, $args) = @_;
17 0           return bless { %$args }, $class;
18             }
19              
20 0     0 0   sub superclass { $_[0]->{superclass} }
21 0     0 0   sub methods { $_[0]->{methods} }
22 0     0 0   sub name { $_[0]->{name} }
23              
24             sub call {
25 0     0 0   my ($self, $interpreter, @args) = @_;
26 0           my $instance = Lox::Instance->new({ klass => $self, fields => {} });
27 0           my $initializer = $self->find_method('init');
28 0 0         if ($initializer) {
29 0           $initializer->bind($instance)->call($interpreter, @args);
30             }
31 0           return $instance;
32             }
33              
34             sub arity {
35 0     0 0   my $self = shift;
36 0           my $initializer = $self->find_method('init');
37 0 0         return $initializer ? $initializer->arity : 0;
38             }
39              
40             sub find_method {
41 0     0 0   my ($self, $lexeme) = @_;
42 0 0         if (my $method = $self->methods->{$lexeme}) {
    0          
43 0           return $method;
44             }
45             elsif ($self->superclass) {
46 0           return $self->superclass->find_method($lexeme);
47             }
48 0           return undef;
49             }
50              
51             1;