5 PAST - Parrot abstract syntax tree
9 This file implements the various abstract syntax tree nodes
10 for compiling programs in Parrot.
14 .namespace [ 'PAST';'Node' ]
16 .sub 'onload' :anon :load :init
17 ## create the PAST::Node base class
18 .local pmc p6meta, base
19 p6meta = new 'P6metaclass'
20 base = p6meta.'new_class'('PAST::Node', 'parent'=>'PCT::Node')
22 p6meta.'new_class'('PAST::Op', 'parent'=>base)
23 p6meta.'new_class'('PAST::Stmts', 'parent'=>base)
24 p6meta.'new_class'('PAST::Val', 'parent'=>base)
25 p6meta.'new_class'('PAST::Var', 'parent'=>base)
26 p6meta.'new_class'('PAST::Block', 'parent'=>base)
27 p6meta.'new_class'('PAST::Control', 'parent'=>base)
28 p6meta.'new_class'('PAST::VarList', 'parent'=>base)
33 =head1 PAST Node types
37 C<PAST::Node> is the base class for all PAST nodes, and is
38 derived from PCT::Node. A node has an array component to
39 hold its children, and a hash component for its attributes.
40 However, we tend to use accessor methods for accessing the node's
41 attributes instead of accessing the hash directly.
43 Every PAST node inherits C<name>, C<source>, and C<pos> attributes
44 from C<PCT::Node>. The C<name> attribute is the node's name, if
45 any, while C<source> and C<pos> are used to identify the location
46 in the original source code for the node. The C<source> and C<pos>
47 values are generally set by the C<node> method inherited from
50 Other node attributes are generally defined by subclasses of C<PAST::Node>.
54 =item returns([value])
56 Accessor method -- sets/returns the return type for the invocant.
60 .sub 'returns' :method
61 .param pmc value :optional
62 .param int has_value :opt_flag
63 .tailcall self.'attr'('returns', value, has_value)
69 Accessor method -- sets/returns the arity (number of expected arguments)
75 .param pmc value :optional
76 .param int has_value :opt_flag
77 .tailcall self.'attr'('arity', value, has_value)
83 Accessor method -- for named arguments, sets/returns the name to be
84 associated with the argument.
89 .param pmc value :optional
90 .param int has_value :opt_flag
91 .tailcall self.'attr'('named', value, has_value)
97 Accessor method -- sets/returns the "flatten" flag on arguments.
102 .param pmc value :optional
103 .param int has_value :opt_flag
104 .tailcall self.'attr'('flat', value, has_value)
107 .sub 'handlers' :method
108 .param pmc value :optional
109 .param int has_value :opt_flag
110 .tailcall self.'attr'('handlers', value, has_value)
116 Get/set the C<lvalue> attribute, which indicates whether this
117 variable is being used in an lvalue context.
121 .sub 'lvalue' :method
122 .param pmc value :optional
123 .param int has_value :opt_flag
124 .tailcall self.'attr'('lvalue', value, has_value)
132 C<PAST::Val> nodes represent constant values in the abstract
133 syntax tree. The C<name> attribute represents the value of the
140 Get/set the constant value for this node.
144 .namespace [ 'PAST';'Val' ]
147 .param pmc value :optional
148 .param int has_value :opt_flag
149 .tailcall self.'attr'('value', value, has_value)
152 =item lvalue([value])
154 Throw an exception if we try to make a PAST::Val into an lvalue.
158 .sub 'lvalue' :method
159 .param pmc value :optional
160 .param int has_value :opt_flag
161 unless has_value goto normal
162 unless value goto normal
163 die "Unable to set lvalue on PAST::Val node"
165 .tailcall self.'attr'('lvalue', value, has_value)
172 C<PAST::Var> nodes represent variables within the abstract
173 syntax tree. The variable name (if any) is given as the node's
180 Get/set the PAST::Var node's "scope" (i.e., how the variable
181 is accessed or set). Allowable values include "package", "lexical",
182 "parameter", "keyed", "attribute" and "register", representing
183 HLL global, lexical, block parameter, array/hash variables, object
184 members and (optionally named) Parrot registers respectively.
188 .namespace [ 'PAST';'Var' ]
191 .param pmc value :optional
192 .param int has_value :opt_flag
193 .tailcall self.'attr'('scope', value, has_value)
199 Get/set the node's C<isdecl> attribute (for lexical variables) to C<flag>.
200 A true value of C<isdecl> indicates that the variable given by
201 this node is to be created within the current lexical scope.
202 Otherwise, the node refers to a lexical variable from an outer scope.
206 .sub 'isdecl' :method
207 .param pmc value :optional
208 .param int has_value :opt_flag
209 .tailcall self.'attr'('isdecl', value, has_value)
213 =item namespace([namespace])
215 Get/set the variable's namespace attribute to the array of strings
216 given by C<namespace>. Useful only for variables with a C<scope>
221 .sub 'namespace' :method
222 .param pmc value :optional
223 .param int has_value :opt_flag
224 .tailcall self.'attr'('namespace', value, has_value)
230 Get/set the node's C<slurpy> attribute (for parameter variables) to C<flag>.
231 A true value of C<slurpy> indicates that the parameter variable given by this
232 node is to be created as a slurpy parameter (consuming all remaining arguments
237 .sub 'slurpy' :method
238 .param pmc value :optional
239 .param int has_value :opt_flag
240 .tailcall self.'attr'('slurpy', value, has_value)
244 =item call_sig([flag])
246 Get/set the node's C<call_sig> attribute (for parameter variables) to C<flag>.
247 A true value of C<call_sig> indicates that the parameter variable given by this
248 node is to be created as a C<:call_sig> parameter. If you use this, it should be
253 .sub 'call_sig' :method
254 .param pmc value :optional
255 .param int has_value :opt_flag
256 .tailcall self.'attr'('call_sig', value, has_value)
260 =item viviself([type])
262 If the variable needs to be instantiated, then C<type> indicates
263 either the type of the value to create for the node or (future
264 implementation) a PAST tree to create the value.
268 .sub 'viviself' :method
269 .param pmc value :optional
270 .param int has_value :opt_flag
271 .tailcall self.'attr'('viviself', value, has_value)
275 =item vivibase([type])
277 For keyed nodes, C<type> indicates the type of aggregate to
278 create for the base if the base doesn't specify its own 'viviself'
283 .sub 'vivibase' :method
284 .param pmc value :optional
285 .param int has_value :opt_flag
286 .tailcall self.'attr'('vivibase', value, has_value)
289 =item multitype([type])
291 Get/set MMD type of Var when used as parameter of Block.
295 .sub 'multitype' :method
296 .param pmc value :optional
297 .param int has_value :opt_flag
298 .tailcall self.'attr'('multitype', value, has_value)
306 C<PAST::Op> nodes represent the operations in an abstract syntax
307 tree. The primary function of the node is given by its C<pasttype>,
308 secondary functions may be given by the node's C<name>, C<pirop>,
313 =item pasttype([type])
315 A C<PAST::Op> node's C<pasttype> determines the type of operation to
316 be performed. Predefined values of C<pasttype> are:
318 assign - Copy the value of the node's second child into
319 the variable expression given by its first child.
321 bind - Bind the variable given by the node's first child
322 to the value given by its second child.
324 if - Evaluate the first child; if the first child is
325 true then evaluate the second child (if any)
326 otherwise evaluate the third child (if any).
327 If either the second or third child are missing,
328 then they evaluate as the result of the first child.
330 unless - Same as 'if' above, but reverse the evaluation
331 of the second and third children nodes.
333 while - Evaluate the first child, if the result is
334 true then evaluate the second child and repeat.
336 until - Evaluate the first child, if the result is
337 false then evaluate the second child and repeat.
339 for - Iterate over the first child. For each element,
340 invoke the sub in the second child, passing the
341 element as the only parameter.
343 call - Call a subroutine, passing the results of any
344 child nodes as arguments. The subroutine to be
345 called is given by the node's C<name> attribute,
346 if the node has no C<name> attribute then the
347 first child is assumed to evaluate to a callable
350 pirop - Execute the named PIR opcode, passing the results
351 of any children nodes as arguments.
353 inline - Execute the sequence of PIR statements given
354 by the node's C<inline> attribute (a string).
355 See the C<inline> method below for details.
357 callmethod - Invokes a method on an object, using children
358 nodes as arguments. If the node has a C<name>
359 attribute, then the first child is the invocant
360 and any remaining children are arguments. If
361 the node doesn't have a C<name> attribute, then
362 the first child evaluates to the method to be
363 called, the second child is the invocant, and
364 the remaining children are arguments to the method call.
366 try - (preliminary) Execute the code given by the first
367 child, and if any exceptions occur then handle them
368 using the code given by the second child.
370 If a node doesn't have a value set for C<pasttype>, then it
371 assumes "pirop" if its C<pirop> attribute is set, otherwise it
376 .namespace [ 'PAST';'Op' ]
378 .sub 'pasttype' :method
379 .param pmc value :optional
380 .param int has_value :opt_flag
381 .tailcall self.'attr'('pasttype', value, has_value)
385 =item pirop([opcode])
387 Get/set the PIR opcode to be executed for this node. The PAST
388 implementation knows about the argument types for many of the
389 PIR opcodes, and will try to automatically convert the results
390 of any children nodes into the correct types if needed. (In
391 general, the implementation tries to convert things to PMCs
392 whenever it isn't certain what else to do.) The table of
393 PIR opcodes that PAST "knows" about is in F<POST.pir>.
398 .param pmc value :optional
399 .param int has_value :opt_flag
400 .tailcall self.'attr'('pirop', value, has_value)
406 Get/set whether this node is an lvalue, or treats its first
407 child as an lvalue (e.g., for assignment).
409 =item inline([STRING code])
411 Get/set the code to be used for inline PIR when C<pasttype> is
412 "inline". The C<code> argument is PIR text to be inserted in
413 the final generated code sequence. Sequences of "%0", "%1",
414 "%2", ... "%9" in C<code> are replaced with the evaluated
415 results of the first, second, third, ..., tenth children nodes.
416 (If you need more than ten arguments to your inline PIR, consider
417 making it a subroutine call instead.)
419 The register to hold the result of the inline PIR operation is
420 given by "%r", "%t", or "%u" in the C<code> string:
422 %r - Generate a unique PMC register for the result.
423 %t - Generate a unique PMC register for the result,
424 and initialize it with an object of type C<returns>
425 before the execution of the inline PIR.
426 %u - Re-use the first child's PMC (%0) if it's a temporary
427 result, otherwise same as %t above.
428 %v - (void) Re-use the first child's PMC (%0) as the result
433 .sub 'inline' :method
434 .param pmc value :optional
435 .param int has_value :opt_flag
436 .tailcall self.'attr'('inline', value, has_value)
442 Set a variety of C<PAST::Op> attributes based on entries
443 in C<hash>. Typically C<hash> is an entry in the operator
444 precedence table, and the attributes being set correspond
445 to traits in the grammar.
449 .sub 'opattr' :method
452 $P0 = split ' ', "pasttype pirop inline lvalue"
455 unless $P1 goto iter_end
458 if null $P2 goto iter_loop
459 $P3 = find_method self, $S0
470 C<PAST::Stmts> is a container of C<PAST::Node> without any specific methods.
474 C<PAST::Block> nodes represent lexical scopes within an abstract
475 syntax tree, and roughly translate to individual Parrot subroutines.
476 A C<PAST::Block> node nested within another C<PAST::Block> node
477 acts like a nested lexical scope.
479 If the block has a C<name> attribute, that becomes the name of
480 the resulting Parrot sub, otherwise a unique name is automatically
481 generated for the block.
485 =item blocktype([STRING type])
487 Get/set the type of the block. The currently understood values
488 are 'declaration', 'immediate', and 'method'. 'Declaration' indicates
489 that a block is simply being defined at this point, while
490 'immediate' indicates a block that is to be immediately
491 executed when it is evaluated in the AST (e.g., the immediate
492 blocks in Perl6 C<if>, C<while>, and other similar statements).
496 .namespace [ 'PAST';'Block' ]
498 .sub 'blocktype' :method
499 .param pmc value :optional
500 .param int has_value :opt_flag
501 .tailcall self.'attr'('blocktype', value, has_value)
505 =item closure([value])
507 Get/set the closure flag for the block to C<value>. If set,
508 any block reference returned by the node is to a clone of the
509 block that has captured the current lexical context.
513 .sub 'closure' :method
514 .param pmc value :optional
515 .param int has_value :opt_flag
516 .tailcall self.'attr'('closure', value, has_value)
520 =item control([value])
522 Get/set the control exception handler for this block to C<value>.
523 The exception handler can be any PAST tree. The special (string)
524 value "return_pir" generates code to handle C<CONTROL_RETURN> exceptions.
528 .sub 'control' :method
529 .param pmc value :optional
530 .param int has_value :opt_flag
531 .tailcall self.'attr'('control', value, has_value)
535 =item loadinit([past])
537 Get/set the "load initializer" for this block to C<past>.
538 The load initializer is a set of operations to be performed
539 as soon as the block is compiled/loaded. For convenience,
540 requests to C<loadinit> autovivify an empty C<PAST::Stmts>
541 node if one does not already exist.
543 Within the load initializer, the C<block> PMC register is
544 automatically initialized to refer to the block itself
545 (to enable attaching properties, adding the block as a method,
546 storing in a symbol table, etc.).
550 .sub 'loadinit' :method
551 .param pmc value :optional
552 .param int has_value :opt_flag
553 if has_value goto getset_value
554 $I0 = exists self['loadinit']
555 if $I0 goto getset_value
556 $P0 = get_hll_global ['PAST'], 'Stmts'
560 .tailcall self.'attr'('loadinit', value, has_value)
564 =item namespace([namespace])
566 Get/set the namespace for this block. The C<namespace> argument
567 can be either a string or an array of strings.
571 .sub 'namespace' :method
572 .param pmc value :optional
573 .param int has_value :opt_flag
574 .tailcall self.'attr'('namespace', value, has_value)
579 Get/set the multi signature for this block. The C<multi> argument
580 can be either a string or an array of strings.
585 .param pmc value :optional
586 .param int has_value :opt_flag
587 .tailcall self.'attr'('multi', value, has_value)
593 Get/set the C<hll> for this block.
598 .param pmc value :optional
599 .param int has_value :opt_flag
600 .tailcall self.'attr'('hll', value, has_value)
604 =item nsentry([nsentry])
606 Get/set the C<nsentry> for this block.
610 .sub 'nsentry' :method
611 .param pmc value :optional
612 .param int has_value :opt_flag
613 .tailcall self.'attr'('nsentry', value, has_value)
617 =item symbol(name [, attr1 => val1, attr2 => val2, ...])
619 If called with named arguments, sets the symbol hash corresponding
620 to C<name> in the current block. The HLL is free to select
621 any symbol attributes desired, although the 'scope' attribute
622 is typically used to assist with lexical scoping of PAST::Var
625 If no named arguments are given, returns the current
626 attribute hash for symbol C<name>.
630 .sub 'symbol' :method
632 .param pmc attr :slurpy :named
634 symtable = self['symtable']
635 unless null symtable goto have_symtable
636 symtable = new 'Hash'
637 self['symtable'] = symtable
640 symbol = symtable[name]
641 if null symbol goto symbol_empty
642 unless attr goto attr_done
646 unless it goto attr_done
654 unless attr goto symbol_done
655 symtable[name] = attr
661 =item symbol_defaults([attr1 => val1, attr2 => val2, ... ])
663 Set default attributes for non-existent symbols in the
664 symbol hash (see C<symbol> above). If no named arguments
665 are given, returns the default attribute hash itself.
667 Currently we just use the '' entry of the symbol hash to
668 store the default attributes, but it's probably not safe
669 to rely on this behavior in the future.
673 .sub 'symbol_defaults' :method
674 .param pmc attr :slurpy :named
675 .tailcall self.'symbol'('', attr :flat :named)
679 =item symtable([value])
681 Get/set the symbol table for the block. May be deprecated in
682 favor of the C<symbol> method above.
686 .sub 'symtable' :method
687 .param pmc value :optional
688 .param int has_value :opt_flag
689 .tailcall self.'attr'('symtable', value, has_value)
693 =item lexical([flag])
695 Get/set whether the block is lexically nested within
696 the block that contains it.
700 .sub 'lexical' :method
701 .param pmc value :optional
702 .param int has_value :opt_flag
703 .tailcall self.'attr'('lexical', value, has_value, 1)
707 =item compiler([name])
709 Indicate that the children nodes of this block are to be
710 compiled using compiler C<name> instead of the standard
715 .sub 'compiler' :method
716 .param pmc value :optional
717 .param int has_value :opt_flag
718 .tailcall self.'attr'('compiler', value, has_value)
721 =item compiler_args()
723 Specify named arguments to be passed to the compiler set
724 through the compiler attribute. Not used if compiler is
729 .sub 'compiler_args' :method
730 .param pmc value :named :slurpy
731 .local int have_value
732 have_value = elements value
733 .tailcall self.'attr'('compiler_args', value, have_value)
738 If C<subid> is provided, then sets the subid for this block.
739 Returns the current subid for the block, generating a unique
740 subid for the block if one does not already exist.
745 .param pmc value :optional
746 .param int has_value :opt_flag
747 if has_value goto getset_value
748 $I0 = exists self['subid']
749 if $I0 goto getset_value
750 value = self.'unique'()
752 suffix = get_global '$!subid_suffix'
753 unless null suffix goto have_suffix
756 $S0 = concat '_', $S0
758 set_global '$!subid_suffix', suffix
763 .tailcall self.'attr'('subid', value, has_value)
767 =item pirflags([pirflags])
769 Get/set any pirflags for this block.
773 .sub 'pirflags' :method
774 .param pmc value :optional
775 .param int has_value :opt_flag
776 .tailcall self.'attr'('pirflags', value, has_value)
780 .namespace [ 'PAST';'Control' ]
782 .sub 'handle_types' :method
783 .param pmc value :optional
784 .param int has_value :opt_flag
785 .tailcall self.'attr'('handle_types', value, has_value)
788 .sub 'handle_types_except' :method
789 .param pmc value :optional
790 .param int has_value :opt_flag
791 .tailcall self.'attr'('handle_types_except', value, has_value)
795 .namespace [ 'PAST';'VarList' ]
797 .sub 'bindvalue' :method
798 .param pmc value :optional
799 .param int has_value :opt_flag
800 .tailcall self.'attr'('bindvalue', value, has_value)
808 Patrick Michaud <pmichaud@pobox.com> is the author and maintainer.
809 Please send patches and suggestions to the Parrot porters or
810 Perl 6 compilers mailing lists.
814 2006-11-20 Patrick Michaud added first draft of POD documentation.
815 2007-11-21 Re-implementation with pdd26 compliance, compiler toolkit
819 Copyright (C) 2006-2008, Parrot Foundation.
827 # vim: expandtab shiftwidth=4 ft=pir: