Tea.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 113 CC 4 SCAR 17.2
CC
4
Cov
0%
translate
20.0
printMainProg
2.0
printClass
2.0
CRAP 5.6
1 package Language::Tea;
2  
3 use warnings;
4 use strict;
5  
6 use lib ( 'lib', '../lib' );
7  
8 use Language::Tea::Traverse;
9 use Language::Tea::Grammar;
10 use Language::Tea::Pad;
11 use Language::Tea::Match2AST;
12 use Language::Tea::AST2Objects;
13 use Language::Tea::NodeUpLinker;
14 use Language::Tea::StatementContext;
15 use Language::Tea::ASTRefactor;
16 use Language::Tea::StaticType;
17 use Language::Tea::JavaEmitter;
18 use Language::Tea::Function;
19
20 =head1 NAME
21  
22 Language::Tea - A Tea code converter.
23  
24 =head1 VERSION
25  
26 Version 0.03
27  
28 =cut
29  
30 our $VERSION = '0.03';
31 our %Classes;
32 our $root;
33  
34 =head1 SYNOPSIS
35  
36 This module converts Tea code into Java.
37  
38 syntax:
39
40 destea.pl filename [option]
41  
42 - filename: The Tea file you want to convert.
43  
44 - option: put any character after the filename if you want destea to print directly to file. This will create a directory named 'Project' with a file MainProgram.java and other files for each class in the Tea file converted.
45  
46 =head1 DESCRIPTION
47  
48 destea will convert Tea code to Java. You have two options:
49 - You can print Java code to standard output;
50 - Or, you can print Java code directly to java files. If you want to use this, you just have to put any character after the filename
51  
52 Example:
53 ./destea.pl example.java a
54  
55 This will create a new directory called 'Project' and inside you'll have MainProgram.java, wich contains all main Tea instructions, and you'll have another files, one for each class in your Tea file converted.
56  
57 Example:
58 Imagine you have a Tea file with two classes: Triangle and Rectangle.
59 If you convert this file, you'll obtain MainProgram.java, Triangle.java and Rectangle.java
60  
61 =cut
62  
63 sub translate {
64 my $source_code= shift;
65 my $filename = shift;
66 my @source_lines = shift;
67  
68 my $Env = Language::Tea::Pad->new();
69 Language::Tea::Function::init_prototypes( $Env );
70  
71 $source_code =~ s/{/ { /g;
72 $source_code =~ s/\(/ \( /g;
73 $source_code =~ s/\)/ \) /g;
74
75 my $match = Language::Tea::Grammar->statements( $source_code );
76 my $ast = Language::Tea::Match2AST::match2ast( $match, $filename, \@source_lines );
77 $root = Language::Tea::AST2Objects::ast2objects( $ast );
78  
79 Language::Tea::NodeUpLinker::create_links( $root );
80 Language::Tea::StatementContext::annotate_context( $root );
81 Language::Tea::ASTRefactor::refactor( $root );
82 $root = Language::Tea::StaticType::annotate_types($root, $Env);
83 {
84 my $i = 0;
85 while ( $i < @{ $root->{statement} } ) {
86 my $node = $root->{statement}[$i];
87 if ( ref( $node ) eq 'TeaPart::Class' ) {
88 my $class = $node->{class}{arg_symbol};
89 $Classes{$class} ||= $node; # XXX
90 splice @{ $root->{statement} }, $i, 1;
91 }
92 elsif ( ref( $node ) eq 'TeaPart::Method' ) {
93 my $class = $node->{class}{arg_symbol};
94 die "undeclared class $class"
95 unless exists $Classes{$class};
96 my $method = $node->{method}{arg_symbol};
97 push @{ $Classes{$class}{statement} }, $node;
98 splice @{ $root->{statement} }, $i, 1;
99 }
100 else {
101 $i++
102 }
103 }
104 }
105 }
106  
107  
108 sub printMainProg {
109 my $package = shift;
110 return Language::Tea::JavaEmitter::emit($root, $package);
111 }
112  
113  
114 sub printClass {
115 my $class = shift;
116 return Language::Tea::JavaEmitter::emit($Classes{$class}, $class);
117 }
118
119  
120 =head1 AUTHOR
121  
122 Mario Silva <mario.silva at verticalone.pt>
123  
124 Flavio Glock <flavio.glock@verticalone.pt>
125  
126 Daniel Ruoso <daniel.ruoso@verticalone.pt>
127  
128 =head1 BUGS
129  
130 Please report any bugs or feature requests to
131 C<bug-language-tea at rt.cpan.org>, or through the web interface at
132 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Language-Tea>.
133 I will be notified, and then you'll automatically be notified of progress on
134 your bug as I make changes.
135  
136 =over 4
137  
138 =item * AnnoCPAN: Annotated CPAN documentation
139  
140 L<http://annocpan.org/dist/Language-Tea>
141  
142 =item * CPAN Ratings
143  
144 L<http://cpanratings.perl.org/d/Language-Tea>
145  
146 =item * RT: CPAN's request tracker
147  
148 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Language-Tea>
149  
150 =item * Search CPAN
151  
152 L<http://search.cpan.org/dist/Language-Tea>
153  
154 =back
155  
156 =head1 COPYRIGHT & LICENSE
157  
158 Copyright 2007 Mario Silva, Flavio Glock, Daniel Ruoso, Vertical One all rights reserved.
159  
160 This program is free software; you can redistribute it and/or modify it
161 under the same terms as Perl itself.
162  
163 =cut
164  
165 1; # End of Language::Tea