summaryrefslogtreecommitdiff
path: root/lib/Tangence/Meta/Type.pm
blob: 084a4c4169a95b91f1dd9a4b428806d8386e7b28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2012-2017 -- leonerd@leonerd.org.uk

package Tangence::Meta::Type;

use strict;
use warnings;

use Carp;

our $VERSION = '0.25';

=head1 NAME

C<Tangence::Meta::Type> - structure representing one C<Tangence> value type

=head1 DESCRIPTION

This data structure object represents information about a type, such as a
method or event argument, a method return value, or a property element type.

Due to their simple contents and immutable nature, these objects may be
implemented as singletons.

=cut

=head1 CONSTRUCTOR

=cut

=head2 new

   $type = Tangence::Meta::Type->new( $primitive )

Returns an instance to represent the given primitive type signature.

   $type = Tangence::Meta::Type->new( $aggregate => $member_type )

Returns an instance to represent the given aggregation of the given type
instance.

=cut

our %PRIMITIVES;
our %LISTS;
our %DICTS;

sub new
{
   my $class = shift;

   if( @_ == 1 ) {
      my ( $sig ) = @_;
      return $PRIMITIVES{$sig} ||= bless [ prim => $sig ], $class;
   }
   elsif( @_ == 2 and $_[0] eq "list" ) {
      my ( undef, $membertype ) = @_;
      return $LISTS{$membertype->sig} ||= bless [ list => $membertype ], $class;
   }
   elsif( @_ == 2 and $_[0] eq "dict" ) {
      my ( undef, $membertype ) = @_;
      return $DICTS{$membertype->sig} ||= bless [ dict => $membertype ], $class;
   }

   die "TODO: @_";
}

=head2 new_from_sig

   $type = Tangence::Meta::Type->new_from_sig( $sig )

Parses the given full Tangence type signature and returns an instance to
represent it.

=cut

sub new_from_sig
{
   my $class = shift;
   my ( $sig ) = @_;

   $sig =~ m/^list\((.*)\)$/ and
      return $class->new( list => $class->new_from_sig( $1 ) );

   $sig =~ m/^dict\((.*)\)$/ and
      return $class->new( dict => $class->new_from_sig( $1 ) );

   return $class->new( $sig );
}

=head1 ACCESSORS

=cut

=head2 aggregate

   $agg = $type->aggregate

Returns C<"prim"> for primitive types, or the aggregation name for list and
dict aggregate types.

=cut

sub aggregate
{
   my $self = shift;
   return $self->[0];
}

=head2 member_type

   $member_type = $type->member_type

Returns the member type for aggregation types. Throws an exception for
primitive types.

=cut

sub member_type
{
   my $self = shift;
   die "Cannot return the member type for primitive types" if $self->[0] eq "prim";
   return $self->[1];
}

=head2 sig

   $sig = $type->sig

Returns the Tangence type signature for the type.

=cut

sub sig
{
   my $self = shift;
   $self->${\"_sig_for_$self->[0]"}();
}

sub _sig_for_prim
{
   my $self = shift;
   return $self->[1];
}

sub _sig_for_list
{
   my $self = shift;
   return "list(" . $self->[1]->sig . ")";
}

sub _sig_for_dict
{
   my $self = shift;
   return "dict(" . $self->[1]->sig . ")";
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;