lib/Language/Zcode/Translator/Generic.pm

filter: Statementstmt 84.0% 21 / 25 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 80.0% 4 / 5 totaltotal 78.7% 37 / 47 CC 5 SCAR 8.4
CC
5
Cov
82%
packed_address_str
4.0
mult_add_str
2.1
program_start
2.0
CRAP 2.3
« lib/Language/Zcode/Translator/XML.pm ↑ Summary lib/Language/Zcode/Parser/Routine.pm »
1 package Language::Zcode::Translator::Generic;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶sub program_start { "" }
Subroutine program_start: not called CC=1 SCAR=6.9
Pod documented
33 1 ▶sub program_end { "" }
Subroutine program_end: called CC=1 SCAR=0.0
Pod documented
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 ▶sub library { "" }
Subroutine library: not called CC=1 SCAR=6.9
Pod undocumented
65
66 372 ▶sub packed_address_str {
Subroutine packed_address_str: called CC=4 SCAR=13.9
Pod documented
67 372 my ($self, $address, $key) = @_;
68 372 my %c = %Language::Zcode::Util::Constants;
69 372 my $mult = $c{packed_multiplier};
70 372 my $add;
71 # (Add will be zero for versions not 6 or 7)
72 372 ▶ if ($key eq "routine") {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'routine') { } 365 7 372
elsif ($key eq 'packed_address_of_string') { } 7 0 7
73 365 $add = 8 * $c{routines_offset};
74 } elsif ($key eq "packed_address_of_string") {
75 7 $add = 8 * $c{strings_offset};
76 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 ▶ if ($address =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($address =~ /^\d+$/) { } 366 6 372
80 # if ($address =~ /^(sp|local\d+|g[a-f\d]{2})$/) {
81 366 return $mult * $address + $add;
82 } else {
83 6 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 6 ▶sub mult_add_str {
Subroutine mult_add_str: called CC=2 SCAR=7.2
Pod documented
95 6 my ($self, $thing, $mult, $add) = @_;
96 # works for Perl, C, BASIC, java...
97 6 ▶ return $add ? "$thing * $mult + $add" : "$thing * $mult";
Branch: 1 branch
BranchTrueFalseTotal
$add ? : 0 6 6
98
99 }
100
101 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
102 0 die "Tried to call $Plotz::Translator::Generic::AUTOLOAD for abstract class!"}
103
104 1 ▶sub DESTROY {1} # make AUTOLOAD happy
Subroutine DESTROY: called CC=1 SCAR=0.0
105
106 # end package Plotz::Translator::Generic
107
108 1 1;
109
« lib/Language/Zcode/Translator/XML.pm ↑ Summary lib/Language/Zcode/Parser/Routine.pm »