Tea/StaticType.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 180 CC 21 SCAR 60.5
CC
21
Cov
0%
annotate_types
462.0
BEGIN
2.0
BEGIN
2.0
CRAP 422.0
1 package Language::Tea::StaticType;
2  
3 use strict;
4 use warnings;
5  
6 sub annotate_types {
7 my ( $root, $Env ) = ( shift, shift );
8  
9 # add lexical scopes
10 #print "\n\n\nAdd lexical scopes\n\n\n";
11 {
12 my $visitor;
13 $visitor = sub {
14 my ( $root, $parent, $outer ) = @_;
15  
16 #print "visit ",ref($_[0]),"\n";
17 #print ref($root),"\n";
18  
19 $root->{pad} = \$outer;
20  
21 if ( ref($root) eq 'TeaPart::Define'
22 || ref($root) eq 'TeaPart::DefineFunc'
23 || ref($root) eq 'TeaPart::Global'
24 || ref($root) eq 'TeaPart::GlobalFunc' )
25 {
26 my $name = Language::Tea::Pad::mangle( $root->{arg_symbol} );
27  
28 #print "*** DEFINE $name \n";
29 ${ $root->{pad} }->add_lexicals(
30 [ '$' . $name . "_TYPE_", '$' . $name . "_NAME_", ] );
31 }
32  
33 if ( exists $root->{statement} ) {
34  
35 #die unless $outer->isa( 'Language::Tea::Pad' );
36 my $inner = Language::Tea::Pad->new( outer => $outer );
37  
38  
39 for my $key ( keys %$root ) {
40 next if $key eq '__node_parent__';
41  
42 #print "visit $key\n";
43 Language::Tea::Traverse::visit_prefix( $root->{$key},
44 $visitor, $root, $inner );
45 }
46  
47 #die;
48 return $root;
49 }
50  
51 return;
52 };
53 $root =
54 Language::Tea::Traverse::visit_prefix( $root, $visitor, undef, $Env,
55 );
56 }
57  
58  
59 #print "\n\n\nTipagem\n\n\n";
60  
61 $root = Language::Tea::Traverse::visit_postfix(
62 $root,
63 sub {
64  
65 #print "visit ",ref($_[0]),"\n";
66 if ( ref $_[0] eq 'TeaPart::arg_string' ) {
67 $_[0]->{type} = 'String';
68 }
69 if ( ref $_[0] eq 'TeaPart::arg_integer' ) {
70 $_[0]->{type} = 'Integer';
71 }
72 if ( ref $_[0] eq 'TeaPart::arg_double' ) {
73 $_[0]->{type} = 'Double';
74 }
75 if ( ref( $_[0] ) eq 'TeaPart::arg_code' ) {
76 $_[0]->{type} = $_[0]->{arg_code}{statement}[-1]{type};
77 }
78 if ( ref( $_[0] ) eq 'TeaPart::arg_do' ) {
79 $_[0]->{type} = $_[0]->{arg_do}{statement}[-1]{type};
80 }
81 if ( ref( $_[0] ) eq 'TeaPart::arg_list' ) {
82 $_[0]->{type} = 'List';
83 }
84 if ( ref( $_[0] ) eq 'TeaPart::Apply' ) {
85 my $func = $_[0]{func};
86  
87 my $result_type;
88 if ( exists $func->{type} ) {
89 $result_type = $func->{type};
90 }
91 else {
92  
93 # get arg types
94 my @args = @{ $_[0]{arg} };
95  
96 my $func_name = $func->{arg_symbol};
97 if ( $func_name eq 'if'
98 || $func_name eq 'while'
99 || $func_name eq 'foreach' )
100 {
101 $result_type = $args[-1]{type};
102 }
103 else {
104  
105 #print "func args @args \n";
106 my @types = ();
107 for (@args) {
108 push @types, $_->{type} || 'Object';
109 }
110  
111 #print "func = $func_name types = @types \n";
112 $result_type =
113 ${ $_[0]{pad} }->get_type( $func_name, @types );
114 }
115 }
116  
117 #print "result_type = $result_type \n";
118 #return $_[0];
119 #$func->{type} = $result_type;
120 $_[0]->{type} = $result_type;
121 }
122 if ( ref( $_[0] ) eq 'TeaPart::Define' || ref( $_[0] ) eq 'TeaPart::Global' ) {
123  
124 #my $old_type = ${$_[0]{pad}}->get_type( $_[0]->{arg_symbol} );
125  
126 # For variables defined without a value, assume TeaUnknowType
127 # TODO: Inspect code to see the actual type.
128 my $new_type;
129 if ( exists $_[0]->{statement} && exists $_[0]->{statement}[0] )
130 {
131 $new_type = $_[0]->{statement}[0]{type};
132 }
133 else {
134 $new_type = 'TeaUnknownType';
135 }
136  
137 $_[0]->{type} = $new_type;
138  
139 #print "Define symbol: ",$_[0]->{arg_symbol}," ", $_[0]->{type} ,"\n";
140 #print "in pad: ${$_[0]{pad}}\n";
141 ${ $_[0]{pad} }->add_type( $_[0]->{arg_symbol}, $_[0]->{type} );
142  
143 #print "Define: name = ", ${$_[0]{pad}}->get_name( $_[0]->{arg_symbol} );
144 #print "get name\n";
145 $_[0]->{mangled} =
146 ${ $_[0]{pad} }->get_name( $_[0]->{arg_symbol} );
147 }
148 if ( ref( $_[0] ) eq 'TeaPart::DefineFunc' || ref( $_[0] ) eq 'TeaPart::GlobalFunc' ) {
149
150 #my $old_type = ${$_[0]{pad}}->get_type( $_[0]->{arg_symbol} );
151 return unless ( exists $_[0]->{arg_code}{statement}[0]{type});
152 my $new_type = $_[0]->{arg_code}{statement}[-1]{type};
153 $_[0]->{type} = $new_type;
154  
155 #print "Define func: ",$_[0]->{arg_symbol}," ", $_[0]->{type} ,"\n";
156 ${ $_[0]{pad} }->add_type( $_[0]->{arg_symbol}, $_[0]->{type} );
157 $_[0]->{mangled} =
158 ${ $_[0]{pad} }->get_name( $_[0]->{arg_symbol} );
159 }
160 if ( ref( $_[0] ) eq 'TeaPart::Dereference' ) {
161 $_[0]->{type} =
162 ${ $_[0]{pad} }->get_type( $_[0]->{arg_symbol} );
163 $_[0]->{mangled} =
164 ${ $_[0]{pad} }->get_name( $_[0]->{arg_symbol} );
165 }
166 if ( ref( $_[0] ) eq 'TeaPart::arg_symbol' ) {
167 $_[0]->{mangled} =
168 ${ $_[0]{pad} }->get_name( $_[0]->{arg_symbol} );
169 }
170 if ( ref( $_[0] ) eq 'TeaPart::Method' ) {
171 $_[0]->{type} = $_[0]->{arg}[-1]{type};
172  
173 # _class_method_METHOD
174 my $symbol =
175 $_[0]->{class}{arg_symbol} . '_'
176 . $_[0]->{method}{arg_symbol}
177 . '_METHOD';
178  
179 #print "Add symbol: $symbol = ",$_[0]->{type},"\n";
180 ${ $_[0]{pad} }->add_type( $symbol, $_[0]->{type} );
181 }
182 if ( ref( $_[0] ) eq 'TeaPart::Call' ) {
183  
184 #print "Call: \n";
185 my $invocant = $_[0]->{invocant};
186 my $method = $_[0]->{method};
187 my $class = ${ $_[0]{pad} }->get_type( $_[0]->{invocant} );
188  
189 #print "Call: class $class\n";
190 # _class_method_METHOD
191 my $symbol = $class . '_' . $method . '_METHOD';
192 #print $symbol." Aki ta o symbol\n";
193 $_[0]->{type} = ${ $_[0]{pad} }->get_type($symbol);
194 }
195 return;
196 }
197 );
198  
199 return $root;
200 }
201  
202 1;