| 1 | package Lox::AstPrinter; | ||||||||||||
| 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 | 0 | ▶ | sub new { bless {}, shift } | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 7 | |||||||||||||
| 8 | 0 | ▶ | sub print_tree { | ||||||||||
|
Subroutine
print_tree: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 9 | 0 | my ($self, $stmts) = @_; | |||||||||||
| 10 | 0 | return join "\n", grep { /\S/ } split "\n", $self->parenthesize(@$stmts); | |||||||||||
| 11 | } | ||||||||||||
| 12 | |||||||||||||
| 13 | 0 | ▶ | sub visit_break_stmt { | ||||||||||
|
Subroutine
visit_break_stmt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 14 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 15 | 0 | return $self->parenthesize('break'); | |||||||||||
| 16 | } | ||||||||||||
| 17 | |||||||||||||
| 18 | 0 | ▶ | sub visit_class_stmt { | ||||||||||
|
Subroutine
visit_class_stmt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 19 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 20 | 0 | return $self->parenthesize('class', $stmt->name, $stmt->methods->@*); | |||||||||||
| 21 | } | ||||||||||||
| 22 | |||||||||||||
| 23 | 0 | ▶ | sub visit_expression_stmt { | ||||||||||
|
Subroutine
visit_expression_stmt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 24 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 25 | 0 | return $self->parenthesize($stmt->expression); | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 0 | ▶ | sub visit_return_stmt { | ||||||||||
|
Subroutine
visit_return_stmt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 29 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 30 | 0 | return $self->parenthesize('return', $stmt->value); | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 0 | ▶ | sub visit_print_stmt { | ||||||||||
|
Subroutine
visit_print_stmt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 34 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 35 | 0 | return $self->parenthesize('print', $stmt->expression); | |||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | 0 | ▶ | sub visit_var_stmt { | ||||||||||
|
Subroutine
visit_var_stmt: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 39 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 40 | 0 | my @expressions = ('var', $stmt->name, '='); | |||||||||||
| 41 | 0 | ▶ | push @expressions, $stmt->initializer if $stmt->initializer; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 0 | return $self->parenthesize(@expressions); | |||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 0 | ▶ | sub visit_while_stmt { | ||||||||||
|
Subroutine
visit_while_stmt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 46 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 47 | 0 | return $self->parenthesize('while', $stmt->condition, $stmt->body); | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | 0 | ▶ | sub visit_function_stmt { | ||||||||||
|
Subroutine
visit_function_stmt: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 51 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 52 | 0 | my @expressions = ('fun', $stmt->name, '('); | |||||||||||
| 53 | 0 | ▶ | push @expressions, $stmt->params->@* if $stmt->params->@*; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 0 | return $self->parenthesize(@expressions, ')', $stmt->body->@*); | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 0 | ▶ | sub visit_function_expr { | ||||||||||
|
Subroutine
visit_function_expr: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 58 | 0 | my ($self, $expr) = @_; | |||||||||||
| 59 | 0 | my @expressions = ('fun', '('); | |||||||||||
| 60 | 0 | ▶ | push @expressions, $expr->params->@* if $expr->params->@*; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 0 | return $self->parenthesize(@expressions, ')', $expr->body->@*); | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | 0 | ▶ | sub visit_if_stmt { | ||||||||||
|
Subroutine
visit_if_stmt: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 65 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 66 | 0 | my @expressions = ('if', $stmt->condition, $stmt->then_branch); | |||||||||||
| 67 | 0 | ▶ | push @expressions, $stmt->else_branch if $stmt->else_branch; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | return $self->parenthesize(@expressions); | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 0 | ▶ | sub visit_block_stmt { | ||||||||||
|
Subroutine
visit_block_stmt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 72 | 0 | my ($self, $stmt) = @_; | |||||||||||
| 73 | 0 | return $self->parenthesize($stmt->statements->@*); | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 0 | ▶ | sub visit_unary_expr { | ||||||||||
|
Subroutine
visit_unary_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 77 | 0 | my ($self, $expr) = @_; | |||||||||||
| 78 | 0 | return $self->parenthesize($expr->operator,$expr->right); | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 0 | ▶ | sub visit_binary_expr { | ||||||||||
|
Subroutine
visit_binary_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 82 | 0 | my ($self, $expr) = @_; | |||||||||||
| 83 | 0 | return $self->parenthesize($expr->left,$expr->operator,$expr->right); | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 0 | ▶ | sub visit_assign_expr { | ||||||||||
|
Subroutine
visit_assign_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 87 | 0 | my ($self, $expr) = @_; | |||||||||||
| 88 | 0 | return $self->parenthesize($expr->name,'=',$expr->value); | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 0 | ▶ | sub visit_call_expr { | ||||||||||
|
Subroutine
visit_call_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 92 | 0 | my ($self, $expr) = @_; | |||||||||||
| 93 | 0 | return $self->parenthesize($expr->callee, $expr->arguments->@*); | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 0 | ▶ | sub visit_set_expr { | ||||||||||
|
Subroutine
visit_set_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 97 | 0 | my ($self, $expr) = @_; | |||||||||||
| 98 | 0 | return $self->parenthesize($expr->object, '.', $expr->name, '(', $expr->value, ')'); | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 0 | ▶ | sub visit_this_expr { | ||||||||||
|
Subroutine
visit_this_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 102 | 0 | my ($self, $expr) = @_; | |||||||||||
| 103 | 0 | return $expr->keyword->lexeme; | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 0 | ▶ | sub visit_variable_expr { | ||||||||||
|
Subroutine
visit_variable_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 107 | 0 | my ($self, $expr) = @_; | |||||||||||
| 108 | 0 | return $expr->name->lexeme; | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 0 | ▶ | sub visit_get_expr { | ||||||||||
|
Subroutine
visit_get_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 112 | 0 | my ($self, $expr) = @_; | |||||||||||
| 113 | 0 | return $self->parenthesize($expr->object, '.', $expr->name, '()'); | |||||||||||
| 114 | } | ||||||||||||
| 115 | |||||||||||||
| 116 | 0 | ▶ | sub visit_grouping_expr { | ||||||||||
|
Subroutine
visit_grouping_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 117 | 0 | my ($self, $expr) = @_; | |||||||||||
| 118 | 0 | return $self->parenthesize($expr->expression); | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 0 | ▶ | sub visit_literal_expr { | ||||||||||
|
Subroutine
visit_literal_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 122 | 0 | my ($self, $expr) = @_; | |||||||||||
| 123 | 0 | return $expr->value; | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | 0 | ▶ | sub visit_logical_expr { | ||||||||||
|
Subroutine
visit_logical_expr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 127 | 0 | my ($self, $expr) = @_; | |||||||||||
| 128 | 0 | return $self->visit_binary($expr); | |||||||||||
| 129 | } | ||||||||||||
| 130 | |||||||||||||
| 131 | 0 | ▶ | sub visit_token { | ||||||||||
|
Subroutine
visit_token: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 132 | 0 | my ($self, $token) = @_; | |||||||||||
| 133 | 0 | return $token->lexeme; | |||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 260 | my $column = 0; | |||||||||||
| 137 | 0 | ▶ | sub parenthesize { | ||||||||||
|
Subroutine
parenthesize: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 138 | 0 | my ($self, @expr) = @_; | |||||||||||
| 139 | 0 | ▶ | my $indent = ' ' x $column // ''; | ||||||||||
|
Condition0%0/2
" " x $column // ""
MC/DC0%0/1
" " x $column // ""
" " x $column
Truth table0%0/2
A
A= " " x $column
| |||||||||||||
| 140 | 0 | $column += 2; | |||||||||||
| 141 | 0 | ▶ | my $values = join ' ', map { ref $_ ? $_->accept($self) : $_ } @expr; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 0 | my $parenthesized = "\n$indent(\n$indent $values\n$indent)"; | |||||||||||
| 143 | 0 | $column -= 2; | |||||||||||
| 144 | 0 | return $parenthesized; | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | 260 | 1; | |||||||||||