5 PCT::Node - base class for PAST and POST nodes
9 This file implements the base class for abstract syntax tree (PAST)
10 and opcode syntax tree (POST) nodes in the Parrot Compiler Toolkit.
14 .namespace [ 'PCT';'Node' ]
16 .sub 'onload' :anon :load :init
17 ## create the PCT::Node base class
19 p6meta = new 'P6metaclass'
20 p6meta.'new_class'('PCT::Node', 'parent'=>'Capture')
23 set_hll_global ['PCT';'Node'], '$!serno', $P0
30 C<PCT::Node> is the base class for all PAST and POST nodes. It's
31 derived from class C<Capture>, so that it has both array and hash
32 components. The array component is used to maintain a node's children,
33 while the hash component contains the attributes of the node. In general
34 we provide and use accessor methods for a node's attributes, instead
35 of accessing the hash component directly.
37 Every PAST/POST node has C<name>, C<source>, and C<pos> attributes.
38 The C<name> attribute is the node's name, if any, while C<source>
39 and C<pos> are used to identify the location in the original source
40 code for the node. The C<source> and C<pos> values are generally
41 set by the C<node> method below.
43 Other node attributes are generally defined by subclasses of C<PCT::Node>.
47 =item init([child1, child2, ..., ] [attr1=>val1, attr2=>val2, ... ])
49 Initialize a node with the given children and attributes.
50 Adds each child to the node (using the C<push> method, below) and
51 calls the appropriate accessor method for each attribute.
57 .param pmc children :slurpy
58 .param pmc adverbs :slurpy :named
63 unless it goto children_end
71 unless it goto adverbs_end
74 $P1 = find_method self, $S0
83 =item new([child1, child2, ..., ] [attr1=>val1, attr2=>val2, ...])
85 Create a new PAST node of initialized with the given
86 children and attributes. Returns the newly created node.
91 .param pmc children :slurpy
92 .param pmc adverbs :slurpy :named
95 $P0 = getattribute $P0, 'parrotclass'
97 $P1.'init'(children :flat, adverbs :flat :named)
116 Add C<child> to the beginning of the invocant's list of children.
120 Remove the first child from the invocant's list of children.
125 Add C<child> to the end of the invocant's list of children.
129 Remove the last child from the invocant's list of children.
134 .sub 'unshift' :method
155 =item push_new(class, [child1, child2, ..., ] [attr1=>val1, attr2=>val2, ...])
157 (Deprecated.) Creates a new node of type C<class>, initializes it with the
158 given children and attributes, and adds it to the end of the invocant's
159 array of children. Returns the newly created node.
163 .sub 'push_new' :method
165 .param pmc children :slurpy
166 .param pmc adverbs :slurpy :named
167 $P0 = split '::', class
169 $P0.'init'(children :flat, adverbs :flat :named)
177 Returns a newly initialized iterator for the invocant's list of
182 .sub 'iterator' :method
192 Sets the invocant's C<source> and C<pos> attributes to those
193 of C<val>. If C<val> is another PAST node, then C<source> and C<pos>
194 are simply copied from that node, otherwise C<val> is assumed to be
195 a C<Match> object and obtains source/position information from that.
202 if null node goto done
203 $I0 = isa node, ['PGE';'Match']
204 if $I0 goto node_match
205 $I0 = isa node, ['PCT';'Node']
208 $I0 = can node, 'orig'
209 unless $I0 goto err_unknown
210 $I0 = can node, 'from'
211 unless $I0 goto err_unknown
212 .local pmc source, pos
213 source = node.'orig'()
217 source = getattribute node, '$.target'
221 source = node['source']
224 self['source'] = source
231 $S0 = concat "Don't know how to save info from node of type ", $S0
238 Accessor method -- sets/returns the C<name> attribute of the invocant.
243 .param pmc value :optional
244 .param int has_value :opt_flag
245 .tailcall self.'attr'('name', value, has_value)
249 =item attr(STR attrname, PMC value, INT has_value)
251 Helper method for accessors. If C<has_value> is true then set
252 the invocant's value of C<attrname> to C<value>. Returns the
253 (resulting) value of C<attrname> in the invocant.
258 .param string attrname
261 .param pmc default :optional
262 .param int has_default :opt_flag
263 if has_value goto setattr
264 value = self[attrname]
265 unless null value goto value_done
266 unless has_default goto value_undef
273 self[attrname] = value
278 =item unique([STR fmt])
280 Generate a unique number that can be used as an identifier.
281 If C<fmt> is provided, then it will be used as a prefix to the
286 .sub 'unique' :method
287 .param string fmt :optional
288 .param int has_fmt :opt_flag
290 if has_fmt goto unique_1
293 $P0 = get_global '$!serno'
295 $S0 = concat fmt, $S0
303 Ask the current object's metaclass if C<self> is a C<type>, through its C<isa>
304 method. If so, return 1, else return 0.
311 $I0 = $P0.'isa'(self, type)
316 =item VTABLE get_bool()
318 Return true since the node is defined.
322 .sub '' :vtable('get_bool') :method
331 Patrick Michaud <pmichaud@pobox.com> is the author and maintainer.
332 Please send patches and suggestions to the Parrot porters or
333 Perl 6 compilers mailing lists.
337 2006-11-20 Patrick Michaud added first draft of POD documentation.
338 2007-11-21 Re-implementation with pdd26 compliance, compiler toolkit
339 2007-12-07 Refactor PAST::Node into separate PCT::Node component.
343 Copyright (C) 2006-2008, Parrot Foundation.
351 # vim: expandtab shiftwidth=4 ft=pir: