File Coverage

blib/lib/Language/Zcode/Translator/Generic.pm
Criterion Covered Total %
statement 20 24 83.3
branch 6 8 75.0
condition n/a
subroutine 6 9 66.6
pod 4 5 80.0
total 36 46 78.2


line stmt bran cond sub pod time code
1             package Language::Zcode::Translator::Generic;
2            
3 1     1   5 use strict;
  1         2  
  1         114  
4 1     1   15 use warnings;
  1         2  
  1         378  
5            
6             =head1 NAME
7            
8             Language::Zcode::Translator::Generic
9            
10             =head1 DESCRIPTION
11            
12             Abstract class that's the parent of all language-specific translators.
13            
14             A translator has methods to translate Z-code into a given other language.
15            
16             =head2 Methods
17            
18             (Implement all of these to have a valid language translator.)
19            
20             =head3 program_start
21            
22             Returns a string that should be at the beginning of the output program.
23             (Empty by default)
24            
25             =head3 program_end
26            
27             Returns a string that should be at the end of the output program.
28             (Empty by default)
29            
30             =cut
31            
32 0     0 1 0 sub program_start { "" }
33 1     1 1 63 sub program_end { "" }
34            
35             =head3 routine_start
36            
37             Returns a string that should be at the beginning of each output subroutine.
38             Might have side effects (e.g., tell translator to start indenting).
39            
40             Input: Name of sub, array of initial local variable values (all 0 for v5+)
41            
42             =head3 translate_command(command_hashref)
43            
44             This is the translation workhorse. Translate a command (a reference
45             to a hash returned by a LZ::Parser::parse routine) to the destination
46             language.
47            
48             =head3 routine_end
49            
50             Returns a string that should be at the end of each output subroutine.
51             Might have side effects (e.g., tell translator to stop indenting).
52            
53             =head3 packed_address_str(address, type_of_address)
54            
55             String representing a version-dependent packed address.
56             (Note packing is different for routines and strings in V6/7.)
57            
58             Calculate actual address if $address is a number.
59             Create a language-dependent multiplication string
60             by calling language-dependent mult_add_str otherwise.
61            
62             =cut
63            
64 0     0 0 0 sub library { "" }
65            
66             sub packed_address_str {
67 372     372 1 752 my ($self, $address, $key) = @_;
68 372         7357 my %c = %Language::Zcode::Util::Constants;
69 372         7299 my $mult = $c{packed_multiplier};
70 372         416 my $add;
71             # (Add will be zero for versions not 6 or 7)
72 372 100       862 if ($key eq "routine") {
    50          
73 365         1030 $add = 8 * $c{routines_offset};
74             } elsif ($key eq "packed_address_of_string") {
75 7         19 $add = 8 * $c{strings_offset};
76 0         0 } else { die "Unknown key $key to packed_address_str" }
77            
78             # Now actually create the string. Only do calculation for true number
79 372 100       9725 if ($address =~ /^\d+$/) {
80             # if ($address =~ /^(sp|local\d+|g[a-f\d]{2})$/) {
81 366         5213 return $mult * $address + $add;
82             } else {
83 6         65 return $self->mult_add_str($address, $mult, $add);
84             }
85             }
86            
87             =head3 mult_add_str(thing, multiplier, adder)
88            
89             Create a language-dependent multiplication/addition string
90             thing*multiplier + adder
91            
92             =cut
93            
94             sub mult_add_str {
95 6     6 1 15 my ($self, $thing, $mult, $add) = @_;
96             # works for Perl, C, BASIC, java...
97 6 50       70 return $add ? "$thing * $mult + $add" : "$thing * $mult";
98            
99             }
100            
101             sub AUTOLOAD {
102 0     0   0 die "Tried to call $Plotz::Translator::Generic::AUTOLOAD for abstract class!"}
103            
104 1     1   3179 sub DESTROY {1} # make AUTOLOAD happy
105            
106             # end package Plotz::Translator::Generic
107            
108             1;
109