LispPerl/Printer.pm

filter: Statementstmt 96.4% 81 / 84 Branchbran 88.8% 32 / 36 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 4 / 4 Podpod 50.0% 1 / 2 totaltotal 92.6% 126 / 136 CC 27 SCAR 28.6
CC
27
Cov
94%
to_string
22.1
to_perl
6.0
BEGIN
1.0
CRAP 17.5
« LispPerl/Evaler.pm ↑ Summary LispPerl/Reader.pm »
1 package Language::LispPerl::Printer;
2 6 $Language::LispPerl::Printer::VERSION = '0.007';
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head2 to_perl
7  
8 Pure function. Takes something Language::LispPerl related and
9 turns it into a pure perl data structure.
10  
11 =cut
12  
13 860 ▶sub to_perl{
Subroutine to_perl: called CC=6 SCAR=17.9
Pod documented
14 860 my $thing = shift;
15  
16 # Object case. Easy.
17 860 ▶ if( Scalar::Util::blessed( $thing ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing)) 130 730 860
18 130 return $thing->to_hash();
19 }
20  
21 730 my $ref_thing = ref($thing);
22 # Pure scalar thing. Easy.
23 730 ▶ unless( $ref_thing ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref_thing) 550 180 730
24 550 return $thing;
25 }
26 180 ▶ if( $ref_thing eq 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_thing eq "ARRAY") 54 126 180
27 54 return [ map{ to_perl( $_ ) } @{$thing} ];
28 }
29 126 ▶ if( $ref_thing eq 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_thing eq "HASH") 126 0 126
30 126 my $hash = {};
31 126 ▶ while( my ( $k , $v ) = each %$thing ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$thing) 370 126 496
32 370 $hash->{$k} = to_perl( $v );
33 }
34 126 return $hash;
35 }
36 0 confess("Cannot turn $thing into pure perl structure");
37 }
38  
39  
40 255 ▶sub to_string {
Subroutine to_string: called CC=22 SCAR=31.0
Pod undocumented
41 255 my $obj = shift;
42 255 ▶ return "" if !defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 255 255
43 255 my $class = $obj->class();
44 255 my $type = $obj->type();
45 255 my $s = "";
46 255 ▶ if ( $class eq "Seq" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'Seq') { } 49 206 255
47 49 ▶ if ( $type eq "vector" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'vector') { } 10 39 49
elsif ($type eq 'map') { } 0 39 39
48 10 $s = "[";
49 }
50 elsif ( ( $type eq "map" ) ) {
51 0 $s = "{";
52 }
53 else {
54 39 $s = "(";
55 }
56 49 foreach my $i ( @{ $obj->value() } ) {
57 112 $s .= to_string($i) . " ";
58 }
59 49 ▶ if ( $type eq "vector" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'vector') { } 10 39 49
elsif ($type eq 'map') { } 0 39 39
60 10 $s .= "]";
61 }
62 elsif ( ( $type eq "map" ) ) {
63 0 $s .= "}";
64 }
65 else {
66 39 $s .= ")";
67 }
68 49 $s =~ s/ ([\)\]\}])$/$1/;
69 }
70 else {
71 206 ▶ if ( $type eq "vector" ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'vector') { } 4 202 206
elsif ($type eq 'map' or $type eq 'meta') { } 8 194 202
elsif ($type eq 'xml') { } 6 188 194
elsif ($type eq 'function' or $type eq 'macro') { } 3 185 188
elsif ($type eq 'exception') { } 2 183 185
elsif ($type eq 'string') { } 34 149 183
72 4 $s = "[";
73 4 foreach my $i ( @{ $obj->value() } ) {
74 13 $s .= to_string($i) . " ";
75 }
76 4 $s .= "]";
77 4 $s =~ s/ \]$/\]/;
78 }
79 ▶ elsif ( $type eq "map" or $type eq "meta" ) {
Condition100%3/3
$type eq "map" or $type eq "meta"
l!l&&r!l&&!r
35194
MC/DC100%2/2
$type eq "map" or $type eq "meta"
$type eq "map" $type eq "meta"
Truth table100%3/3
A or B
A= $type eq "map"B= $type eq "meta"
ABresult
1X1
011
000
80 8 $s = "{";
81 8 foreach my $i ( keys %{ $obj->value() } ) {
82 9 $s .= $i . "=>" . to_string( $obj->value()->{$i} ) . " ";
83 }
84 8 $s .= "}";
85 8 $s =~ s/ \}$/\}/;
86 }
87 elsif ( $type eq "xml" ) {
88 6 $s = "<";
89 6 $s .= $obj->{name};
90 6 ▶ if ( defined $obj->{meta_data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj->{"meta_data"}) 1 5 6
91 1 my %meta = %{ $obj->meta_data()->value() };
92 1 foreach my $i ( keys %meta ) {
93 1 $s .= " " . $i . "=\"" . to_string( $meta{$i} ) . "\"";
94 }
95 }
96 6 $s .= ">";
97 6 foreach my $i ( @{ $obj->value() } ) {
98 7 $s .= to_string($i) . " ";
99 }
100 6 $s .= "</" . $obj->{name} . ">";
101 }
102 ▶ elsif ( $type eq "function" or $type eq "macro" ) {
Condition66%2/3
$type eq "function" or $type eq "macro"
l!l&&r!l&&!r
30185
MC/DC50%1/2
$type eq "function" or $type eq "macro"
$type eq "function" $type eq "macro"
Truth table66%2/3
A or B
A= $type eq "function"B= $type eq "macro"
ABresult
1X1
011
000
103 3 $s = to_string( $obj->value() );
104 }
105 elsif ( $type eq "exception" ) {
106 2 $s = "exception: ";
107 2 $s .= $obj->{label} . " - ";
108 2 $s .= $obj->value();
109 2 foreach my $c ( @{ $obj->{caller} } ) {
110 3 $s .= "\n\t" . to_string($c);
111 3 $s .= "[";
112 3 $s .= "file:" . $c->{pos}->{filename} . "; ";
113 3 $s .= "line:" . $c->{pos}->{line} . "; ";
114 3 $s .= "col:" . $c->{pos}->{col} . ";";
115 3 $s .= "]";
116 }
117 }
118 elsif( $type eq 'string' ){
119 34 $s = $obj->value();
120 34 $s =~ s/"/\\"/g;
121 34 $s = '"'.$s.'"';
122 }
123 else {
124 149 $s = $obj->value();
125 }
126 }
127 255 return $s;
128 }
129  
130 6 1;
131  
« LispPerl/Evaler.pm ↑ Summary LispPerl/Reader.pm »