| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 24 | 550 | return $thing; | |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||
| 26 | 180 | ▶ | if( $ref_thing eq 'ARRAY' ){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 27 | 54 | return [ map{ to_perl( $_ ) } @{$thing} ]; | |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||
| 29 | 126 | ▶ | if( $ref_thing eq 'HASH' ){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 30 | 126 | my $hash = {}; | |||||||||||||||||||||||||||||
| 31 | 126 | ▶ | while( my ( $k , $v ) = each %$thing ){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 47 | 49 | ▶ | if ( $type eq "vector" ) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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"
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"
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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"
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"
| |||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||