Tangence/Meta/Type.pm

filter: Statementstmt 95.4% 42 / 44 Branchbran 83.3% 10 / 12 Conditioncond 60.0% 9 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 91.6% 11 / 12 Podpod 80.0% 4 / 5 totaltotal 78.5% 77 / 98 CC 12 SCAR 16.3
CC
12
Cov
86%
make
10.5
make_from_sig
3.0
member_type
2.1
CRAP 5.1
« Tangence/Type.pm ↑ Summary Tangence/Compiler/Parser.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2012-2024 -- leonerd@leonerd.org.uk
5  
6 15 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Tangence::Meta::Type 0.33;
11 class Tangence::Meta::Type :strict(params);
12  
13 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 C<Tangence::Meta::Type> - structure representing one C<Tangence> value type
18  
19 =head1 DESCRIPTION
20  
21 This data structure object represents information about a type, such as a
22 method or event argument, a method return value, or a property element type.
23  
24 Due to their simple contents and immutable nature, these objects may be
25 implemented as singletons. Repeated calls to the constructor method for the
26 same type name will yield the same instance.
27  
28 =cut
29  
30 =head1 CONSTRUCTOR
31  
32 =cut
33  
34 =head2 make
35  
36 $type = Tangence::Meta::Type->make( $primitive )
37  
38 Returns an instance to represent the given primitive type signature.
39  
40 $type = Tangence::Meta::Type->make( $aggregate => $member_type )
41  
42 Returns an instance to represent the given aggregation of the given type
43 instance.
44  
45 =cut
46  
47 15 our %PRIMITIVES;
48 15 our %LISTS;
49 15 our %DICTS;
50  
51 sub make
52 1040 ▶{
Subroutine make: called CC=9 SCAR=23.5
Pod documented
53 1040 my $class = shift;
54  
55 1040 ▶ if( @_ == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1) { } 820 220 1040
elsif (@_ == 2 and $_[0] eq 'list') { } 141 79 220
elsif (@_ == 2 and $_[0] eq 'dict') { } 79 0 79
56 820 my ( $sig ) = @_;
57 820 ▶ return $PRIMITIVES{$sig} //=
Condition66%2/3
$Tangence::Meta::Type::PRIMITIVES{$sig} //= $class->new("member_type", $sig)
l!l&&r!l&&!r
6991210
MC/DC0%0/2
$Tangence::Meta::Type::PRIMITIVES{$sig} //= $class->new("member_type", $sig)
$Tangence::Meta::Type::PRIMITIVES{$sig} $class->new("member_type", $sig)
Truth table66%2/3
A //= B
A= $Tangence::Meta::Type::PRIMITIVES{$sig}B= $class->new("member_type", $sig)
ABresult
1X1
011
000
58 $class->new( member_type => $sig );
59 }
60 ▶ elsif( @_ == 2 and $_[0] eq "list" ) {
Condition66%2/3
@_ == 2 and $_[0] eq "list"
!ll&&!rl&&r
079141
MC/DC50%1/2
@_ == 2 and $_[0] eq "list"
@_ == 2 $_[0] eq "list"
Truth table66%2/3
A and B
A= @_ == 2B= $_[0] eq "list"
ABresult
0X0
100
111
61 141 my ( undef, $membertype ) = @_;
62 141 ▶ return $LISTS{$membertype->sig} //=
Condition66%2/3
$Tangence::Meta::Type::LISTS{$membertype->sig} //= $class->new("aggregate", "list", "member_type", $membertype)
l!l&&r!l&&!r
92490
MC/DC0%0/2
$Tangence::Meta::Type::LISTS{$membertype->sig} //= $class->new("aggregate", "list", "member_type", $membertype)
$Tangence::Meta::Type::LISTS{$membertype->sig} $class->new("aggregate", "list", "member_type", $membertype)
Truth table66%2/3
A //= B
A= $Tangence::Meta::Type::LISTS{$membertype->sig}B= $class->new("aggregate", "list", "member_type", $membertype)
ABresult
1X1
011
000
63 $class->new( aggregate => "list", member_type => $membertype );
64 }
65 ▶ elsif( @_ == 2 and $_[0] eq "dict" ) {
Condition33%1/3
@_ == 2 and $_[0] eq "dict"
!ll&&!rl&&r
0079
MC/DC0%0/2
@_ == 2 and $_[0] eq "dict"
@_ == 2 $_[0] eq "dict"
Truth table33%1/3
A and B
A= @_ == 2B= $_[0] eq "dict"
ABresult
0X0
100
111
66 79 my ( undef, $membertype ) = @_;
67 79 ▶ return $DICTS{$membertype->sig} //=
Condition66%2/3
$Tangence::Meta::Type::DICTS{$membertype->sig} //= $class->new("aggregate", "dict", "member_type", $membertype)
l!l&&r!l&&!r
61180
MC/DC0%0/2
$Tangence::Meta::Type::DICTS{$membertype->sig} //= $class->new("aggregate", "dict", "member_type", $membertype)
$Tangence::Meta::Type::DICTS{$membertype->sig} $class->new("aggregate", "dict", "member_type", $membertype)
Truth table66%2/3
A //= B
A= $Tangence::Meta::Type::DICTS{$membertype->sig}B= $class->new("aggregate", "dict", "member_type", $membertype)
ABresult
1X1
011
000
68 $class->new( aggregate => "dict", member_type => $membertype );
69 }
70  
71 0 die "TODO: @_";
72 }
73  
74 =head2 make _from_sig
75  
76 $type = Tangence::Meta::Type->make_from_sig( $sig )
77  
78 Parses the given full Tangence type signature and returns an instance to
79 represent it.
80  
81 =cut
82  
83 529 ▶sub make_from_sig ( $class, $sig )
Subroutine make_from_sig: called CC=3 SCAR=11.0
Pod undocumented
84 {
85 529 ▶ $sig =~ m/^list\((.*)\)$/ and
Branch: 1 branch
BranchTrueFalseTotal
$sig =~ /^list\((.*)\)$/u and return $class->make("list", $class->make_from_sig($1)) 54 475 529
86 return $class->make( list => $class->make_from_sig( $1 ) );
87  
88 475 ▶ $sig =~ m/^dict\((.*)\)$/ and
Branch: 1 branch
BranchTrueFalseTotal
$sig =~ /^dict\((.*)\)$/u and return $class->make("dict", $class->make_from_sig($1)) 45 430 475
89 return $class->make( dict => $class->make_from_sig( $1 ) );
90  
91 430 return $class->make( $sig );
92 }
93  
94 1 field $aggregate :param :reader = "prim";
95 1 ▶field $member_type :param;
Subroutine aggregate: called CC=1 SCAR=0.0
Pod documented
96  
97 =head1 ACCESSORS
98  
99 =cut
100  
101 =head2 aggregate
102  
103 $agg = $type->aggregate
104  
105 Returns C<"prim"> for primitive types, or the aggregation name for list and
106 dict aggregate types.
107  
108 =cut
109  
110 =head2 member_type
111  
112 $member_type = $type->member_type
113  
114 Returns the member type for aggregation types. Throws an exception for
115 primitive types.
116  
117 =cut
118  
119 method member_type
120 396 ▶{
Subroutine member_type: called CC=2 SCAR=7.2
Pod documented
121 396 ▶ die "Cannot return the member type for primitive types" if $aggregate eq "prim";
Branch: 1 branch
BranchTrueFalseTotal
if $aggregate eq "prim" 0 396 396
122 396 return $member_type;
123 15 }
124  
125 =head2 sig
126  
127 $sig = $type->sig
128  
129 Returns the Tangence type signature for the type.
130  
131 =cut
132  
133 method sig
134 425 ▶{
Subroutine sig: called CC=1 SCAR=0.0
Pod documented
135 425 return $self->${\"_sig_for_$aggregate"}();
136 15 }
137  
138 method _sig_for_prim
139 415 ▶{
Subroutine _sig_for_prim: called CC=1 SCAR=0.0
140 415 return $member_type;
141 15 }
142  
143 method _sig_for_list
144 10 ▶{
Subroutine _sig_for_list: called CC=1 SCAR=0.0
145 10 return "list(" . $member_type->sig . ")";
146 15 }
147  
148 method _sig_for_dict
149 0 ▶{
Subroutine _sig_for_dict: not called CC=1 SCAR=6.9
150 0 return "dict(" . $member_type->sig . ")";
151 15 }
152  
153 =head1 AUTHOR
154  
155 Paul Evans <leonerd@leonerd.org.uk>
156  
157 =cut
158  
159 15 0x55AA;
« Tangence/Type.pm ↑ Summary Tangence/Compiler/Parser.pm »