Scheme79asm.pm

filter: Statementstmt 100% 111 / 111 Branchbran 85.0% 37 / 40 (3 uncoverable) Conditioncond 100% 10 / 12 (2 uncoverable) MC/DCmcdc 100% 4 / 6 (2 uncoverable) Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 96.8% 186 / 193 (7 uncoverable) CC 31 SCAR 19.9
CC
31
Cov
96%
process
14.0
print_verilog
7.0
print_binary16
6.5
CRAP 7.3
↑ Summary Scheme79asm/Compiler.pm »
1 package App::Scheme79asm;
2  
3 1 ▶use 5.014000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use re '/s';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::SExpression qw/consp scalarp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '1.000';
14  
15 1 our %TYPES = (
16 LIST => 0,
17 SYMBOL => 1,
18 NUMBER => 1,
19 VAR => 2,
20 VARIABLE => 2,
21 CLOSURE => 3,
22 PROC => 4,
23 PROCEDURE => 4,
24 IF => 5,
25 COND => 5,
26 CONDITIONAL => 5,
27 CALL => 6,
28 QUOTE => 7,
29 QUOTED => 7,
30  
31 MORE => 0,
32 CAR => 1,
33 CDR => 2,
34 CONS => 3,
35 ATOM => 4,
36 PROGN => 5,
37 'REVERSE-LIST' => 6,
38 FUNCALL => 7,
39 );
40  
41 1 *consp = *Data::SExpression::consp;
42 1 *scalarp = *Data::SExpression::scalarp;
43  
44 36 ▶sub process {
Subroutine process: called CC=14 SCAR=26.4
Pod documented
45 36 my ($self, $sexp, $location) = @_;
46 36 ▶ die 'Toplevel is not a list: ', Dumper($sexp), "\n" unless ref $sexp eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sexp eq "ARRAY" 1 35 36
47 35 my ($type, @addrs) = @$sexp;
48 35 my $addr;
49  
50 35 ▶ die 'Type of toplevel is not atom: '. Dumper($type), "\n" unless scalarp($type);
Branch: 1 branch
BranchTrueFalseTotal
unless &scalarp($type) 1 34 35
51  
52 34 ▶ if (@addrs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@addrs > 1) { } 8 26 34
53 8 $addr = $self->{freeptr} + 1;
54 8 $self->{freeptr} += @addrs;
55 8 $self->process($addrs[$_], $addr + $_) for 0 .. $#addrs;
56 } else {
57 26 $addr = $addrs[0];
58 }
59  
60 34 ▶ $addr = $self->process($addr) if ref $addr eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $addr eq "ARRAY" 4 30 34
61 34 ▶ die 'Addr of toplevel is not atom: ', Dumper($addr), "\n" unless scalarp($addr);
Branch: 1 branch
BranchTrueFalseTotal
unless &scalarp($addr) 1 33 34
62 33 my ($comment_type, $comment_addr) = ($type, $addr);
63 33 ▶ die 'Computed addr is not a number: ', Dumper($addr), "\n" unless looks_like_number $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($addr) 1 32 33
64  
65 32 ▶ if (!looks_like_number $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($type)) 30 2 32
66 30 ▶ die "No such type: $type\n" unless exists $TYPES{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $App::Scheme79asm::TYPES{$type} 1 29 30
67 29 $type = $TYPES{$type};
68 }
69  
70 31 ▶ $addr += (1 << $self->{addr_bits}) if $addr < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $addr < 0 2 29 31
71 31 ▶ die "Type too large: $type\n" if $type >= (1 << $self->{type_bits});
Branch: 1 branch
BranchTrueFalseTotal
if $type >= 1 << $self->{"type_bits"} 1 30 31
72 30 ▶ die "Addr too large: $addr\n" if $addr >= (1 << $self->{addr_bits});
Branch: 1 branch
BranchTrueFalseTotal
if $addr >= 1 << $self->{"addr_bits"} 1 29 30
73 29 my $result = ($type << $self->{addr_bits}) + $addr;
74  
75 29 ▶ unless ($location) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($location) 13 16 29
76 13 $self->{freeptr}++;
77 $location = $self->{freeptr}
78 13 }
79 29 $self->{memory}[$location] = $result;
80 29 $self->{comment}[$location] = "$comment_type $comment_addr";
81 29 $location
82 }
83  
84 15 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
85 15 my ($self, $string) = @_;
86 15 my $ds = Data::SExpression->new({symbol_case => 'up', use_symbol_class => 1, fold_lists => 1});
87  
88 15 my $sexp;
89 15 while () {
90 24 ▶ last if $string =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\s*$/su 9 15 24
91 15 ($sexp, $string) = $ds->read($string);
92 15 $self->process($sexp)
93 }
94 }
95  
96 9 ▶sub finish {
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
97 9 my ($self) = @_;
98 9 $self->{memory}[5] = $self->{memory}[$self->{freeptr}];
99 9 $self->{comment}[5] = $self->{comment}[$self->{freeptr}];
100 9 $self->{memory}[4] = $self->{freeptr};
101 9 delete $self->{memory}[$self->{freeptr}]
102 }
103  
104 16 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
105 16 my ($class, %args) = @_;
106 16 ▶ $args{type_bits} //= 3;
Condition100%2/2
$args{"type_bits"} //= 3
l!l
214
MC/DC100%1/1
$args{"type_bits"} //= 3
$args{"type_bits"}
Truth table100%2/2
A
A= $args{"type_bits"}
Aresult
11
00
107 16 ▶ $args{addr_bits} //= 8;
Condition100%2/2
$args{"addr_bits"} //= 8
l!l
79
MC/DC100%1/1
$args{"addr_bits"} //= 8
$args{"addr_bits"}
Truth table100%2/2
A
A= $args{"addr_bits"}
Aresult
11
00
108 16 ▶ $args{freeptr} //= 6;
Condition100%2/2
$args{"freeptr"} //= 6
l!l
214
MC/DC100%1/1
$args{"freeptr"} //= 6
$args{"freeptr"}
Truth table100%2/2
A
A= $args{"freeptr"}
Aresult
11
00
109 16 ▶ $args{memory} //= [0, 0, (1<<$args{addr_bits}), (1<<$args{addr_bits}), 0, 0, 0];
Condition100%2/2
$args{"memory"} //= [0, 0, 1 << $args{"addr_bits"}, 1 << $args{"addr_bits"}, 0, 0, 0]
l!l
214
MC/DC100%1/1
$args{"memory"} //= [0, 0, 1 << $args{"addr_bits"}, 1 << $args{"addr_bits"}, 0, 0, 0]
$args{"memory"}
Truth table100%2/2
A
A= $args{"memory"}
Aresult
11
00
110 16 my @default_comments = ('(cdr part of NIL)', '(car part of NIL)', '(cdr part of T)', '(car part of T)', '(free storage pointer)', '', '(result of computation)');
111 16 for (0 .. $#default_comments) {
112 112 $args{comment}[$_] = $default_comments[$_]
113 }
114 16 bless \%args, $class
115 }
116  
117 5 ▶sub print_binary16 {
Subroutine print_binary16: called CC=6 SCAR=18.7
Pod documented
118 5 my ($self, $fh) = @_;
119 5 ▶ $fh //= \*STDOUT; # uncoverable condition right
Condition100%1/2
$fh //= \*STDOUT
l!l
50
MC/DC100%0/1
$fh //= \*STDOUT
$fh
Truth table100%1/2
A
A= $fh
Aresult
11
00
120  
121 5 ▶ die "addr_bits + type_bits >= 16\n"if $self->{addr_bits} + $self->{type_bits} > 16;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"addr_bits"} + $self->{"type_bits"} > 16 1 4 5
122  
123 4 my $length = @{$self->{memory}};
124 4 ▶ print $fh pack 'n', $length or croak "Failed to print memory size: $!"; # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
print $fh pack("n", $length) or croak("Failed to print memory size: $!") 4 0 4
125 4 for (@{$self->{memory}}) {
126 41 ▶ print $fh pack 'n', $_ or croak "Failed to print memory: $!" # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
print $fh pack("n", $_) or croak("Failed to print memory: $!") 41 0 41
127 }
128 }
129  
130 4 ▶sub print_verilog {
Subroutine print_verilog: called CC=7 SCAR=19.5
Pod documented
131 4 my ($self, $fh) = @_;
132 4 ▶ $fh //= \*STDOUT; # uncoverable condition right
Condition100%1/2
$fh //= \*STDOUT
l!l
40
MC/DC100%0/1
$fh //= \*STDOUT
$fh
Truth table100%1/2
A
A= $fh
Aresult
11
00
133  
134 4 my $bits = $self->{type_bits} + $self->{addr_bits};
135 4 my $index_length = length $#{$self->{memory}};
136 4 my $index_format = '%' . $index_length . 'd';
137 4 for my $index (0 .. $#{$self->{memory}}) {
138 41 my $val = $self->{memory}[$index];
139 41 my $comment = $self->{comment}[$index];
140 41 ▶ if ($index == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == 4) { } 4 37 41
141 4 $val = "${bits}'d$val"
142 } else {
143 37 ▶ $val = $val ? sprintf "%d'b%0${bits}b", $bits, $val : '0';
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 27 10 37
144 }
145 41 my $spaces = ' ' x ($bits + 5 - (length $val));
146 41 $index = sprintf $index_format, $index;
147  
148 41 my $string = "mem[$index] <= $val;";
149 41 ▶ $string .= "$spaces // $comment" if defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment 38 3 41
150 41 ▶ say $fh $string or croak "Failed to print verilog: $!"; # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
say $fh $string or croak("Failed to print verilog: $!") 41 0 41
151 }
152  
153 }
154 11 ▶sub parse_and_print_binary16 {
Subroutine parse_and_print_binary16: called CC=1 SCAR=0.0
Pod documented
155 11 my ($self, $string, $fh) = @_;
156 11 $self->parse($string);
157 5 $self->finish;
158 5 $self->print_binary16($fh);
159 }
160  
161 4 ▶sub parse_and_print_verilog {
Subroutine parse_and_print_verilog: called CC=1 SCAR=0.0
Pod documented
162 4 my ($self, $string, $fh) = @_;
163 4 $self->parse($string);
164 4 $self->finish;
165 4 $self->print_verilog($fh);
166 }
167  
168 1 1;
169 __END__
↑ Summary Scheme79asm/Compiler.pm »