lib/Language/Zcode/Parser/Generic.pm

filter: Statementstmt 83.2% 119 / 143 Branchbran 32.1% 9 / 28 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 96.3% 26 / 27 Podpod 100% 4 / 4 totaltotal 73.3% 162 / 221 CC 20 SCAR 21.8
CC
20
Cov
72%
find_zfile
30.5
parse_header
14.5
read_memory
3.0
CRAP 8.8
« lib/Language/Zcode/Translator/Perl.pm ↑ Summary lib/Language/Zcode/Translator/XML.pm »
1 package Language::Zcode::Parser::Generic;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 2 ▶use Language::Zcode::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 =head1 Language::Zcode::Parser::Generic
10
11 Base class for Z-code parsers.
12
13 A Parser reads and parses a Z-code file into a big Perl hash.
14
15 For finding where the subroutines start and end, you can either depend on
16 an external call to txd, a 1992 C program, or a beta pure Perl version.
17
18 Everything else is done in pure Perl.
19
20 =cut
21
22 =head2 new (class, args...)
23
24 Base class does nothing with args
25
26 =cut
27
28 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
29 2 my ($class, @arg) = @_;
30 2 bless {}, $class;
31 }
32
33 =head2 find_zfile (filename)
34
35 If the input filename is not found AND the user did not enter, e.g., '.z5' at
36 the end of the filename, the system will try to find a file ending with .z[1-9]
37 or .dat.
38
39 Multiple or no matches -> return false
40
41 =cut
42
43 0 ▶sub find_zfile {
Subroutine find_zfile: not called CC=6 SCAR=34.2
Pod documented
44 0 my ($self, $infile) = @_;
45 0 ▶ return $infile if -e $infile;
Branch: 1 branch
BranchTrueFalseTotal
if -e $infile 0 0 0
46
47 0 my $fn = ""; # filename to return
48 0 ▶ if ($infile !~ /\.(z[1-9]|dat)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $infile =~ /\.(z[1-9]|dat)$/i) { } 0 0 0
49 0 ▶ my @files = glob("$infile.z[1-9]");
Subroutine BEGIN: called CC=1 SCAR=0.2
50 0 ▶ push @files, "$infile.dat" if -e "$infile.dat";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$infile.dat" 0 0 0
51 0 ▶ if (@files == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@files == 0) { } 0 0 0
elsif (@files > 1) { } 0 0 0
52 0 warn "No file $infile.z[1-9] or $infile.dat\n";
53 } elsif (@files > 1) {
54 0 warn "Too many files match $infile.z[1-9] or $infile.dat: @files\n";
55 } else {
56 0 $fn = $files[0];
57 }
58 } else {
59 0 warn "File '$infile' not found\n";
60 }
61
62 0 return $fn;
63 }
64
65 =head2 read_memory (infile)
66
67 Reads the given Z-code file into memory
68
69 =cut
70
71 2 ▶sub read_memory {
Subroutine read_memory: called CC=3 SCAR=11.1
Pod documented
72 2 my ($self, $infile) = @_;
73 # Read in actual Z file
74 2 ▶ my $ZFILE = new IO::File "<$infile" or die "Zfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $ZFILE = "IO::File"->new("<$infile") or die "Zfile: $!" 2 0 2
75 2 binmode $ZFILE;
76 2 my $size = -s $infile;
77 2 my $q = "";
78 # Read it all into one big string, split it into an array
79 2 my $err = read($ZFILE, $q, $size);
80 2 ▶ die "Problem reading Z file from Perl: $!" unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 0 2 2
81 2 @Language::Zcode::Util::Memory = unpack('C*', $q);
82 2 close($ZFILE);
83 }
84
85 =head2 parse_header
86
87 Parse Z-code header.
88
89 Creates %Constants, which stores a bunch of constants
90 like the Z version number, where in memory things are stored, etc.
91
92 =cut
93
94 2 ▶sub parse_header {
Subroutine parse_header: called CC=13 SCAR=26.8
Pod documented
95 2 my $self = shift;
96
97 # see spec section 11:
98 2 ▶ use constant HEADER_SIZE => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
99
100 # These are all addresses in the header of various Z constants
101 2 ▶ use constant VERSION_NUMBER => 0x00;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 2 ▶ use constant RELEASE_NUMBER => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 2 ▶ use constant PAGED_MEMORY_ADDRESS => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 2 ▶ use constant FIRST_INSTRUCTION_ADDRESS => 0x06;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 2 ▶ use constant DICTIONARY_ADDRESS => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 2 ▶ use constant OBJECT_TABLE_ADDRESS => 0x0a;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 2 ▶ use constant GLOBAL_VARIABLE_ADDRESS => 0x0c;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 2 ▶ use constant STATIC_MEMORY_ADDRESS => 0x0e;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 2 ▶ use constant SERIAL_CODE => 0x12;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 ▶ use constant ABBREV_TABLE_ADDRESS => 0x18;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 2 ▶ use constant FILE_LENGTH => 0x1a;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 2 ▶ use constant CHECKSUM => 0x1c;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶ use constant INTERPRETER_NUMBER => 0x1e;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 2 ▶ use constant INTERPRETER_VERSION => 0x1f;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 ▶ use constant ROUTINES_OFFSET => 0x28;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 2 ▶ use constant STRINGS_OFFSET => 0x2a;
Subroutine BEGIN: called CC=1 SCAR=0.0
117
118 # interpreter version name; "P" for Plotz
119 2 ▶ use constant INTERPRETER_CODE => ord "P";
Subroutine BEGIN: called CC=1 SCAR=0.0
120
121 2 my %info;
122 2 my $version = $Language::Zcode::Util::Memory[VERSION_NUMBER];
123 2 ▶ if ($version < 1 or $version > 8) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version < 1 or $version > 8) { } 0 2 2
elsif ($version < 3 || $version > 5 and $version != 8) { } 0 4 4
Condition33%1/3
$version < 1 or $version > 8
l!l&&r!l&&!r
002
MC/DC0%0/2
$version < 1 or $version > 8
$version < 1 $version > 8
Truth table33%1/3
A or B
A= $version < 1B= $version > 8
ABresult
1X1
011
000
124 0 die "This does not appear to be a valid game file.\n";
125 ▶ } elsif (($version < 3 or $version > 5) and $version != 8) {
Condition33%2/6
$version < 3 || $version > 5 and $version != 8
!ll&&!rl&&r
200
$version < 3 or $version > 5
l!l&&r!l&&!r
002
MC/DC0%0/3
$version < 3 || $version > 5 and $version != 8
$version < 3 $version > 5 $version != 8
Truth table20%1/5
A or B and C
A= $version < 3B= $version > 5C= $version != 8
ABCresult
00X0
1X00
0100
1X11
0111
126 0 die "Sorry, only z-code versions 3,4,5 and 8 are supported at present...\nAnd even those need work! :)\n"
127 }
128
129 2 $info{version} = $version;
130 2 $info{release_number} = Language::Zcode::Util::get_word_at(RELEASE_NUMBER);
131 2 $info{paged_memory_address} = Language::Zcode::Util::get_word_at(PAGED_MEMORY_ADDRESS);
132 2 $info{first_instruction_address} = Language::Zcode::Util::get_word_at(FIRST_INSTRUCTION_ADDRESS);
133 2 $info{dictionary_address} = Language::Zcode::Util::get_word_at(DICTIONARY_ADDRESS);
134 2 $info{object_table_address} = Language::Zcode::Util::get_word_at(OBJECT_TABLE_ADDRESS);
135 2 $info{global_variable_address} = Language::Zcode::Util::get_word_at(GLOBAL_VARIABLE_ADDRESS);
136 2 $info{static_memory_address} = Language::Zcode::Util::get_word_at(STATIC_MEMORY_ADDRESS);
137 # see zmach06e.txt
138 2 $info{abbrev_table_address} = Language::Zcode::Util::get_word_at(ABBREV_TABLE_ADDRESS);
139 2 my $c = "";
140 2 for (SERIAL_CODE .. SERIAL_CODE + 5) {
141 12 $c .= chr Language::Zcode::Util::get_byte_at($_);
142 }
143 2 $info{serial_code} = qq{"$c"};
144
145 # set object/dictionary "constants" for this version...
146 2 ▶ if ($version <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version <= 3) { } 0 2 2
147 # 13.3, 13.4
148 0 $info{encoded_word_length} = 6;
149
150 # 12.3.1
151 0 $info{object_bytes} = 9;
152 0 $info{attribute_bytes} = 4;
153 0 $info{pointer_size} = 1;
154 0 $info{max_properties} = 31; # 12.2
155 0 $info{max_objects} = 255; # 12.3.1
156 } else {
157 2 $info{encoded_word_length} = 9;
158
159 # 12.3.2
160 2 $info{object_bytes} = 14;
161 2 $info{attribute_bytes} = 6;
162 2 $info{pointer_size} = 2;
163 2 $info{max_properties} = 63; # 12.2
164 2 $info{max_objects} = 65535; # 12.3.2
165 }
166 die("check your math!")
167 if (($info{attribute_bytes} + ($info{pointer_size} * 3) + 2)
168 2 ▶ != $info{object_bytes});
Branch: 1 branch
BranchTrueFalseTotal
if $info{"attribute_bytes"} + $info{"pointer_size"} * 3 + 2 != $info{"object_bytes"} 0 2 2
169
170 2 my $flen = Language::Zcode::Util::get_word_at(FILE_LENGTH);
171 2 ▶ if ($version <= 3) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version <= 3) { } 0 2 2
elsif ($version == 4 or $version == 5) { } 2 0 2
172 # see 11.1.6
173 0 $flen *= 2;
174 ▶ } elsif ($version == 4 || $version == 5) {
Condition33%1/3
$version == 4 or $version == 5
l!l&&r!l&&!r
020
MC/DC0%0/2
$version == 4 or $version == 5
$version == 4 $version == 5
Truth table33%1/3
A or B
A= $version == 4B= $version == 5
ABresult
1X1
011
000
175 2 $flen *= 4;
176 } else {
177 0 $flen *= 8;
178 }
179 2 $info{file_length} = $flen;
180
181 2 $info{file_checksum} = Language::Zcode::Util::get_word_at(CHECKSUM);
182
183 # Packed string/routine calculation
184 2 my %packed_mult = (1=>2, 2=>2, 3=>2, 4=>4, 5=>4, 6=>4, 7=>4, 8=>8);
185 2 $info{packed_multiplier} = $packed_mult{$version};
186 2 ▶ if ($version >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 6) { } 0 2 2
187 0 $info{routines_offset} = &Language::Zcode::Util::get_word_at(ROUTINES_OFFSET);
188 0 $info{strings_offset} = &Language::Zcode::Util::get_word_at(STRINGS_OFFSET);
189 } else {
190 2 $info{routines_offset} = 0;
191 2 $info{strings_offset} = 0;
192 }
193
194 2 %Language::Zcode::Util::Constants = %info;
195
196 # Now set any data that we know will be true in the output program
197 # interpreter number
198 # &set_byte_at(INTERPRETER_NUMBER, $interpreter_id);
199 2 &Language::Zcode::Util::set_byte_at(INTERPRETER_VERSION, INTERPRETER_CODE);
200
201 2 return;
202 }
203
204 2 1;
« lib/Language/Zcode/Translator/Perl.pm ↑ Summary lib/Language/Zcode/Translator/XML.pm »