| 1 | package Lox::Interpreter; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 260 | ▶ | use feature 'say'; | ||||||||||||||||||||||||||||||||||||||||||||
|
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::Callable; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 260 | ▶ | use Lox::Environment; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 260 | ▶ | use Lox::Function; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 260 | ▶ | use Lox::Class; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 260 | ▶ | use Lox::Nil; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 260 | ▶ | use Lox::TokenType; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 260 | our $VERSION = 0.02; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 260 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 260 | my ($class, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 260 | ▶ | $args //= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args //= {}
MC/DC0%0/1
$args //= {}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 260 | my $globals = Lox::Environment->new({}); | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 260 | my $interpreter = bless { | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | environment => $globals, | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | globals => $globals, | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | locals => {}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | %$args, | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | $interpreter->globals->define('clock', Lox::Callable->new({ | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | arity => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | call => sub { time }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 260 | })); | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 260 | return $interpreter; | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 3549 | ▶ | sub environment :lvalue { $_[0]->{environment} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
environment: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 905 | ▶ | sub globals { $_[0]->{globals} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
globals: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 2197 | ▶ | sub locals { $_[0]->{locals} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
locals: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 202 | ▶ | sub interpret { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
interpret: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 202 | my ($self, $stmts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 202 | for (@$stmts) { | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 641 | $self->execute($_); | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1850 | ▶ | sub execute { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 1850 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1850 | ▶ | $stmt->accept($self) unless $self->{breaking}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 363 | ▶ | sub resolve { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 363 | my ($self, $expr, $depth) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 363 | $self->locals->{"$expr"} = $depth; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub visit_break_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_break_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | $self->{breaking}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 88 | ▶ | sub visit_class_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_class_stmt: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 88 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 88 | my $superclass = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 88 | ▶ | if (my $sc = $stmt->superclass) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 26 | $superclass = $self->evaluate($sc); | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 26 | ▶ | unless (ref $superclass eq 'Lox::Class') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 3 | Lox::runtime_error($sc->name, 'Superclass must be a class'); | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 85 | $self->environment->define($stmt->name->lexeme, undef); | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 85 | ▶ | if ($superclass) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 23 | $self->environment = Lox::Environment->new({ enclosing => $self->environment }); | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 23 | $self->environment->define('super', $superclass); | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 85 | my %methods; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 85 | for my $method ($stmt->methods->@*) { | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 84 | my $function = Lox::Function->new({ | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | is_initializer => $method->name->lexeme eq 'init', | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | declaration => $method, | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | closure => $self->environment, | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 84 | $methods{$method->name->lexeme} = $function; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 85 | my $klass = Lox::Class->new({ | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | superclass => $superclass, | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | methods => \%methods, | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | name => $stmt->name->lexeme, | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 85 | ▶ | if ($superclass) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 23 | $self->environment = $self->environment->enclosing; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 85 | $self->environment->assign($stmt->name, $klass); | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 85 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 276 | ▶ | sub visit_expression_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_expression_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 276 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | 276 | $self->evaluate($stmt->expression); | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 217 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 323 | ▶ | sub visit_if_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_if_stmt: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 323 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | 323 | ▶ | if ($self->is_truthy($self->evaluate($stmt->condition))) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 155 | $self->execute($stmt->then_branch); | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | elsif ($stmt->else_branch) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 13 | $self->execute($stmt->else_branch); | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 77 | ▶ | sub visit_function_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_function_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 77 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 77 | my $function = Lox::Function->new({ | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | declaration => $stmt, | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | closure => $self->environment, | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 77 | $self->environment->define($stmt->name->lexeme, $function); | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | 77 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 19 | ▶ | sub visit_function_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_function_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 19 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | 19 | return Lox::Function->new({ | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | declaration => $expr, | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | closure => $self->environment, | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 28 | ▶ | sub visit_logical_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_logical_expr: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 28 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | 28 | my $left = $self->evaluate($expr->left); | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | 28 | ▶ | if ($expr->operator->type == OR) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 14 | ▶ | return $left if $self->is_truthy($left); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 14 | ▶ | return $left if !$self->is_truthy($left); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 19 | return $self->evaluate($expr->right); | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 106 | ▶ | sub visit_set_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_set_expr: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 106 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 106 | my $object = $self->evaluate($expr->object); | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | 105 | ▶ | if (ref $object ne 'Lox::Instance') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 6 | Lox::runtime_error($expr->name, "Only instances have fields"); | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 99 | my $value = $self->evaluate($expr->value); | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | 99 | $object->set($expr->name, $value); | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | 99 | return $value | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 14 | ▶ | sub visit_super_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_super_expr: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 14 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | 14 | my $distance = $self->locals->{"$expr"}; | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | 14 | my $superclass = $self->environment->get_at($distance, 'super'); | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | 14 | my $object = $self->environment->get_at($distance - 1, 'this'); | |||||||||||||||||||||||||||||||||||||||||||||
| 162 | 14 | my $method = $superclass->find_method($expr->method->lexeme); | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 14 | ▶ | unless ($method) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | Lox::runtime_error($expr->method, | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | sprintf 'Undefined property \'%s\'', $expr->method->lexeme); | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 13 | return $method->bind($object); | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 25 | ▶ | sub visit_this_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_this_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 25 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | 25 | return $self->look_up_variable($expr); | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 463 | ▶ | sub visit_print_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_print_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 463 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 463 | my $value = $self->evaluate($stmt->expression); | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | 458 | say $self->stringify($value); | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | 458 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 350 | ▶ | sub visit_return_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_return_stmt: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 350 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 350 | ▶ | if ($stmt->value) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 345 | $self->{returning} = $self->evaluate($stmt->value); | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 348 | die "return\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 156 | ▶ | sub visit_var_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_var_stmt: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 156 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 156 | my $value = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 156 | ▶ | if ($stmt->initializer) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 139 | $value = $self->evaluate($stmt->initializer); | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 153 | $self->environment->define($stmt->name->{lexeme}, $value); | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | 153 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 26 | ▶ | sub visit_while_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_while_stmt: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 26 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | 26 | ▶ | while ($self->is_truthy($self->evaluate($stmt->condition))) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 35 | $self->execute($stmt->body); | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | 28 | ▶ | last if $self->{breaking}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 19 | return undef $self->{breaking}; | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 88 | ▶ | sub visit_block_stmt { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_block_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 88 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 88 | $self->execute_block( | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | $stmt->statements, | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | Lox::Environment->new({ enclosing => $self->environment })); | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 78 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 524 | ▶ | sub execute_block { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute_block: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 524 | my ($self, $statements, $environment) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 221 | 524 | my $prev_environment = $self->environment; | |||||||||||||||||||||||||||||||||||||||||||||
| 222 | 524 | $self->environment = $environment; | |||||||||||||||||||||||||||||||||||||||||||||
| 223 | 524 | my $error; | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | 524 | for my $stmt (@$statements) { | |||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1006 | eval { $self->execute($stmt) }; # so we can reset the env | |||||||||||||||||||||||||||||||||||||||||||||
| 226 | 997 | ▶ | if ($error = $@) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 355 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 515 | $self->environment = $prev_environment; | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | 515 | ▶ | die $error if $error; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 160 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 1392 | ▶ | sub visit_literal_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_literal_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 1392 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 1392 | return $expr->value; | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 500 | ▶ | sub visit_call_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_call_expr: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 500 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | 500 | my $callee = $self->evaluate($expr->callee); | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | 496 | my @args; | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | 496 | for my $arg ($expr->arguments->@*) { | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | 448 | push @args, $self->evaluate($arg); | |||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 496 | ▶ | unless (ref $callee && $callee->isa('Lox::Callable')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $callee and $callee->isa("Lox::Callable")
MC/DC100%2/2
ref $callee and $callee->isa("Lox::Callable")
ref $callee
$callee->isa("Lox::Callable")
Truth table100%3/3
A and B
A= ref $calleeB= $callee->isa("Lox::Callable")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 6 | Lox::runtime_error($expr->paren, 'Can only call functions and classes'); | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 490 | ▶ | if (@args!= $callee->arity) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 11 | Lox::runtime_error($expr->paren, | |||||||||||||||||||||||||||||||||||||||||||||
| 253 | sprintf 'Expected %d arguments but got %s',$callee->arity,scalar @args); | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 479 | ▶ | return $callee->call($self, \@args) // $Nil; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$callee->call($self, \@args) // $Lox::Interpreter::Nil
MC/DC0%0/2
$callee->call($self, \@args) // $Lox::Interpreter::Nil
$callee->call($self, \@args)
$Lox::Interpreter::Nil
Truth table66%2/3
A // B
A= $callee->call($self, \@args)B= $Lox::Interpreter::Nil
| |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 163 | ▶ | sub visit_get_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_get_expr: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 163 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 260 | 163 | my $object = $self->evaluate($expr->object); | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 163 | ▶ | if (ref $object eq 'Lox::Instance') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 157 | return $object->get($expr->name); | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 6 | Lox::runtime_error($expr->name, 'Only instances have properties'); | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 10 | ▶ | sub visit_grouping_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_grouping_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 10 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | 10 | return $self->evaluate($expr->expression); | |||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 32 | ▶ | sub visit_unary_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_unary_expr: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 32 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | 32 | my $right = $self->evaluate($expr->right); | |||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 32 | ▶ | if ($expr->operator->{type} == MINUS) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # numbers are not objects | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 17 | ▶ | Lox::runtime_error($expr->operator, 'Operand must be a number') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | if ref $right; | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 16 | return -$right; | |||||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 15 | ▶ | return !($self->is_truthy($right) ? $True : $False); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 60 | ▶ | sub visit_assign_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_assign_expr: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 60 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | 60 | my $value = $self->evaluate($expr->value); | |||||||||||||||||||||||||||||||||||||||||||||
| 291 | 60 | my $distance = $self->locals->{"$expr"}; | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | 60 | ▶ | if (defined $distance) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 22 | $self->environment->assign_at($distance, $expr->name, $value); | |||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 295 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 38 | $self->globals->assign($expr->name, $value); | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 59 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 1735 | ▶ | sub visit_variable_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_variable_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 1735 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 303 | 1735 | return $self->look_up_variable($expr); | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 1760 | ▶ | sub look_up_variable { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
look_up_variable: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 1760 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 308 | 1760 | my $distance = $self->locals->{"$expr"}; | |||||||||||||||||||||||||||||||||||||||||||||
| 309 | 1760 | ▶ | return defined $distance | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | ? $self->environment->get_at($distance, $expr->name->lexeme) | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | : $self->globals->get($expr->name); | ||||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 995 | ▶ | sub visit_binary_expr { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit_binary_expr: called CC=36 SCAR=35.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 995 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | 995 | my $left = $self->evaluate($expr->left); | |||||||||||||||||||||||||||||||||||||||||||||
| 317 | 995 | my $right = $self->evaluate($expr->right); | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 995 | my $type = $expr->operator->{type}; | |||||||||||||||||||||||||||||||||||||||||||||
| 320 | 995 | ▶ | if ($type == EQUAL_EQUAL) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 10 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 64 | ▶ | return $self->are_equal($left, $right) ? $True : $False; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | elsif ($type == BANG_EQUAL) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 21 | ▶ | return !$self->are_equal($left, $right) ? $True : $False; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | elsif ($type == GREATER) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | # numbers are not objects | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 9 | ▶ | Lox::runtime_error($expr->operator, 'Operands must be two numbers') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $left or ref $right
MC/DC100%2/2
ref $left or ref $right
ref $left
ref $right
Truth table100%3/3
A or B
A= ref $leftB= ref $right
| |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | if ref $left || ref $right; | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 7 | ▶ | return $left > $right ? $True : $False; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 332 | elsif ($type == GREATER_EQUAL) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 11 | ▶ | Lox::runtime_error($expr->operator, 'Operands must be two numbers') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $left or ref $right
MC/DC100%2/2
ref $left or ref $right
ref $left
ref $right
Truth table100%3/3
A or B
A= ref $leftB= ref $right
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | if ref $left || ref $right; | ||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 9 | ▶ | return $left >= $right ? $True : $False; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 337 | elsif ($type == LESS) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 311 | ▶ | Lox::runtime_error($expr->operator, 'Operands must be two numbers') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $left or ref $right
MC/DC100%2/2
ref $left or ref $right
ref $left
ref $right
Truth table100%3/3
A or B
A= ref $leftB= ref $right
| |||||||||||||||||||||||||||||||||||||||||||||||
| 339 | if ref $left || ref $right; | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 309 | ▶ | return $left < $right ? $True : $False; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | elsif ($type == LESS_EQUAL) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 8 | ▶ | Lox::runtime_error($expr->operator, 'Operands must be two numbers') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $left or ref $right
MC/DC100%2/2
ref $left or ref $right
ref $left
ref $right
Truth table100%3/3
A or B
A= ref $leftB= ref $right
| |||||||||||||||||||||||||||||||||||||||||||||||
| 344 | if ref $left || ref $right; | ||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 6 | ▶ | return $left <= $right ? $True : $False; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 347 | elsif ($type == MINUS) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 289 | ▶ | Lox::runtime_error($expr->operator, 'Operands must be two numbers') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $left or ref $right
MC/DC100%2/2
ref $left or ref $right
ref $left
ref $right
Truth table100%3/3
A or B
A= ref $leftB= ref $right
| |||||||||||||||||||||||||||||||||||||||||||||||
| 349 | if ref $left || ref $right; | ||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 287 | return $left - $right; | |||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 352 | elsif ($type == PLUS) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 268 | ▶ | if (ref $left || ref $right) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $left or ref $right
MC/DC100%2/2
ref $left or ref $right
ref $left
ref $right
Truth table100%3/3
A or B
A= ref $leftB= ref $right
| |||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 22 | ▶ | if (ref $left eq ref $right) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 17 | ▶ | if (ref $left eq 'Lox::String') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 16 | return Lox::String->new("$left" . "$right"); | |||||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 359 | Lox::runtime_error( | ||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 6 | $expr->operator, 'Operands must be two numbers or two strings'); | |||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 246 | return $left + $right; # Lox numbers are the only non-object values | |||||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 364 | elsif ($type == SLASH) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 7 | ▶ | Lox::runtime_error($expr->operator, 'Operands must be two numbers') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $left or ref $right
MC/DC100%2/2
ref $left or ref $right
ref $left
ref $right
Truth table100%3/3
A or B
A= ref $leftB= ref $right
| |||||||||||||||||||||||||||||||||||||||||||||||
| 366 | if ref $left || ref $right; | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 5 | ▶ | return $right ? $left / $right : 'NaN'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | elsif ($type == STAR) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 7 | ▶ | Lox::runtime_error($expr->operator, 'Operands must be two numbers') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $left or ref $right
MC/DC100%2/2
ref $left or ref $right
ref $left
ref $right
Truth table100%3/3
A or B
A= ref $leftB= ref $right
| |||||||||||||||||||||||||||||||||||||||||||||||
| 371 | if ref $left || ref $right; | ||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 5 | return $left * $right; | |||||||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 5079 | ▶ | sub evaluate { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 5079 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 378 | 5079 | return $expr->accept($self); | |||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 418 | ▶ | sub is_truthy { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_truthy: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 418 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | 418 | ▶ | return !!$value if ref $value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 17 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 85 | ▶ | sub are_equal { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
are_equal: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 85 | my ($self, $left, $right) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 389 | 85 | ▶ | if (my $ltype = ref $left) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 45 | ▶ | if ($ltype eq ref $right) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 20 | return "$left" eq "$right"; | |||||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 25 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 395 | elsif (ref $right) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 398 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 38 | return $left == $right; | |||||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 458 | ▶ | sub stringify { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stringify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 458 | my ($self, $object) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 405 | 458 | return "$object"; | |||||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 260 | 1; | |||||||||||||||||||||||||||||||||||||||||||||