blib/lib/ZX81/Tokenize.pm

filter: Statementstmt 70.3% 152 / 216 Branchbran 77.4% 226 / 292 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 9 / 9 Podpod 50.0% 3 / 6 totaltotal 73.9% 394 / 533 CC 150 SCAR 61.0
CC
150
Cov
74%
tokenize_line
878.0
encode_char
66.0
encode_float
5.0
CRAP 444.7
↑ Summary
1 package ZX81::Tokenize;
2  
3 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use POSIX qw(frexp);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.006';
7  
8 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our @EXPORT_OK = qw(
11 tokenize_basic
12 encode_chars
13 encode_float
14 );
15  
16 #------------------------------------------------------------------------------
17  
18 10 ▶sub tokenize_line {
Subroutine tokenize_line: called CC=75 SCAR=67.8
Pod undocumented
19 10 my ($line) = @_;
20  
21 # get line number
22 10 ▶ $line =~ s/^\s*(\d+)\s*// or die "line number expected in '$line'";
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/^\s*(\d+)\s*//u or die "line number expected in '${line}'" 10 0 10
23 10 my $line_num = $1;
24  
25 # convert line tokens
26 10 my $bin = "";
27 10 ▶ if ( $line =~ s/^\s*REM ?//i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^\s*REM ?//iu) { } 2 8 10
28  
29 # don't expand numbers and keywords within REM comments
30 2 $bin .= pack( "C", 0xEA ); # REM token
31 2 ▶ while ( $line ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line ne "") 5 2 7
32 5 ( my $char_bin, $line ) = encode_char($line);
33 5 $bin .= $char_bin;
34 }
35 2 $bin .= $line; # rest of the comment
36 2 $line = ""; # end processing this line
37 }
38 else {
39 # tokenize BASIC keywords and characters
40 8 while (1) {
41 23 $line =~ s/^\s+//;
42 23 ▶ last if $line eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 8 15 23
43  
44 15 ▶ if ( $line =~ s/^\\([0-9a-f][0-9a-f])//i ) {
Branch: 70 branches
BranchTrueFalseTotal
if ($line =~ s/^\\([0-9a-f][0-9a-f])//iu) { } 0 15 15
elsif ($line =~ s/^RND\b//iu) { } 0 15 15
elsif ($line =~ s/^INKEY\$//iu) { } 0 15 15
elsif ($line =~ s/^PI\b//iu) { } 0 15 15
elsif ($line =~ s/^AT\b//iu) { } 0 15 15
elsif ($line =~ s/^TAB\b//iu) { } 0 15 15
elsif ($line =~ s/^CODE\b//iu) { } 0 15 15
elsif ($line =~ s/^VAL\b//iu) { } 0 15 15
elsif ($line =~ s/^LEN\b//iu) { } 0 15 15
elsif ($line =~ s/^SIN\b//iu) { } 0 15 15
elsif ($line =~ s/^COS\b//iu) { } 0 15 15
elsif ($line =~ s/^TAN\b//iu) { } 0 15 15
elsif ($line =~ s/^ASN\b//iu) { } 0 15 15
elsif ($line =~ s/^ACS\b//iu) { } 0 15 15
elsif ($line =~ s/^ATN\b//iu) { } 0 15 15
elsif ($line =~ s/^LN\b//iu) { } 0 15 15
elsif ($line =~ s/^EXP\b//iu) { } 0 15 15
elsif ($line =~ s/^INT\b//iu) { } 0 15 15
elsif ($line =~ s/^SQR\b//iu) { } 0 15 15
elsif ($line =~ s/^SGN\b//iu) { } 0 15 15
elsif ($line =~ s/^ABS\b//iu) { } 0 15 15
elsif ($line =~ s/^PEEK\b//iu) { } 0 15 15
elsif ($line =~ s/^USR\b//iu) { } 0 15 15
elsif ($line =~ s/^STR\$//iu) { } 0 15 15
elsif ($line =~ s/^CHR\$//iu) { } 0 15 15
elsif ($line =~ s/^NOT\b//iu) { } 0 15 15
elsif ($line =~ s/^OR\b//iu) { } 0 15 15
elsif ($line =~ s/^AND\b//iu) { } 0 15 15
elsif ($line =~ s/^THEN\b//iu) { } 0 15 15
elsif ($line =~ s/^TO\b//iu) { } 0 15 15
elsif ($line =~ s/^STEP\b//iu) { } 0 15 15
elsif ($line =~ s/^LPRINT\b//iu) { } 0 15 15
elsif ($line =~ s/^LLIST\b//iu) { } 0 15 15
elsif ($line =~ s/^STOP\b//iu) { } 0 15 15
elsif ($line =~ s/^SLOW\b//iu) { } 0 15 15
elsif ($line =~ s/^FAST\b//iu) { } 0 15 15
elsif ($line =~ s/^NEW\b//iu) { } 0 15 15
elsif ($line =~ s/^SCROLL\b//iu) { } 0 15 15
elsif ($line =~ s/^CONT\b//iu) { } 0 15 15
elsif ($line =~ s/^DIM\b//iu) { } 0 15 15
elsif ($line =~ s/^REM\b//iu) { } 0 15 15
elsif ($line =~ s/^FOR\b//iu) { } 0 15 15
elsif ($line =~ s/^GOTO\b//iu) { } 1 14 15
elsif ($line =~ s/^GOSUB\b//iu) { } 0 14 14
elsif ($line =~ s/^INPUT\b//iu) { } 0 14 14
elsif ($line =~ s/^LOAD\b//iu) { } 0 14 14
elsif ($line =~ s/^LIST\b//iu) { } 0 14 14
elsif ($line =~ s/^LET\b//iu) { } 1 13 14
elsif ($line =~ s/^PAUSE\b//iu) { } 0 13 13
elsif ($line =~ s/^NEXT\b//iu) { } 0 13 13
elsif ($line =~ s/^POKE\b//iu) { } 0 13 13
elsif ($line =~ s/^PRINT\b//iu) { } 3 10 13
elsif ($line =~ s/^PLOT\b//iu) { } 0 10 10
elsif ($line =~ s/^RUN\b//iu) { } 3 7 10
elsif ($line =~ s/^SAVE\b//iu) { } 0 7 7
elsif ($line =~ s/^RAND\b//iu) { } 0 7 7
elsif ($line =~ s/^IF\b//iu) { } 0 7 7
elsif ($line =~ s/^CLS\b//iu) { } 0 7 7
elsif ($line =~ s/^UNPLOT\b//iu) { } 0 7 7
elsif ($line =~ s/^CLEAR\b//iu) { } 0 7 7
elsif ($line =~ s/^RETURN\b//iu) { } 0 7 7
elsif ($line =~ s/^COPY\b//iu) { } 0 7 7
elsif ($line =~ s/^\\\"//iu) { } 0 7 7
elsif ($line =~ s/^\*\*//iu) { } 0 7 7
elsif ($line =~ s/^<=//iu) { } 0 7 7
elsif ($line =~ s/^>=//iu) { } 0 7 7
elsif ($line =~ s/^<>//iu) { } 0 7 7
elsif ($line =~ s/^((\d*\.\d+|\d+\.\d*|\d+)(e[-+]?\d+)?)//iu) { } 3 4 7
elsif ($line =~ s/^(\w+)//iu) { } 1 3 4
elsif ($line =~ s/^\"//iu) { } 2 1 3
45 0 $bin .= pack( "C", hex($1) );
46 }
47 0 elsif ( $line =~ s/^RND\b//i ) { $bin .= pack( "C", 0x40 ); }
48 0 elsif ( $line =~ s/^INKEY\$//i ) { $bin .= pack( "C", 0x41 ); }
49 0 elsif ( $line =~ s/^PI\b//i ) { $bin .= pack( "C", 0x42 ); }
50 0 elsif ( $line =~ s/^AT\b//i ) { $bin .= pack( "C", 0xC1 ); }
51 0 elsif ( $line =~ s/^TAB\b//i ) { $bin .= pack( "C", 0xC2 ); }
52 0 elsif ( $line =~ s/^CODE\b//i ) { $bin .= pack( "C", 0xC4 ); }
53 0 elsif ( $line =~ s/^VAL\b//i ) { $bin .= pack( "C", 0xC5 ); }
54 0 elsif ( $line =~ s/^LEN\b//i ) { $bin .= pack( "C", 0xC6 ); }
55 0 elsif ( $line =~ s/^SIN\b//i ) { $bin .= pack( "C", 0xC7 ); }
56 0 elsif ( $line =~ s/^COS\b//i ) { $bin .= pack( "C", 0xC8 ); }
57 0 elsif ( $line =~ s/^TAN\b//i ) { $bin .= pack( "C", 0xC9 ); }
58 0 elsif ( $line =~ s/^ASN\b//i ) { $bin .= pack( "C", 0xCA ); }
59 0 elsif ( $line =~ s/^ACS\b//i ) { $bin .= pack( "C", 0xCB ); }
60 0 elsif ( $line =~ s/^ATN\b//i ) { $bin .= pack( "C", 0xCC ); }
61 0 elsif ( $line =~ s/^LN\b//i ) { $bin .= pack( "C", 0xCD ); }
62 0 elsif ( $line =~ s/^EXP\b//i ) { $bin .= pack( "C", 0xCE ); }
63 0 elsif ( $line =~ s/^INT\b//i ) { $bin .= pack( "C", 0xCF ); }
64 0 elsif ( $line =~ s/^SQR\b//i ) { $bin .= pack( "C", 0xD0 ); }
65 0 elsif ( $line =~ s/^SGN\b//i ) { $bin .= pack( "C", 0xD1 ); }
66 0 elsif ( $line =~ s/^ABS\b//i ) { $bin .= pack( "C", 0xD2 ); }
67 0 elsif ( $line =~ s/^PEEK\b//i ) { $bin .= pack( "C", 0xD3 ); }
68 0 elsif ( $line =~ s/^USR\b//i ) { $bin .= pack( "C", 0xD4 ); }
69 0 elsif ( $line =~ s/^STR\$//i ) { $bin .= pack( "C", 0xD5 ); }
70 0 elsif ( $line =~ s/^CHR\$//i ) { $bin .= pack( "C", 0xD6 ); }
71 0 elsif ( $line =~ s/^NOT\b//i ) { $bin .= pack( "C", 0xD7 ); }
72 0 elsif ( $line =~ s/^OR\b//i ) { $bin .= pack( "C", 0xD9 ); }
73 0 elsif ( $line =~ s/^AND\b//i ) { $bin .= pack( "C", 0xDA ); }
74 0 elsif ( $line =~ s/^THEN\b//i ) { $bin .= pack( "C", 0xDE ); }
75 0 elsif ( $line =~ s/^TO\b//i ) { $bin .= pack( "C", 0xDF ); }
76 0 elsif ( $line =~ s/^STEP\b//i ) { $bin .= pack( "C", 0xE0 ); }
77 0 elsif ( $line =~ s/^LPRINT\b//i ) { $bin .= pack( "C", 0xE1 ); }
78 0 elsif ( $line =~ s/^LLIST\b//i ) { $bin .= pack( "C", 0xE2 ); }
79 0 elsif ( $line =~ s/^STOP\b//i ) { $bin .= pack( "C", 0xE3 ); }
80 0 elsif ( $line =~ s/^SLOW\b//i ) { $bin .= pack( "C", 0xE4 ); }
81 0 elsif ( $line =~ s/^FAST\b//i ) { $bin .= pack( "C", 0xE5 ); }
82 0 elsif ( $line =~ s/^NEW\b//i ) { $bin .= pack( "C", 0xE6 ); }
83 0 elsif ( $line =~ s/^SCROLL\b//i ) { $bin .= pack( "C", 0xE7 ); }
84 0 elsif ( $line =~ s/^CONT\b//i ) { $bin .= pack( "C", 0xE8 ); }
85 0 elsif ( $line =~ s/^DIM\b//i ) { $bin .= pack( "C", 0xE9 ); }
86 0 elsif ( $line =~ s/^REM\b//i ) { $bin .= pack( "C", 0xEA ); }
87 0 elsif ( $line =~ s/^FOR\b//i ) { $bin .= pack( "C", 0xEB ); }
88 1 elsif ( $line =~ s/^GOTO\b//i ) { $bin .= pack( "C", 0xEC ); }
89 0 elsif ( $line =~ s/^GOSUB\b//i ) { $bin .= pack( "C", 0xED ); }
90 0 elsif ( $line =~ s/^INPUT\b//i ) { $bin .= pack( "C", 0xEE ); }
91 0 elsif ( $line =~ s/^LOAD\b//i ) { $bin .= pack( "C", 0xEF ); }
92 0 elsif ( $line =~ s/^LIST\b//i ) { $bin .= pack( "C", 0xF0 ); }
93 1 elsif ( $line =~ s/^LET\b//i ) { $bin .= pack( "C", 0xF1 ); }
94 0 elsif ( $line =~ s/^PAUSE\b//i ) { $bin .= pack( "C", 0xF2 ); }
95 0 elsif ( $line =~ s/^NEXT\b//i ) { $bin .= pack( "C", 0xF3 ); }
96 0 elsif ( $line =~ s/^POKE\b//i ) { $bin .= pack( "C", 0xF4 ); }
97 3 elsif ( $line =~ s/^PRINT\b//i ) { $bin .= pack( "C", 0xF5 ); }
98 0 elsif ( $line =~ s/^PLOT\b//i ) { $bin .= pack( "C", 0xF6 ); }
99 3 elsif ( $line =~ s/^RUN\b//i ) { $bin .= pack( "C", 0xF7 ); }
100 0 elsif ( $line =~ s/^SAVE\b//i ) { $bin .= pack( "C", 0xF8 ); }
101 0 elsif ( $line =~ s/^RAND\b//i ) { $bin .= pack( "C", 0xF9 ); }
102 0 elsif ( $line =~ s/^IF\b//i ) { $bin .= pack( "C", 0xFA ); }
103 0 elsif ( $line =~ s/^CLS\b//i ) { $bin .= pack( "C", 0xFB ); }
104 0 elsif ( $line =~ s/^UNPLOT\b//i ) { $bin .= pack( "C", 0xFC ); }
105 0 elsif ( $line =~ s/^CLEAR\b//i ) { $bin .= pack( "C", 0xFD ); }
106 0 elsif ( $line =~ s/^RETURN\b//i ) { $bin .= pack( "C", 0xFE ); }
107 0 elsif ( $line =~ s/^COPY\b//i ) { $bin .= pack( "C", 0xFF ); }
108 0 elsif ( $line =~ s/^\\\"//i ) { $bin .= pack( "C", 0xC0 ); }
109 0 elsif ( $line =~ s/^\*\*//i ) { $bin .= pack( "C", 0xD8 ); }
110 0 elsif ( $line =~ s/^<=//i ) { $bin .= pack( "C", 0xDB ); }
111 0 elsif ( $line =~ s/^>=//i ) { $bin .= pack( "C", 0xDC ); }
112 0 elsif ( $line =~ s/^<>//i ) { $bin .= pack( "C", 0xDD ); }
113 elsif ( $line =~ s/^((\d*\.\d+|\d+\.\d*|\d+)(e[-+]?\d+)?)//i ) {
114  
115 # number
116 3 my $num = $1;
117 3 my $num_chars = encode_chars($num);
118 3 my $num_bin = encode_float($num);
119 3 $bin .= $num_chars . pack( "C", 0x7E ) . $num_bin;
120 }
121 elsif ( $line =~ s/^(\w+)//i ) {
122  
123 # identifier
124 1 my $ident = $1;
125 1 $bin .= encode_chars($ident);
126 }
127 elsif ( $line =~ s/^\"//i ) {
128  
129 # string
130 2 ( my $str_bin, $line ) = encode_string($line);
131 2 $bin .= $str_bin;
132 }
133 else {
134 1 ( my $char_bin, $line ) = encode_char($line);
135 1 $bin .= $char_bin;
136 }
137 }
138 }
139  
140 # appends newline
141 10 $bin .= pack( "C", 0x76 );
142  
143 # pack line number, size and packed line
144 10 my $size = length($bin);
145 10 return pack( "nv", $line_num, $size ) . $bin;
146 }
147  
148 10 ▶sub tokenize_basic {
Subroutine tokenize_basic: called CC=3 SCAR=11.0
Pod documented
149 10 my ($basic) = @_;
150 10 my $bin = "";
151 10 $basic =~ s/\\\n/ /g; # join continuation lines
152 10 for my $line ( split( /\n/, $basic ) ) {
153 14 $line =~ s/^\s*#.*//; # remove comments
154 14 ▶ next unless $line =~ /\S/; # ignore empty lines
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/u 4 10 14
155  
156 10 $bin .= tokenize_line($line);
157 }
158 10 return $bin;
159 }
160  
161 #------------------------------------------------------------------------------
162 462 ▶sub encode_char {
Subroutine encode_char: called CC=66 SCAR=41.9
Pod undocumented
163 462 my ($text) = @_;
164  
165 462 my $bin;
166 462 ▶ if ( $text =~ s/^\\([0-9a-f][0-9a-f])//i ) { $bin = pack( "C", hex($1) ); }
Branch: 65 branches
BranchTrueFalseTotal
if ($text =~ s/^\\([0-9a-f][0-9a-f])//iu) { } 260 202 462
elsif ($text =~ s/^\\ //iu) { } 1 201 202
elsif ($text =~ s/^\\' //iu) { } 1 200 201
elsif ($text =~ s/^\\ '//iu) { } 1 199 200
elsif ($text =~ s/^\\''//iu) { } 1 198 199
elsif ($text =~ s/^\\\. //iu) { } 1 197 198
elsif ($text =~ s/^\\: //iu) { } 1 196 197
elsif ($text =~ s/^\\\.'//iu) { } 1 195 196
elsif ($text =~ s/^\\:'//iu) { } 1 194 195
elsif ($text =~ s/^\\##//iu) { } 1 193 194
elsif ($text =~ s/^\\,,//iu) { } 1 192 193
elsif ($text =~ s/^\\~~//iu) { } 1 191 192
elsif ($text =~ s/^\\:://iu) { } 1 190 191
elsif ($text =~ s/^\\\.://iu) { } 1 189 190
elsif ($text =~ s/^\\:\.//iu) { } 1 188 189
elsif ($text =~ s/^\\\.\.//iu) { } 1 187 188
elsif ($text =~ s/^\\'://iu) { } 1 186 187
elsif ($text =~ s/^\\ ://iu) { } 1 185 186
elsif ($text =~ s/^\\'\.//iu) { } 1 184 185
elsif ($text =~ s/^\\ \.//iu) { } 1 183 184
elsif ($text =~ s/^\\\@\@//iu) { } 1 182 183
elsif ($text =~ s/^\\;;//iu) { } 1 181 182
elsif ($text =~ s/^\\!!//iu) { } 1 180 181
elsif ($text =~ s/^\n//iu) { } 1 179 180
elsif ($text =~ s/^ //iu) { } 1 178 179
elsif ($text =~ s/^\"//iu) { } 3 175 178
elsif ($text =~ s/^\#//iu) { } 1 174 175
elsif ($text =~ s/^\$//iu) { } 1 173 174
elsif ($text =~ s/^://iu) { } 1 172 173
elsif ($text =~ s/^\?//iu) { } 1 171 172
elsif ($text =~ s/^\(//iu) { } 1 170 171
elsif ($text =~ s/^\)//iu) { } 1 169 170
elsif ($text =~ s/^>//iu) { } 1 168 169
elsif ($text =~ s/^<//iu) { } 1 167 168
elsif ($text =~ s/^=//iu) { } 2 165 167
elsif ($text =~ s/^\+//iu) { } 1 164 165
elsif ($text =~ s/^-//iu) { } 1 163 164
elsif ($text =~ s/^\*//iu) { } 1 162 163
elsif ($text =~ s[^/][]iu) { } 1 161 162
elsif ($text =~ s/^;//iu) { } 1 160 161
elsif ($text =~ s/^,//iu) { } 1 159 160
elsif ($text =~ s/^\.//iu) { } 1 158 159
elsif ($text =~ s/^(\d)//iu) { } 16 142 158
elsif ($text =~ s/^(\w)//iu) { } 59 83 142
elsif ($text =~ s/^\% //iu) { } 1 82 83
elsif ($text =~ s/^\%\"//iu) { } 1 81 82
elsif ($text =~ s/^\%\#//iu) { } 1 80 81
elsif ($text =~ s/^\%\$//iu) { } 1 79 80
elsif ($text =~ s/^\%://iu) { } 1 78 79
elsif ($text =~ s/^\%\?//iu) { } 1 77 78
elsif ($text =~ s/^\%\(//iu) { } 1 76 77
elsif ($text =~ s/^\%\)//iu) { } 1 75 76
elsif ($text =~ s/^\%>//iu) { } 1 74 75
elsif ($text =~ s/^\%<//iu) { } 1 73 74
elsif ($text =~ s/^\%=//iu) { } 1 72 73
elsif ($text =~ s/^\%\+//iu) { } 1 71 72
elsif ($text =~ s/^\%-//iu) { } 1 70 71
elsif ($text =~ s/^\%\*//iu) { } 1 69 70
elsif ($text =~ s[^\%/][]iu) { } 1 68 69
elsif ($text =~ s/^\%;//iu) { } 1 67 68
elsif ($text =~ s/^\%,//iu) { } 1 66 67
elsif ($text =~ s/^\%\.//iu) { } 1 65 66
elsif ($text =~ s/^\%(\d)//iu) { } 10 55 65
elsif ($text =~ s/^\%(\w)//iu) { } 52 3 55
elsif ($text =~ s/^\\\"//iu) { } 2 1 3
167 1 elsif ( $text =~ s/^\\ //i ) { $bin = pack( "C", 0x00 ); }
168 1 elsif ( $text =~ s/^\\' //i ) { $bin = pack( "C", 0x01 ); }
169 1 elsif ( $text =~ s/^\\ '//i ) { $bin = pack( "C", 0x02 ); }
170 1 elsif ( $text =~ s/^\\''//i ) { $bin = pack( "C", 0x03 ); }
171 1 elsif ( $text =~ s/^\\\. //i ) { $bin = pack( "C", 0x04 ); }
172 1 elsif ( $text =~ s/^\\: //i ) { $bin = pack( "C", 0x05 ); }
173 1 elsif ( $text =~ s/^\\\.'//i ) { $bin = pack( "C", 0x06 ); }
174 1 elsif ( $text =~ s/^\\:'//i ) { $bin = pack( "C", 0x07 ); }
175 1 elsif ( $text =~ s/^\\##//i ) { $bin = pack( "C", 0x08 ); }
176 1 elsif ( $text =~ s/^\\,,//i ) { $bin = pack( "C", 0x09 ); }
177 1 elsif ( $text =~ s/^\\~~//i ) { $bin = pack( "C", 0x0a ); }
178 1 elsif ( $text =~ s/^\\:://i ) { $bin = pack( "C", 0x80 ); }
179 1 elsif ( $text =~ s/^\\\.://i ) { $bin = pack( "C", 0x81 ); }
180 1 elsif ( $text =~ s/^\\:\.//i ) { $bin = pack( "C", 0x82 ); }
181 1 elsif ( $text =~ s/^\\\.\.//i ) { $bin = pack( "C", 0x83 ); }
182 1 elsif ( $text =~ s/^\\'://i ) { $bin = pack( "C", 0x84 ); }
183 1 elsif ( $text =~ s/^\\ ://i ) { $bin = pack( "C", 0x85 ); }
184 1 elsif ( $text =~ s/^\\'\.//i ) { $bin = pack( "C", 0x86 ); }
185 1 elsif ( $text =~ s/^\\ \.//i ) { $bin = pack( "C", 0x87 ); }
186 1 elsif ( $text =~ s/^\\\@\@//i ) { $bin = pack( "C", 0x88 ); }
187 1 elsif ( $text =~ s/^\\;;//i ) { $bin = pack( "C", 0x89 ); }
188 1 elsif ( $text =~ s/^\\!!//i ) { $bin = pack( "C", 0x8a ); }
189 1 elsif ( $text =~ s/^\n//i ) { $bin = pack( "C", 0x76 ); }
190 1 elsif ( $text =~ s/^ //i ) { $bin = pack( "C", 0x00 ); }
191 3 elsif ( $text =~ s/^\"//i ) { $bin = pack( "C", 0x0b ); }
192 1 elsif ( $text =~ s/^\#//i ) { $bin = pack( "C", 0x0c ); }
193 1 elsif ( $text =~ s/^\$//i ) { $bin = pack( "C", 0x0d ); }
194 1 elsif ( $text =~ s/^://i ) { $bin = pack( "C", 0x0e ); }
195 1 elsif ( $text =~ s/^\?//i ) { $bin = pack( "C", 0x0f ); }
196 1 elsif ( $text =~ s/^\(//i ) { $bin = pack( "C", 0x10 ); }
197 1 elsif ( $text =~ s/^\)//i ) { $bin = pack( "C", 0x11 ); }
198 1 elsif ( $text =~ s/^>//i ) { $bin = pack( "C", 0x12 ); }
199 1 elsif ( $text =~ s/^<//i ) { $bin = pack( "C", 0x13 ); }
200 2 elsif ( $text =~ s/^=//i ) { $bin = pack( "C", 0x14 ); }
201 1 elsif ( $text =~ s/^\+//i ) { $bin = pack( "C", 0x15 ); }
202 1 elsif ( $text =~ s/^-//i ) { $bin = pack( "C", 0x16 ); }
203 1 elsif ( $text =~ s/^\*//i ) { $bin = pack( "C", 0x17 ); }
204 1 elsif ( $text =~ s/^\///i ) { $bin = pack( "C", 0x18 ); }
205 1 elsif ( $text =~ s/^;//i ) { $bin = pack( "C", 0x19 ); }
206 1 elsif ( $text =~ s/^,//i ) { $bin = pack( "C", 0x1a ); }
207 1 elsif ( $text =~ s/^\.//i ) { $bin = pack( "C", 0x1b ); }
208 elsif ( $text =~ s/^(\d)//i ) {
209 16 $bin = pack( "C", 0x1c + ord($1) - ord('0') );
210 }
211 elsif ( $text =~ s/^(\w)//i ) {
212 59 $bin = pack( "C", 0x26 + ord( uc($1) ) - ord('A') );
213 }
214 1 elsif ( $text =~ s/^\% //i ) { $bin = pack( "C", 0x80 ); }
215 1 elsif ( $text =~ s/^\%\"//i ) { $bin = pack( "C", 0x8b ); }
216 1 elsif ( $text =~ s/^\%\#//i ) { $bin = pack( "C", 0x8c ); }
217 1 elsif ( $text =~ s/^\%\$//i ) { $bin = pack( "C", 0x8d ); }
218 1 elsif ( $text =~ s/^\%://i ) { $bin = pack( "C", 0x8e ); }
219 1 elsif ( $text =~ s/^\%\?//i ) { $bin = pack( "C", 0x8f ); }
220 1 elsif ( $text =~ s/^\%\(//i ) { $bin = pack( "C", 0x90 ); }
221 1 elsif ( $text =~ s/^\%\)//i ) { $bin = pack( "C", 0x91 ); }
222 1 elsif ( $text =~ s/^\%>//i ) { $bin = pack( "C", 0x92 ); }
223 1 elsif ( $text =~ s/^\%<//i ) { $bin = pack( "C", 0x93 ); }
224 1 elsif ( $text =~ s/^\%=//i ) { $bin = pack( "C", 0x94 ); }
225 1 elsif ( $text =~ s/^\%\+//i ) { $bin = pack( "C", 0x95 ); }
226 1 elsif ( $text =~ s/^\%-//i ) { $bin = pack( "C", 0x96 ); }
227 1 elsif ( $text =~ s/^\%\*//i ) { $bin = pack( "C", 0x97 ); }
228 1 elsif ( $text =~ s/^\%\///i ) { $bin = pack( "C", 0x98 ); }
229 1 elsif ( $text =~ s/^\%;//i ) { $bin = pack( "C", 0x99 ); }
230 1 elsif ( $text =~ s/^\%,//i ) { $bin = pack( "C", 0x9a ); }
231 1 elsif ( $text =~ s/^\%\.//i ) { $bin = pack( "C", 0x9b ); }
232 elsif ( $text =~ s/^\%(\d)//i ) {
233 10 $bin = pack( "C", 0x9c + ord($1) - ord('0') );
234 }
235 elsif ( $text =~ s/^\%(\w)//i ) {
236 52 $bin = pack( "C", 0xa6 + ord( uc($1) ) - ord('A') );
237 }
238 2 elsif ( $text =~ s/^\\\"//i ) { $bin .= pack( "C", 0xC0 ); }
239 1 else { die "cannot encode '$text'"; }
240  
241 461 return ( $bin, $text );
242 }
243  
244 # enters with the starting quote already consumed
245 2 ▶sub encode_string {
Subroutine encode_string: called CC=4 SCAR=14.1
Pod undocumented
246 2 my ($text) = @_;
247 2 my $orig_text = '"' . $text;
248  
249 2 my $str_bin = "";
250 2 ▶ while ( $text ne "" && $text !~ /^\"/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne "" and not $text =~ /^\"/iu) 8 2 10
Condition66%2/3
$text ne "" and not $text =~ /^\"/iu
!ll&&!rl&&r
028
MC/DC50%1/2
$text ne "" and not $text =~ /^\"/iu
$text ne "" not $text =~ /^\"/iu
Truth table66%2/3
A and not(B)
A= $text ne ""B= not $text =~ /^\"/iu
ABresult
0X0
100
111
251 8 ( my $char_bin, $text ) = encode_char($text);
252 8 $str_bin .= $char_bin;
253 }
254 2 ▶ if ( $text =~ /^\"/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^\"/iu) { } 2 0 2
255 2 ( my $quote_bin, $text ) = encode_char($text);
256 2 return ( $quote_bin . $str_bin . $quote_bin, $text );
257 }
258 else {
259 0 die "unmatched '\"' in $orig_text";
260 }
261 }
262  
263 14 ▶sub encode_chars {
Subroutine encode_chars: called CC=2 SCAR=6.9
Pod documented
264 14 my ($text) = @_;
265 14 my $bin = "";
266 14 ▶ while ( $text ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne "") 446 13 459
267 446 ( my $char_bin, $text ) = encode_char($text);
268 445 $bin .= $char_bin;
269 }
270 13 return $bin;
271 }
272  
273 #------------------------------------------------------------------------------
274  
275 23 ▶sub encode_float {
Subroutine encode_float: called CC=5 SCAR=16.2
Pod documented
276 23 my ($value) = @_;
277  
278 # Special case zero
279 23 ▶ if ( $value == 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == 0) 2 21 23
280 2 return pack( "C5", 0, 0, 0, 0, 0 );
281 }
282  
283 # Work with the absolute value
284 21 my $sign = ( $value < 0 );
285 21 $value = abs($value);
286  
287 # Compute mantissa and exponent
288 # frexp gives:
289 # num = frac * 2**exp2
290 # with 0.5 <= frac < 1
291 21 my ( $frac, $exp2 ) = frexp($value);
292 21 my $exp = $exp2 - 1;
293  
294 21 ▶ die "$value out of range" unless $exp >= -129 && $exp <= 126;
Branch: 1 branch
BranchTrueFalseTotal
unless $exp >= -129 and $exp <= 126 0 21 21
Condition33%1/3
$exp >= -129 and $exp <= 126
!ll&&!rl&&r
0021
MC/DC0%0/2
$exp >= -129 and $exp <= 126
$exp >= -129 $exp <= 126
Truth table33%1/3
A and B
A= $exp >= -129B= $exp <= 126
ABresult
0X0
100
111
295  
296 21 my $normalized = $value / ( 2**$exp );
297 21 my $man = int( ( $normalized - 1.0 ) * 0x80000000 + 0.5 );
298  
299 # Apply the sign bit
300 21 ▶ if ($sign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign) { } 9 12 21
301 9 $man |= 0x80000000; # set sign bit
302 }
303 else {
304 12 $man &= 0x7fffffff; # clear sign bit
305 }
306  
307 # Apply exponent bias
308 21 $exp += 129;
309  
310 # pack the result
311 21 return pack( "C C4",
312 $exp,
313 ( $man >> 24 ) & 0xFF,
314 ( $man >> 16 ) & 0xFF,
315 ( $man >> 8 ) & 0xFF,
316 $man & 0xFF );
317 }
318  
319 3 1;
320  
321 __END__
↑ Summary