-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Capture.pir
180 lines (122 loc) Β· 3.13 KB
/
Capture.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
## $Id$
=head1 TITLE
Capture - Perl 6 Capture class
=head1 DESCRIPTION
This file sets up the Perl 6 C<Capture> class.
=cut
.namespace ['Capture']
.sub 'onload' :anon :init :load
.local pmc p6meta, captureproto
p6meta = get_hll_global ['Mu'], '$!P6META'
captureproto = p6meta.'new_class'('Capture', 'parent'=>'Any', 'attr'=>'$!pos $!named')
.end
=head2 Methods
=over 4
=item new
Takes a bunch of positional and named arguments and builds a capture from
them.
=cut
.sub 'new' :method
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named
# Create capture.
$P0 = self.'CREATE'('P6opaque')
setattribute $P0, '$!pos', pos_args
setattribute $P0, '$!named', named_args
.return ($P0)
.end
=item postcircumfix:<[ ]>
=cut
.sub 'postcircumfix:<[ ]>' :method :vtable('get_pmc_keyed_int')
.param int i
$P0 = getattribute self, '$!pos'
$P0 = $P0[i]
.return ($P0)
.end
=item postcircumfix:<{ }>
=cut
.sub 'postcircumfix:<{ }>' :method :vtable('get_pmc_keyed_str')
.param pmc key
$P0 = getattribute self, '$!named'
$P0 = $P0[key]
.return ($P0)
.end
=item elems
The number of positional elements in the Capture.
=cut
.sub 'elems' :method :vtable('elements')
$P0 = getattribute self, '$!pos'
$I0 = elements $P0
.return ($I0)
.end
=item hash
XXX Returns Parrot Hash, not Perl 6 Hash.
=cut
.sub 'hash' :method
$P0 = getattribute self, '$!named'
.return ($P0)
.end
=item !PARROT_POSITIONALS
Gets a Parrot RPA that we can use :flat on.
=cut
.sub '!PARROT_POSITIONALS' :method
$P0 = getattribute self, '$!pos'
.return ($P0)
.end
=item !PARROT_NAMEDS
Gets a Parrot Hash that we can use :flat :named on.
=cut
.sub '!PARROT_NAMEDS' :method
$P0 = getattribute self, '$!named'
.return ($P0)
.end
=back
=head2 Functions
=over 4
=item !snapshot_capture
Snapshots the current capture cursor.
Well, akshually...until we implement Capture Cursor it just kinda pretends
to. :-) Hands back a Capture containing the snapshot.
=cut
.namespace []
.sub '!snapshot_capture'
.param pmc capture
.param int pos_position
.param pmc nameds_unbound
.local int num_positionals
.local pmc positionals, nameds
num_positionals = elements capture
positionals = root_new ['parrot';'ResizablePMCArray']
nameds = root_new ['parrot';'Hash']
# Copy positionals.
pos_loop:
if pos_position >= num_positionals goto pos_loop_end
$P0 = capture[pos_position]
push positionals, $P0
inc pos_position
goto pos_loop
pos_loop_end:
# Copy still unbound named parameters.
if null nameds_unbound goto named_loop_end
$P0 = iter nameds_unbound
named_loop:
unless $P0 goto named_loop_end
$S0 = shift $P0
$P1 = capture[$S0]
nameds[$S0] = $P1
goto named_loop
named_loop_end:
# Finally, create capture.
$P0 = get_hll_global 'Capture'
$P0 = $P0.'CREATE'('P6opaque')
setattribute $P0, '$!pos', positionals
setattribute $P0, '$!named', nameds
.return ($P0)
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: