| 1 | package Lox::Resolver; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 5 | 260 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 260 | my @constants = qw(CLASS FUNCTION INITIALIZER METHOD NONE SUBCLASS); | |||||||||||||||||||||||||||
| 7 | 260 | my %constant_values = map { $constants[$_] => $_ } 0..$#constants; | |||||||||||||||||||||||||||
| 8 | 260 | require constant; | |||||||||||||||||||||||||||
| 9 | 260 | constant->import(\%constant_values); | |||||||||||||||||||||||||||
| 10 | } | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 260 | our $VERSION = 0.02; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 215 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 215 | my ($class, $interpreter) = @_; | |||||||||||||||||||||||||||
| 16 | 215 | return bless { | |||||||||||||||||||||||||||
| 17 | current_function => NONE, | ||||||||||||||||||||||||||||
| 18 | current_class => NONE, | ||||||||||||||||||||||||||||
| 19 | interpreter => $interpreter, | ||||||||||||||||||||||||||||
| 20 | scopes => [], | ||||||||||||||||||||||||||||
| 21 | }, $class; | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 639 | ▶ | sub current_function :lvalue { $_[0]->{current_function} } | ||||||||||||||||||||||||||
|
Subroutine
current_function: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 367 | ▶ | sub current_class :lvalue { $_[0]->{current_class} } | ||||||||||||||||||||||||||
|
Subroutine
current_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 363 | ▶ | sub interpreter { $_[0]->{interpreter} } | ||||||||||||||||||||||||||
|
Subroutine
interpreter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 5617 | ▶ | sub scopes { $_[0]->{scopes} } | ||||||||||||||||||||||||||
|
Subroutine
scopes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 215 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 215 | my ($self, $stmts) = @_; | |||||||||||||||||||||||||||
| 31 | 215 | $self->resolve($stmts); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 79 | ▶ | sub visit_block_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_block_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 79 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 36 | 79 | $self->begin_scope; | |||||||||||||||||||||||||||
| 37 | 79 | $self->resolve($stmt->statements); | |||||||||||||||||||||||||||
| 38 | 79 | $self->end_scope; | |||||||||||||||||||||||||||
| 39 | 79 | return undef; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | sub visit_break_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_break_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 2 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 44 | 2 | return undef; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 93 | ▶ | sub visit_class_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_class_stmt: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 93 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 49 | 93 | my $enclosing_class = $self->current_class; | |||||||||||||||||||||||||||
| 50 | 93 | $self->current_class = CLASS; | |||||||||||||||||||||||||||
| 51 | 93 | $self->declare($stmt->name); | |||||||||||||||||||||||||||
| 52 | 93 | $self->define($stmt->name); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 93 | ▶ | if (my $sc = $stmt->superclass) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 28 | ▶ | if($stmt->name->lexeme eq $sc->name->lexeme) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 2 | Lox::error($sc->name, 'A class cannot inherit from itself'); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 28 | $self->current_class = SUBCLASS; | |||||||||||||||||||||||||||
| 59 | 28 | $self->resolve($sc); | |||||||||||||||||||||||||||
| 60 | 28 | $self->begin_scope(); | |||||||||||||||||||||||||||
| 61 | 28 | $self->scopes->[-1]{super} = 1; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 93 | $self->begin_scope; | |||||||||||||||||||||||||||
| 65 | 93 | $self->scopes->[-1]->{this} = 1; | |||||||||||||||||||||||||||
| 66 | 93 | foreach my $method ($stmt->methods->@*) { | |||||||||||||||||||||||||||
| 67 | 87 | ▶ | my $declaration = $method->name->lexeme eq 'init' ? INITIALIZER : METHOD; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 87 | $self->resolve_function($method, $declaration); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 93 | $self->end_scope; | |||||||||||||||||||||||||||
| 71 | 93 | ▶ | $self->end_scope if $stmt->superclass; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 93 | $self->current_class = $enclosing_class; | |||||||||||||||||||||||||||
| 73 | 93 | return undef; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 289 | ▶ | sub visit_expression_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_expression_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 289 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 78 | 289 | $self->resolve($stmt->expression); | |||||||||||||||||||||||||||
| 79 | 289 | return undef; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 42 | ▶ | sub visit_if_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_if_stmt: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 42 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 84 | 42 | $self->resolve($stmt->condition); | |||||||||||||||||||||||||||
| 85 | 42 | $self->resolve($stmt->then_branch); | |||||||||||||||||||||||||||
| 86 | 42 | ▶ | if ($stmt->else_branch) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 15 | $self->resolve($stmt->else_branch); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 42 | return undef; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 469 | ▶ | sub visit_print_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_print_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 469 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 94 | 469 | $self->resolve($stmt->expression); | |||||||||||||||||||||||||||
| 95 | 469 | return undef; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 88 | ▶ | sub visit_return_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_return_stmt: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 88 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 100 | 88 | ▶ | if ($stmt->value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 84 | ▶ | if ($self->current_function == INITIALIZER) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | Lox::error($stmt->keyword, 'Cannot return a value from an initializer'); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 84 | $self->resolve($stmt->value); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 88 | return undef; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 79 | ▶ | sub visit_function_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_function_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 79 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 111 | 79 | $self->declare($stmt->name); | |||||||||||||||||||||||||||
| 112 | 79 | $self->define($stmt->name); | |||||||||||||||||||||||||||
| 113 | 79 | $self->resolve_function($stmt, FUNCTION); | |||||||||||||||||||||||||||
| 114 | 79 | return undef; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 163 | ▶ | sub visit_get_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_get_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 163 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 119 | 163 | $self->resolve($expr->object); | |||||||||||||||||||||||||||
| 120 | 163 | return undef; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3194 | ▶ | sub resolve { | ||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 3194 | my ($self, $stmt_or_expr) = @_; | |||||||||||||||||||||||||||
| 125 | 3194 | ▶ | if (ref $stmt_or_expr ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 2715 | $stmt_or_expr = [$stmt_or_expr]; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 3194 | $_->accept($self) for (@$stmt_or_expr); | |||||||||||||||||||||||||||
| 129 | 3194 | return undef; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 185 | ▶ | sub resolve_function { | ||||||||||||||||||||||||||
|
Subroutine
resolve_function: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 133 | 185 | my ($self, $stmt, $type) = @_; | |||||||||||||||||||||||||||
| 134 | 185 | my $enclosing_function = $self->current_function; | |||||||||||||||||||||||||||
| 135 | 185 | $self->current_function = $type; | |||||||||||||||||||||||||||
| 136 | 185 | $self->begin_scope; | |||||||||||||||||||||||||||
| 137 | 185 | for my $param ($stmt->params->@*) { | |||||||||||||||||||||||||||
| 138 | 168 | $self->declare($param); | |||||||||||||||||||||||||||
| 139 | 168 | $self->define($param); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 185 | $self->resolve($stmt->body); | |||||||||||||||||||||||||||
| 142 | 185 | $self->end_scope; | |||||||||||||||||||||||||||
| 143 | 185 | $self->current_function = $enclosing_function; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 385 | ▶ | sub begin_scope { | ||||||||||||||||||||||||||
|
Subroutine
begin_scope: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 147 | 385 | my $self = shift; | |||||||||||||||||||||||||||
| 148 | 385 | push $self->scopes->@*, {}; | |||||||||||||||||||||||||||
| 149 | 385 | return undef; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 385 | ▶ | sub end_scope { | ||||||||||||||||||||||||||
|
Subroutine
end_scope: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 153 | 385 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | 385 | pop $self->scopes->@*; | |||||||||||||||||||||||||||
| 155 | 385 | return undef; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 157 | ▶ | sub visit_var_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_var_stmt: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | 157 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 160 | 157 | $self->declare($stmt->name); | |||||||||||||||||||||||||||
| 161 | 157 | ▶ | if (my $init = $stmt->initializer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 139 | $self->resolve($init); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 157 | $self->define($stmt->name); | |||||||||||||||||||||||||||
| 165 | 157 | return undef; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 30 | ▶ | sub visit_while_stmt { | ||||||||||||||||||||||||||
|
Subroutine
visit_while_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 169 | 30 | my ($self, $stmt) = @_; | |||||||||||||||||||||||||||
| 170 | 30 | $self->resolve($stmt->condition); | |||||||||||||||||||||||||||
| 171 | 30 | $self->resolve($stmt->body); | |||||||||||||||||||||||||||
| 172 | 30 | return undef; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 49 | ▶ | sub visit_assign_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_assign_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | 49 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 177 | 49 | $self->resolve($expr->value); | |||||||||||||||||||||||||||
| 178 | 49 | $self->resolve_local($expr, $expr->name); | |||||||||||||||||||||||||||
| 179 | 49 | return undef; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 294 | ▶ | sub visit_binary_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_binary_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 183 | 294 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 184 | 294 | $self->resolve($expr->left); | |||||||||||||||||||||||||||
| 185 | 294 | $self->resolve($expr->right); | |||||||||||||||||||||||||||
| 186 | 294 | return undef; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 242 | ▶ | sub visit_call_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_call_expr: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 190 | 242 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 191 | 242 | $self->resolve($expr->callee); | |||||||||||||||||||||||||||
| 192 | 242 | for my $argument ($expr->arguments->@*) { | |||||||||||||||||||||||||||
| 193 | 189 | $self->resolve($argument); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 242 | return undef; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 19 | ▶ | sub visit_function_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_function_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 199 | 19 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 200 | 19 | $self->resolve_function($expr, FUNCTION); | |||||||||||||||||||||||||||
| 201 | 19 | return undef; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 12 | ▶ | sub visit_grouping_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_grouping_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 205 | 12 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 206 | 12 | $self->resolve($expr->expression); | |||||||||||||||||||||||||||
| 207 | 12 | return undef; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 877 | ▶ | sub visit_literal_expr { undef } | ||||||||||||||||||||||||||
|
Subroutine
visit_literal_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 28 | ▶ | sub visit_logical_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_logical_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 213 | 28 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 214 | 28 | $self->resolve($expr->left); | |||||||||||||||||||||||||||
| 215 | 28 | $self->resolve($expr->right); | |||||||||||||||||||||||||||
| 216 | 28 | return undef; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 108 | ▶ | sub visit_set_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_set_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 220 | 108 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 221 | 108 | $self->resolve($expr->value); | |||||||||||||||||||||||||||
| 222 | 108 | $self->resolve($expr->object); | |||||||||||||||||||||||||||
| 223 | 108 | return undef; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 32 | ▶ | sub visit_unary_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_unary_expr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 227 | 32 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 228 | 32 | $self->resolve($expr->right); | |||||||||||||||||||||||||||
| 229 | 32 | return undef; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 497 | ▶ | sub declare { | ||||||||||||||||||||||||||
|
Subroutine
declare: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 233 | 497 | my ($self, $name_token) = @_; | |||||||||||||||||||||||||||
| 234 | 497 | ▶ | return undef unless $self->scopes->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 267 | my $scope = $self->scopes->[-1]; | |||||||||||||||||||||||||||
| 237 | 267 | ▶ | if (exists $scope->{$name_token->lexeme}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 3 | Lox::error($name_token, | |||||||||||||||||||||||||||
| 239 | 'Variable with this name already declared in this scope'); | ||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 267 | return $self->scopes->[-1]{$name_token->lexeme} = 0; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 497 | ▶ | sub define { | ||||||||||||||||||||||||||
|
Subroutine
define: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 246 | 497 | my ($self, $name_token) = @_; | |||||||||||||||||||||||||||
| 247 | 497 | ▶ | return undef unless $self->scopes->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 267 | return $self->scopes->[-1]{$name_token->lexeme} = 1; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 18 | ▶ | sub visit_super_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_super_expr: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 252 | 18 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 253 | 18 | ▶ | if ($self->current_class == NONE) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 254 | 3 | Lox::error($expr->keyword, 'Cannot use \'super\' outside of a class'); | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | elsif ($self->current_class != SUBCLASS) { | ||||||||||||||||||||||||||||
| 257 | 2 | Lox::error($expr->keyword, | |||||||||||||||||||||||||||
| 258 | 'Cannot use \'super\' in a class with no superclass'); | ||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | 18 | $self->resolve_local($expr, $expr->keyword); | |||||||||||||||||||||||||||
| 261 | 18 | return undef; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 27 | ▶ | sub visit_this_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_this_expr: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 265 | 27 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 266 | 27 | ▶ | if ($self->current_class == NONE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 2 | Lox::error($expr->keyword, 'Cannot use \'this\' outside of a class'); | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | 27 | $self->resolve_local($expr, $expr->keyword); | |||||||||||||||||||||||||||
| 270 | 27 | return undef; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 759 | ▶ | sub visit_variable_expr { | ||||||||||||||||||||||||||
|
Subroutine
visit_variable_expr: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 274 | 759 | my ($self, $expr) = @_; | |||||||||||||||||||||||||||
| 275 | 759 | ▶ | return undef unless $self->scopes->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 492 | my $value = $self->scopes->[-1]{$expr->name->lexeme}; | |||||||||||||||||||||||||||
| 278 | 492 | ▶ | if (defined $value && $value == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value == 0
MC/DC100%2/2
defined $value and $value == 0
defined $value
$value == 0
Truth table100%3/3
A and B
A= defined $valueB= $value == 0
| |||||||||||||||||||||||||||||
| 279 | 1 | Lox::error($expr->name, | |||||||||||||||||||||||||||
| 280 | 'Cannot read local variable in its own initializer'); | ||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | 492 | $self->resolve_local($expr, $expr->name); | |||||||||||||||||||||||||||
| 283 | 492 | return undef; | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 586 | ▶ | sub resolve_local { | ||||||||||||||||||||||||||
|
Subroutine
resolve_local: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 287 | 586 | my ($self, $expr, $name_token) = @_; | |||||||||||||||||||||||||||
| 288 | 586 | ▶ | for (my $i = $#{$self->scopes}; $i >= 0; $i--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 731 | ▶ | if (exists $self->scopes->[$i]{$name_token->lexeme}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 363 | $self->interpreter->resolve($expr, $#{$self->scopes} - $i); | |||||||||||||||||||||||||||
| 291 | 363 | return; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | # not found assume it is global | ||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 260 | 1; | |||||||||||||||||||||||||||