/
config-helpers.pir
302 lines (206 loc) · 5.6 KB
/
config-helpers.pir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
=head1 NAME
Glue.pir - Rakudo "glue" builtins (functions/globals) converted for NQP
=head1 SYNOPSIS
# Load this library
load_bytecode('src/lib/Glue.pbc');
# I/O
$contents := slurp($filename);
spew( $filename, $contents);
append($filename, $contents);
# Regular expressions
$regex_object := rx($regex_source);
@matches := all_matches($regex, $text);
$edited := subst($original, $regex, $replacement);
# Global variables;
our $PROGRAM_NAME;
our @ARGS;
our %ENV;
our %VM;
our $OS;
our $OSVER;
=cut
.namespace []
.include 'interpinfo.pasm'
.include 'sysinfo.pasm'
.include 'iglobals.pasm'
=head1 DESCRIPTION
=head2 Functions
=over 4
=item $contents := slurp($filename)
Read the C<$contents> of a file as a single string.
=cut
.sub 'slurp'
.param string filename
.local string contents
$P0 = open filename, 'r'
contents = $P0.'readall'()
close $P0
.return(contents)
.end
=item spew($filename, $contents)
Write the string C<$contents> to a file.
=cut
.sub 'spew'
.param string filename
.param string contents
$P0 = open filename, 'w'
$P0.'print'(contents)
close $P0
.end
=item append($filename, $contents)
Append the string C<$contents> to a file.
=cut
.sub 'append'
.param string filename
.param string contents
$P0 = open filename, 'a'
$P0.'print'(contents)
close $P0
.end
=item $regex_object := rx($regex_source)
Compile C<$regex_source> (a string representing the source code form of a
Perl 6 Regex) into a C<$regex_object>, suitable for using in C<match()> and
C<subst()>.
=cut
.sub 'rx'
.param string source
.local pmc p6regex, object
p6regex = compreg 'PGE::Perl6Regex'
object = p6regex(source)
.return(object)
.end
=item @matches := all_matches($regex, $text)
Find all matches (C<:g> style, not C<:exhaustive>) for C<$regex> in the
C<$text>. The C<$regex> must be a regex object returned by C<rx()>.
=cut
.sub 'all_matches'
.param pmc regex
.param string text
# Find all matches in the original string
.local pmc matches, match
matches = root_new ['parrot';'ResizablePMCArray']
match = regex(text)
unless match goto done_matching
match_loop:
push matches, match
$I0 = match.'to'()
match = regex(match, 'continue' => $I0)
unless match goto done_matching
goto match_loop
done_matching:
.return(matches)
.end
=item $edited := subst($original, $regex, $replacement)
Substitute all matches of the C<$regex> in the C<$original> string with the
C<$replacement>, and return the edited string. The C<$regex> must be a regex
object returned by the C<rx()> function.
The C<$replacement> may be either a simple string or a sub that will be called
with each match object in turn, and must return the proper replacement string
for that match.
=cut
.sub 'subst'
.param string original
.param pmc regex
.param pmc replacement
# Find all matches in the original string
.local pmc matches
matches = all_matches(regex, original)
# Do the substitutions on a clone of the original string
.local string edited
edited = clone original
# Now replace all the matched substrings
.local pmc match
.local int offset
offset = 0
replace_loop:
unless matches goto done_replacing
match = shift matches
# Handle either string or sub replacement
.local string replace_string
$I0 = isa replacement, 'Sub'
if $I0 goto call_replacement_sub
replace_string = replacement
goto have_replace_string
call_replacement_sub:
replace_string = replacement(match)
have_replace_string:
# Perform the replacement
$I0 = match.'from'()
$I1 = match.'to'()
$I2 = $I1 - $I0
$I0 += offset
substr edited, $I0, $I2, replace_string
$I3 = length replace_string
$I3 -= $I2
offset += $I3
goto replace_loop
done_replacing:
.return(edited)
.end
=item $joined := join($delimiter, @strings)
Join C<@strings> together with the specified C<$delimiter>.
=cut
.sub 'join'
.param string delim
.param pmc strings
.local string joined
joined = join delim, strings
.return (joined)
.end
=item @pieces := split($delimiter, $original)
Split the C<$original> string with the specified C<$delimiter>, which is not
included in the resulting C<@pieces>.
=cut
.sub 'split'
.param string delim
.param string original
.local pmc pieces
pieces = split delim, original
.return (pieces)
.end
=back
=head2 Global Variables
=over 4
=item $PROGRAM_NAME
Name of running program (argv[0] in C)
=item @ARGS
Program's command line arguments (including options, which are NOT parsed)
=item %VM
Parrot configuration
=item %ENV
Process-wide environment variables
=item $OS
Operating system generic name
=item $OSVER
Operating system version
=back
=cut
.sub 'onload' :anon :load :init
load_bytecode 'config.pbc'
$P0 = getinterp
$P1 = $P0[.IGLOBALS_CONFIG_HASH]
$P2 = new ['Hash']
$P2['config'] = $P1
set_hll_global '%VM', $P2
$P1 = $P0[.IGLOBALS_ARGV_LIST]
if $P1 goto have_args
unshift $P1, '<anonymous>'
have_args:
$S0 = shift $P1
$P2 = box $S0
set_hll_global '$PROGRAM_NAME', $P2
set_hll_global '@ARGS', $P1
$P0 = root_new ['parrot';'Env']
set_hll_global '%ENV', $P0
$S0 = sysinfo .SYSINFO_PARROT_OS
$P0 = box $S0
set_hll_global '$OS', $P0
$S0 = sysinfo .SYSINFO_PARROT_OS_VERSION
$P0 = box $S0
set_hll_global '$OSVER', $P0
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: