3 PCT::Node - base class for PAST and POST nodes
7 This file implements the base class for abstract syntax tree (PAST)
8 and opcode syntax tree (POST) nodes in the Parrot Compiler Toolkit.
12 .namespace [ 'PCT::Node' ]
14 .sub 'onload' :anon :load :init
15 ## create the PCT::Node base class
16 ## FIXME: Eventually we want this to be a subclass of
17 ## Capture, but as of now Capture isn't working so we
18 ## use the Capture_PIR class for now.
19 load_bytecode 'Parrot/Capture_PIR.pbc'
22 p6meta = new 'P6metaclass'
23 p6meta.'new_class'('PCT::Node', 'parent'=>'Capture_PIR')
27 set_hll_global ['PCT::Node'], '$!serno', $P0
34 C<PCT::Node> is the base class for all PAST and POST nodes. It's
35 derived from class C<Capture>, so that it has both array and hash
36 components. The array component is used to maintain a node's children,
37 while the hash component contains the attributes of the node. In general
38 we provide and use accessor methods for a node's attributes, instead
39 of accessing the hash component directly.
41 Every PAST/POST node has C<name>, C<source>, and C<pos> attributes.
42 The C<name> attribute is the node's name, if any, while C<source>
43 and C<pos> are used to identify the location in the original source
44 code for the node. The C<source> and C<pos> values are generally
45 set by the C<node> method below.
47 Other node attributes are generally defined by subclasses of C<PCT::Node>.
51 =item init([child1, child2, ..., ] [attr1=>val1, attr2=>val2, ... ])
53 Initialize a node with the given children and attributes.
54 Adds each child to the node (using the C<push> method, below) and
55 calls the appropriate accessor method for each attribute.
61 .param pmc children :slurpy
62 .param pmc adverbs :slurpy :named
65 iter = new 'Iterator', children
67 unless iter goto children_end
73 iter = new 'Iterator', adverbs
75 unless iter goto adverbs_end
78 $P1 = find_method self, $S0
87 =item new([child1, child2, ..., ] [attr1=>val1, attr2=>val2, ...])
89 Create a new PAST node of initialized with the given
90 children and attributes. Returns the newly created node.
95 .param pmc children :slurpy
96 .param pmc adverbs :slurpy :named
99 $P0 = getattribute $P0, 'parrotclass'
101 $P1.'init'(children :flat, adverbs :flat :named)
108 Create and returns a clone of a PAST node.
112 .sub 'clone' :vtable :method
117 iter = self.'iterator'()
119 unless iter goto iter_child_end
125 iter = new 'Iterator', self
127 unless iter goto iter_attr_end
139 Add C<child> to the beginning of the invocant's list of children.
143 Remove the first child from the invocant's list of children.
148 Add C<child> to the end of the invocant's list of children.
152 Remove the last child from the invocant's list of children.
157 .sub 'unshift' :method
178 =item push_new(class, [child1, child2, ..., ] [attr1=>val1, attr2=>val2, ...])
180 (Deprecated.) Creates a new node of type C<class>, initializes it with the
181 given children and attributes, and adds it to the end of the invocant's
182 array of children. Returns the newly created node.
186 .sub 'push_new' :method
188 .param pmc children :slurpy
189 .param pmc adverbs :slurpy :named
191 $P0.'init'(children :flat, adverbs :flat :named)
199 Returns a newly initialized iterator for the invocant's list of
204 .sub 'iterator' :method
207 iter = new 'Iterator', $P0
215 Sets the invocant's C<source> and C<pos> attributes to those
216 of C<val>. If C<val> is another PAST node, then C<source> and C<pos>
217 are simply copied from that node, otherwise C<val> is assumed to be
218 a C<Match> object and obtains source/position information from that.
224 $I0 = isa node, 'PAST::Node'
225 if $I0 goto clone_past
243 Accessor method -- sets/returns the C<name> attribute of the invocant.
248 .param pmc value :optional
249 .param int has_value :opt_flag
250 .return self.'attr'('name', value, has_value)
254 =item attr(STR attrname, PMC value, INT has_value)
256 Helper method for accessors. If C<has_value> is true then set
257 the invocant's value of C<attrname> to C<value>. Returns the
258 (resulting) value of C<attrname> in the invocant.
263 .param string attrname
266 .param pmc default :optional
267 .param int has_default :opt_flag
268 if has_value goto setattr
269 value = self[attrname]
270 unless null value goto value_done
271 unless has_default goto value_undef
278 self[attrname] = value
283 =item unique([STR fmt])
285 Generate a unique number that can be used as an identifier.
286 If C<fmt> is provided, then it will be used as a prefix to the
291 .sub 'unique' :method
292 .param string fmt :optional
293 .param int has_fmt :opt_flag
295 if has_fmt goto unique_1
298 $P0 = get_global '$!serno'
300 $S0 = concat fmt, $S0
310 Patrick Michaud <pmichaud@pobox.com> is the author and maintainer.
311 Please send patches and suggestions to the Parrot porters or
312 Perl 6 compilers mailing lists.
316 2006-11-20 Patrick Michaud added first draft of POD documentation.
317 2007-11-21 Re-implementation with pdd26 compliance, compiler toolkit
318 2007-12-07 Refactor PAST::Node into separate PCT::Node component.
322 Copyright (C) 2006-2008, The Perl Foundation.
330 # vim: expandtab shiftwidth=4 ft=pir: