line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mylisp::Builtin;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21
|
use 5.012;
|
|
1
|
|
|
|
|
8
|
|
4
|
1
|
|
|
1
|
|
8
|
use experimental 'switch';
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
148
|
use Exporter;
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
129
|
|
7
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
8
|
|
|
|
|
|
|
our @EXPORT = qw(
|
9
|
|
|
|
|
|
|
End True False In Out Qstr Qint Blank Ep
|
10
|
|
|
|
|
|
|
read_file write_file croak error len
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
is_alpha is_digit is_hspace is_lower is_space
|
13
|
|
|
|
|
|
|
is_upper is_vspace is_words is_xdigit
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
start_with end_with to_end add trim repeat
|
16
|
|
|
|
|
|
|
first_char last_char rest_str cut to_chars
|
17
|
|
|
|
|
|
|
str_to_int
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
subarray to_str aflat amatch
|
20
|
|
|
|
|
|
|
apush aunshift aappend ashift ajoin asplit
|
21
|
|
|
|
|
|
|
first tail rest sort_array
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
int_to_str has_change
|
24
|
|
|
|
|
|
|
);
|
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
1
|
|
9
|
use Carp;
|
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
152
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use constant {
|
29
|
1
|
|
|
|
|
3033
|
End => chr(0),
|
30
|
|
|
|
|
|
|
True => chr(1),
|
31
|
|
|
|
|
|
|
False => chr(2),
|
32
|
|
|
|
|
|
|
In => chr(3),
|
33
|
|
|
|
|
|
|
Out => chr(4),
|
34
|
|
|
|
|
|
|
Qstr => chr(5),
|
35
|
|
|
|
|
|
|
Qint => chr(6),
|
36
|
|
|
|
|
|
|
Blank => chr(3).chr(4),
|
37
|
|
|
|
|
|
|
Ep => chr(92),
|
38
|
1
|
|
|
1
|
|
9
|
};
|
|
1
|
|
|
|
|
2
|
|
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
0
|
0
|
|
sub error { say @_; exit }
|
|
0
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub len {
|
43
|
0
|
|
|
0
|
0
|
|
my $data = shift;
|
44
|
0
|
0
|
|
|
|
|
return length($data) if ref($data) eq ref('');
|
45
|
0
|
|
|
|
|
|
return scalar(@{$data});
|
|
0
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
}
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
## =======================
|
49
|
|
|
|
|
|
|
## for char
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub is_alpha {
|
52
|
0
|
|
|
0
|
0
|
|
my $c = shift;
|
53
|
0
|
0
|
|
|
|
|
return 1 if is_lower($c);
|
54
|
0
|
0
|
|
|
|
|
return 1 if is_upper($c);
|
55
|
0
|
0
|
|
|
|
|
return 1 if $c eq '_';
|
56
|
0
|
|
|
|
|
|
return 0;
|
57
|
|
|
|
|
|
|
}
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub is_digit {
|
60
|
0
|
|
|
0
|
0
|
|
my $c = shift;
|
61
|
0
|
|
|
|
|
|
my $i = ord($c);
|
62
|
0
|
|
0
|
|
|
|
return ($i >= 48 and $i <= 57);
|
63
|
|
|
|
|
|
|
}
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub is_hspace {
|
66
|
0
|
|
|
0
|
0
|
|
my $c = shift;
|
67
|
0
|
|
0
|
|
|
|
return ($c eq ' ' or $c eq "\t");
|
68
|
|
|
|
|
|
|
}
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub is_lower {
|
71
|
0
|
|
|
0
|
0
|
|
my $c = shift;
|
72
|
0
|
|
|
|
|
|
my $i = ord($c);
|
73
|
0
|
|
0
|
|
|
|
return ($i >= 97 and $i <= 122);
|
74
|
|
|
|
|
|
|
}
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub is_space {
|
77
|
0
|
|
|
0
|
0
|
|
my $c = shift;
|
78
|
0
|
|
0
|
|
|
|
return ($c eq "\n" or
|
79
|
|
|
|
|
|
|
$c eq "\t" or
|
80
|
|
|
|
|
|
|
$c eq "\r" or
|
81
|
|
|
|
|
|
|
$c eq ' ');
|
82
|
|
|
|
|
|
|
}
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub is_upper {
|
85
|
0
|
|
|
0
|
0
|
|
my $c = shift;
|
86
|
0
|
|
|
|
|
|
my $i = ord($c);
|
87
|
0
|
|
0
|
|
|
|
return ($i >= 65 and $i <= 90);
|
88
|
|
|
|
|
|
|
}
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub is_vspace {
|
91
|
0
|
|
|
0
|
0
|
|
my $c = shift;
|
92
|
0
|
|
0
|
|
|
|
return ($c eq "\r" or $c eq "\n");
|
93
|
|
|
|
|
|
|
}
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub is_words {
|
96
|
0
|
|
|
0
|
0
|
|
my $c = shift;
|
97
|
0
|
0
|
|
|
|
|
return 1 if is_digit($c);
|
98
|
0
|
0
|
|
|
|
|
return 1 if is_alpha($c);
|
99
|
0
|
|
|
|
|
|
return 0;
|
100
|
|
|
|
|
|
|
}
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub is_xdigit {
|
103
|
0
|
|
|
0
|
0
|
|
my $c = shift;
|
104
|
0
|
0
|
|
|
|
|
return 1 if is_digit($c);
|
105
|
0
|
|
|
|
|
|
my $i = ord($c);
|
106
|
0
|
0
|
0
|
|
|
|
return 1 if $i >= 65 and $i <= 70;
|
107
|
0
|
0
|
0
|
|
|
|
return 1 if $i >= 97 and $i <= 102;
|
108
|
0
|
|
|
|
|
|
return 0;
|
109
|
|
|
|
|
|
|
}
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
## ===============================
|
112
|
|
|
|
|
|
|
## for string
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub start_with {
|
115
|
0
|
|
|
0
|
0
|
|
my ($str, $start) = @_;
|
116
|
0
|
0
|
|
|
|
|
return 1 if index($str, $start) == 0;
|
117
|
0
|
|
|
|
|
|
return 0;
|
118
|
|
|
|
|
|
|
}
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub end_with {
|
121
|
0
|
|
|
0
|
0
|
|
my ($str, $end) = @_;
|
122
|
0
|
|
|
|
|
|
my $len = length($end);
|
123
|
0
|
|
|
|
|
|
return substr($str, -$len) eq $end;
|
124
|
|
|
|
|
|
|
}
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub to_end {
|
127
|
0
|
|
|
0
|
0
|
|
my ($text, $off) = @_;
|
128
|
0
|
|
|
|
|
|
my $str = substr $text, $off;
|
129
|
0
|
|
|
|
|
|
my $index = index($str, "\n");
|
130
|
0
|
|
|
|
|
|
return substr($str, 0, $index);
|
131
|
|
|
|
|
|
|
}
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub add {
|
134
|
0
|
|
|
0
|
0
|
|
my @strs = @_;
|
135
|
0
|
|
|
|
|
|
return join '', @strs;
|
136
|
|
|
|
|
|
|
}
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub trim {
|
139
|
0
|
|
|
0
|
0
|
|
my $str = shift;
|
140
|
0
|
|
|
|
|
|
$str =~ s/^\s+|\s+$//g;
|
141
|
0
|
|
|
|
|
|
return $str;
|
142
|
|
|
|
|
|
|
}
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub repeat {
|
145
|
0
|
|
|
0
|
0
|
|
my ($str, $count) = @_;
|
146
|
0
|
|
|
|
|
|
return $str x $count;
|
147
|
|
|
|
|
|
|
}
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub first_char {
|
150
|
0
|
|
|
0
|
0
|
|
my $data = shift;
|
151
|
0
|
|
|
|
|
|
return substr $data, 0, 1;
|
152
|
|
|
|
|
|
|
}
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub last_char {
|
155
|
0
|
|
|
0
|
0
|
|
my $str = shift;
|
156
|
0
|
|
|
|
|
|
return substr $str, -1;
|
157
|
|
|
|
|
|
|
}
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub rest_str {
|
160
|
0
|
|
|
0
|
0
|
|
my $str = shift;
|
161
|
0
|
|
|
|
|
|
return substr $str, 1;
|
162
|
|
|
|
|
|
|
}
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub cut {
|
165
|
0
|
|
|
0
|
0
|
|
my $str = shift;
|
166
|
0
|
|
|
|
|
|
return substr($str, 0, -1);
|
167
|
|
|
|
|
|
|
}
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub to_chars {
|
170
|
0
|
|
|
0
|
0
|
|
my $str = shift;
|
171
|
0
|
|
|
|
|
|
return [ split '', $str ];
|
172
|
|
|
|
|
|
|
}
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub str_to_int {
|
175
|
0
|
|
|
0
|
0
|
|
my $str = shift;
|
176
|
0
|
|
|
|
|
|
return 0 + $str;
|
177
|
|
|
|
|
|
|
}
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub int_to_str {
|
180
|
0
|
|
|
0
|
0
|
|
my $int = shift;
|
181
|
0
|
|
|
|
|
|
return "$int";
|
182
|
|
|
|
|
|
|
}
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
### ===============================
|
185
|
|
|
|
|
|
|
### for array
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub subarray {
|
188
|
0
|
|
|
0
|
0
|
|
my ($array, $from, $to) = @_;
|
189
|
0
|
|
|
|
|
|
my @array = @{$array};
|
|
0
|
|
|
|
|
|
|
190
|
0
|
0
|
|
|
|
|
if ($to > 0) {
|
191
|
0
|
|
|
|
|
|
my $len = $to - $from + 1;
|
192
|
0
|
|
|
|
|
|
my $sub_array = [splice @array, $from, $len];
|
193
|
0
|
|
|
|
|
|
return $sub_array;
|
194
|
|
|
|
|
|
|
}
|
195
|
0
|
0
|
|
|
|
|
if (defined $to) {
|
196
|
0
|
|
|
|
|
|
return [splice @array, $from, $to];
|
197
|
|
|
|
|
|
|
}
|
198
|
0
|
|
|
|
|
|
return [splice @array, $from];
|
199
|
|
|
|
|
|
|
}
|
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub to_str {
|
202
|
0
|
|
|
0
|
0
|
|
my $array = shift;
|
203
|
0
|
|
|
|
|
|
return join '', @{$array};
|
|
0
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
}
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub aflat {
|
207
|
0
|
|
|
0
|
0
|
|
my $array = shift;
|
208
|
0
|
|
|
|
|
|
return $array->[0], $array->[1];
|
209
|
|
|
|
|
|
|
}
|
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub amatch {
|
212
|
0
|
|
|
0
|
0
|
|
my $array = shift;
|
213
|
0
|
|
|
|
|
|
return $array->[0], rest($array);
|
214
|
|
|
|
|
|
|
}
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
sub apush {
|
217
|
0
|
|
|
0
|
0
|
|
my ($array, $elem) = @_;
|
218
|
0
|
|
|
|
|
|
push @{$array}, $elem;
|
|
0
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
|
return $array;
|
220
|
|
|
|
|
|
|
}
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub aunshift {
|
223
|
0
|
|
|
0
|
0
|
|
my ($elem, $array) = @_;
|
224
|
0
|
|
|
|
|
|
unshift @{$array}, $elem;
|
|
0
|
|
|
|
|
|
|
225
|
0
|
|
|
|
|
|
return $array;
|
226
|
|
|
|
|
|
|
}
|
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
sub aappend {
|
229
|
0
|
|
|
0
|
0
|
|
my ($a, $b) = @_;
|
230
|
0
|
|
|
|
|
|
push @{$a}, @{$b};
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
return $a;
|
232
|
|
|
|
|
|
|
}
|
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sub ashift {
|
235
|
0
|
|
|
0
|
0
|
|
my $array = shift;
|
236
|
0
|
|
|
|
|
|
shift @{$array};
|
|
0
|
|
|
|
|
|
|
237
|
0
|
|
|
|
|
|
return 1;
|
238
|
|
|
|
|
|
|
}
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub ajoin {
|
241
|
0
|
|
|
0
|
0
|
|
my ($char, $array) = @_;
|
242
|
0
|
|
|
|
|
|
return join $char, @{$array};
|
|
0
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
}
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
sub asplit {
|
246
|
0
|
|
|
0
|
0
|
|
my ($char, $str) = @_;
|
247
|
0
|
|
|
|
|
|
return [ split $char, $str ];
|
248
|
|
|
|
|
|
|
}
|
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
sub first {
|
251
|
0
|
|
|
0
|
0
|
|
my $array = shift;
|
252
|
0
|
|
|
|
|
|
return $array->[0];
|
253
|
|
|
|
|
|
|
}
|
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
sub tail {
|
256
|
0
|
|
|
0
|
0
|
|
my $data = shift;
|
257
|
0
|
|
|
|
|
|
return $data->[-1];
|
258
|
|
|
|
|
|
|
}
|
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
sub rest {
|
261
|
0
|
|
|
0
|
0
|
|
my $data = shift;
|
262
|
0
|
|
|
|
|
|
return subarray($data, 1);
|
263
|
|
|
|
|
|
|
}
|
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub sort_array {
|
266
|
0
|
|
|
0
|
0
|
|
my $array = shift;
|
267
|
0
|
|
|
|
|
|
return [reverse sort @{$array}];
|
|
0
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
}
|
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
## =============================
|
271
|
|
|
|
|
|
|
## for file
|
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sub read_file {
|
274
|
0
|
|
|
0
|
0
|
|
my $file = shift;
|
275
|
0
|
0
|
|
|
|
|
croak("file: $file not exists") if not -e $file;
|
276
|
0
|
|
|
|
|
|
local $/;
|
277
|
0
|
0
|
|
|
|
|
open my ($fh), '<', $file or die $!;
|
278
|
0
|
|
|
|
|
|
return <$fh>;
|
279
|
|
|
|
|
|
|
}
|
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
sub write_file {
|
282
|
0
|
|
|
0
|
0
|
|
my ($file, $str) = @_;
|
283
|
0
|
0
|
|
|
|
|
open my ($fh), '>', $file or die $!;
|
284
|
0
|
|
|
|
|
|
print {$fh} $str;
|
|
0
|
|
|
|
|
|
|
285
|
0
|
|
|
|
|
|
return $file;
|
286
|
|
|
|
|
|
|
}
|
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
sub get_file_time {
|
289
|
0
|
|
|
0
|
0
|
|
my $file = shift;
|
290
|
0
|
0
|
|
|
|
|
if (not(-e $file)) {
|
291
|
0
|
|
|
|
|
|
say "$file is not exists!";
|
292
|
|
|
|
|
|
|
}
|
293
|
|
|
|
|
|
|
else {
|
294
|
0
|
|
|
|
|
|
return (stat($file))[9];
|
295
|
|
|
|
|
|
|
}
|
296
|
|
|
|
|
|
|
}
|
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
sub has_change {
|
299
|
0
|
|
|
0
|
0
|
|
my ($file, $to_file) = @_;
|
300
|
0
|
0
|
0
|
|
|
|
if ((-e $file) && (-e $to_file)) {
|
301
|
0
|
|
|
|
|
|
my $file_time = get_file_time($file);
|
302
|
0
|
|
|
|
|
|
my $to_file_time = get_file_time($to_file);
|
303
|
0
|
|
|
|
|
|
return ($file_time > $to_file_time);
|
304
|
|
|
|
|
|
|
}
|
305
|
0
|
|
|
|
|
|
return 1;
|
306
|
|
|
|
|
|
|
}
|
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
1;
|