Tea/AST2Objects.pm untested

filter: Statementstmt 0.0% 0 / 140 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 291 CC 37 SCAR 72.0
CC
37
Cov
0%
ast2objects
1406.0
BEGIN
2.0
BEGIN
2.0
CRAP 1334.0
1 package Language::Tea::AST2Objects;
2  
3 use strict;
4 use warnings;
5  
6 sub ast2objects {
7  
8 # select comment, define, dereference, method-apply or function-call
9 # set native types
10 return bless Language::Tea::Traverse::visit_postfix(
11 $_[0],
12 sub {
13 if ( exists $_[0]{arg_string} ) {
14 bless $_[0], 'TeaPart::arg_string';
15 }
16 if ( exists $_[0]{arg_integer} ) {
17 bless $_[0], 'TeaPart::arg_integer';
18 }
19 if ( exists $_[0]{arg_double} ) {
20 bless $_[0], 'TeaPart::arg_double';
21 }
22 if ( exists $_[0]{arg_do} ) {
23 bless $_[0], 'TeaPart::arg_do';
24 }
25 if ( exists $_[0]{arg_code} ) {
26 bless $_[0], 'TeaPart::arg_code';
27 }
28 if ( exists $_[0]{arg_substitution} ) {
29 bless $_[0], 'TeaPart::arg_substitution';
30 }
31 if ( exists $_[0]{arg_list} ) {
32 bless $_[0], 'TeaPart::arg_list';
33 }
34 if ( exists $_[0]{arg} && ref $_[0]{arg} eq 'ARRAY' ) {
35 bless $_[0], 'TeaPart::definition_list';
36 }
37 if ( exists $_[0]{arg_symbol} && not exists $_[0]{define} ) {
38 bless $_[0], 'TeaPart::arg_symbol';
39 }
40 if ( exists $_[0]{comment} && !exists $_[0]{arg} ) {
41  
42 # This is a comment line
43 return bless $_[0]{comment}[0], 'TeaPart::Comment';
44 }
45 if ( exists $_[0]{statement} ) {
46  
47 #print ref $_[0]{statement},"\n";
48 my @out_statement;
49 for my $i ( 0 .. $#{ $_[0]{statement} } ) {
50 my $arg = $_[0]{statement}[$i];
51 if ( exists $arg->{comment} ) {
52 $arg->{comment} = bless $arg->{comment}[0],
53 'TeaPart::Comment';
54 }
55 if ( exists $arg->{define} && $arg->{define} eq "define") {
56 if ( exists $arg->{arg_list} ) {
57 $_[0]{statement}[$i]{arg_list} = { arg => [] }
58 unless ref $_[0]{statement}[$i]{arg_list};
59 bless {
60 arg_code => $_[0]{statement}[$i]{arg_code} },
61 'TeaPart::arg_code';
62 bless {
63 arg_list => $_[0]{statement}[$i]{arg_list} },
64 'TeaPart::arg_list';
65 bless $_[0]{statement}[$i], 'TeaPart::DefineFunc';
66 }
67 else {
68 bless $_[0]{statement}[$i], 'TeaPart::Define';
69 }
70 }
71 elsif ( exists $arg->{define} && $arg->{define} eq "global") {
72 if ( exists $arg->{arg_list} ) {
73 $_[0]{statement}[$i]{arg_list} = { arg => [] }
74 unless ref $_[0]{statement}[$i]{arg_list};
75 bless {
76 arg_code => $_[0]{statement}[$i]{arg_code} },
77 'TeaPart::arg_code';
78 bless {
79 arg_list => $_[0]{statement}[$i]{arg_list} },
80 'TeaPart::arg_list';
81 bless $_[0]{statement}[$i], 'TeaPart::GlobalFunc';
82 }
83 else {
84 bless $_[0]{statement}[$i], 'TeaPart::Global';
85 }
86 }
87 elsif ( exists $arg->{arg} ) {
88 my $first = shift @{ $arg->{arg} };
89  
90 if ( exists $first->{arg_substitution} ) {
91 if ( @{ $arg->{arg} } ) {
92 $arg->{invocant} =
93 $first->{arg_substitution}{arg_symbol};
94 $arg->{method} =
95 ( shift @{ $arg->{arg} } )->{arg_symbol};
96 bless $_[0]{statement}[$i], 'TeaPart::Call';
97 ####################
98 # Special case #
99 ####################
100 if ($arg->{invocant} eq "stdout" && $arg->{method} eq "writeln") {
101 $arg->{invocant} = "System.out";
102 $arg->{method} = "println";
103 }
104
105 }
106 else {
107 $arg->{arg_symbol} =
108 $first->{arg_substitution}{arg_symbol};
109 bless $_[0]{statement}[$i],
110 'TeaPart::Dereference';
111 }
112 }
113 elsif ( exists $first->{arg_symbol}
114 && $first->{arg_symbol} eq 'new' )
115 {
116 $arg->{class} =
117 ( shift @{ $arg->{arg} } )->{arg_symbol};
118 $arg->{type} = $arg->{class};
119 $arg->{arg_list} = $arg->{arg};
120 bless $_[0]{statement}[$i], 'TeaPart::New';
121 }
122 elsif ( exists $first->{arg_symbol}
123 && $first->{arg_symbol} eq 'method' )
124 {
125 $arg->{class} = ( shift @{ $arg->{arg} } );
126 $arg->{method} = ( shift @{ $arg->{arg} } );
127 $arg->{arg_list} =
128 ( shift @{ $arg->{arg} } )->{arg_list};
129  
130 #$arg->{type} = $arg->{class};
131 bless $_[0]{statement}[$i], 'TeaPart::Method';
132 }
133 elsif ( exists $first->{arg_symbol}
134 && $first->{arg_symbol} eq 'class' )
135 {
136 $arg->{class} = ( shift @{ $arg->{arg} } );
137 $arg->{super_class} = (shift @{ $arg->{arg} } )->{arg_symbol} if (defined $arg->{arg}[0]{arg_symbol});
138 $arg->{arg_list} =
139 ( shift @{ $arg->{arg} } )->{arg_list};
140  
141 #$arg->{type} = $arg->{class};
142 bless $_[0]{statement}[$i], 'TeaPart::Class';
143 }
144 elsif ( exists $first->{arg_symbol}
145 && $first->{arg_symbol} eq 'if' )
146 {
147 $arg->{condition} = ( shift @{ $arg->{arg} } );
148 $arg->{then} = ( shift @{ $arg->{arg} } );
149 $arg->{else} = ( shift @{ $arg->{arg} } );
150 bless $_[0]{statement}[$i], 'TeaPart::If';
151 }
152 elsif ( exists $first->{arg_symbol}
153 && $first->{arg_symbol} eq 'cond' )
154 {
155 my $aux = (@{$arg->{arg}}) % 2;
156 for(my $i = 0; $i < (@{$arg->{arg}})-$aux;++$i){
157 $arg->{condition}[$i] = ( shift @{ $arg->{arg} } );
158 $arg->{instructions}[$i] = ( shift @{ $arg->{arg} } );
159 }
160 $arg->{else} = ( shift @{ $arg->{arg} } ) if ($aux);
161 bless $_[0]{statement}[$i], 'TeaPart::Cond';
162 }
163 elsif ( exists $first->{arg_symbol}
164 && $first->{arg_symbol} eq 'while' )
165 {
166 if (defined $arg->{arg}[0]{arg_code}{statement}[0]) {
167 $arg->{condition} =
168 ( shift @{ $arg->{arg} } )
169 ->{arg_code}{statement}[0] ;
170 } else {
171 $arg->{condition} =
172 ( shift @{ $arg->{arg} } );
173 }
174  
175 $arg->{block} = ( shift @{ $arg->{arg} } );
176 bless $_[0]{statement}[$i], 'TeaPart::While';
177 }
178 elsif ( exists $first->{arg_symbol}
179 && $first->{arg_symbol} eq 'foreach' )
180 {
181 $arg->{var1} = ( shift @{ $arg->{arg} } );
182 $arg->{var2} = ( shift @{ $arg->{arg} } );
183 #->{arg_code}{statement}[0];
184 $arg->{block} = ( shift @{ $arg->{arg} } );
185 bless $_[0]{statement}[$i], 'TeaPart::foreach';
186 }
187  
188  
189 elsif ( exists $first->{arg_symbol}
190 && $first->{arg_symbol} eq 'tea-autoload' )
191 {
192  
193 # tea-autoload name sourceFile
194 die
195 "sourceFile must be a string, in /tea-autoload name sourceFile/"
196 unless exists $arg->{arg}[1]{'arg_string'};
197 my $filename = $arg->{arg}[1]{'arg_string'};
198  
199 my $root = Main::compile($filename);
200  
201 push @out_statement, @{ $root->{statement} };
202 next;
203  
204 }
205 elsif ( exists $first->{arg_do} ) {
206 $arg->{arg_do} = $first->{arg_do};
207 bless $_[0]{statement}[$i], 'TeaPart::arg_do';
208 }
209 else {
210 $arg->{func} = $first;
211 bless $_[0]{statement}[$i], 'TeaPart::Apply';
212
213 #########################################
214 # Functions that need special treatment #
215 #########################################
216
217 if ( exists $first->{arg_symbol}
218 && $first->{arg_symbol} eq 'url-build' )
219 {
220 my @list = @{$arg->{arg}[1]->{arg_list}{arg}};
221 my @auxiliar;
222 foreach ( @list) {
223 foreach (@{$_->{arg_list}{arg}}) {
224 #print $values[$i]->[$j++] . "\n" ;
225 push @auxiliar, $_;
226 }
227 }
228 $arg->{arg} = [$arg->{arg}[0], @auxiliar];
229 }
230
231 }
232 }
233 push @out_statement, $_[0]{statement}[$i];
234 }
235 $_[0]{statement} = \@out_statement;
236 return;
237 }
238 return;
239 }
240 ),
241 "TeaProgram";
242 }
243  
244 1;