File: Type.pm

package info (click to toggle)
libtangence-perl 0.33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 580 kB
  • sloc: perl: 6,076; makefile: 15
file content (159 lines) | stat: -rw-r--r-- 3,134 bytes parent folder | download
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
#  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-2024 -- leonerd@leonerd.org.uk

use v5.26;
use warnings;
use Object::Pad 0.800;

package Tangence::Meta::Type 0.33;
class Tangence::Meta::Type :strict(params);

use Carp;

=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. Repeated calls to the constructor method for the
same type name will yield the same instance.

=cut

=head1 CONSTRUCTOR

=cut

=head2 make

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

Returns an instance to represent the given primitive type signature.

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

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

=cut

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

sub make
{
   my $class = shift;

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

   die "TODO: @_";
}

=head2 make _from_sig

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

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

=cut

sub make_from_sig ( $class, $sig )
{
   $sig =~ m/^list\((.*)\)$/ and
      return $class->make( list => $class->make_from_sig( $1 ) );

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

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

field $aggregate   :param :reader = "prim";
field $member_type :param;

=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

=head2 member_type

   $member_type = $type->member_type

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

=cut

method member_type
{
   die "Cannot return the member type for primitive types" if $aggregate eq "prim";
   return $member_type;
}

=head2 sig

   $sig = $type->sig

Returns the Tangence type signature for the type.

=cut

method sig
{
   return $self->${\"_sig_for_$aggregate"}();
}

method _sig_for_prim
{
   return $member_type;
}

method _sig_for_list
{
   return "list(" . $member_type->sig . ")";
}

method _sig_for_dict
{
   return "dict(" . $member_type->sig . ")";
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;