Author: allison
Date: Wed May 3 23:42:12 2006
New Revision: 12497
Modified:
trunk/ (props changed)
trunk/compilers/past/Node.pir
trunk/compilers/past/PAST/Op.pir
trunk/compilers/past/PAST/Val.pir
Log:
[EMAIL PROTECTED]: allison | 2006-05-03 22:50:21 -0700
PAST: Integrating some of the innovations of the Perl 6 compiler into
the base node set.
Modified: trunk/compilers/past/Node.pir
==============================================================================
--- trunk/compilers/past/Node.pir (original)
+++ trunk/compilers/past/Node.pir Wed May 3 23:42:12 2006
@@ -30,6 +30,101 @@
setattribute self, "children", $P3
.end
+=head1 Accessor Methods
+
+These methods define the external interface of node objects.
+
+=head2 source
+
+Sets or gets the source code string that a particular node was parsed
+from. (Useful for debugging output.)
+
+ node.'source'($S1)
+ $S2 = node.'source'()
+
+=cut
+
+.sub 'source' :method
+ .param string source :optional
+ .param int passed_source :opt_flag
+ .return self.'accessor'('source', source, passed_source)
+.end
+
+=head2 pos
+
+Sets or gets the position offset in the original source code file that a
+particular node was parsed from. (Useful for debugging output.)
+
+ node.'pos'($I1)
+ $S2 = node.'pos'()
+
+=cut
+
+.sub 'pos' :method
+ .param int pos :optional
+ .param int passed_pos :opt_flag
+ .return self.'accessor'('pos', pos, passed_pos)
+.end
+
+=head2 children
+
+Returns a simple array of the children of a particular node.
+
+ $P1 = node.'children'()
+
+=cut
+
+.sub 'children' :method
+ $P2 = getattribute self, 'children'
+ .return ($P2)
+.end
+
+=head2 child_iter
+
+Returns an iterator for the children of a node.
+
+ iter = node.'child_iter'()
+
+=cut
+
+.sub 'child_iter' :method
+ $P1 = getattribute self, "children"
+ $P2 = new Iterator, $P1 # setup iterator for node
+ $P2 = 0
+ .return($P2)
+.end
+
+=head2 add_child
+
+Adds one child to the node's list of children. If the list doesn't exist
+yet, it creates it.
+
+ node.'add_child'($P1)
+
+=cut
+
+.sub 'add_child' :method
+ .param pmc child
+ .local pmc children
+ children = getattribute self, 'children'
+ $I0 = defined children
+ if $I0 goto children_exist
+ children = new .ResizablePMCArray
+ setattribute self, "children", children
+ children_exist:
+ push children, child
+ .return()
+.end
+
+=head2 elements
+
+Check how many elements a node has by calling the C<elements> opcode on
+it:
+
+ $I1 = elements node
+
+=cut
+
.sub '__elements' :method
$P1 = getattribute self, 'children'
$I1 = elements $P1
@@ -40,62 +135,67 @@
.return( 1 )
.end
-.sub source :method
- .param string source :optional
- .param int got_source :opt_flag
- unless got_source goto get
- set:
- $P1 = new .String
- $P1 = source
- setattribute self, "source", $P1
- .return ($P1)
- get:
- $P2 = getattribute self, "source"
- .return ($P2)
-.end
+=head2 Array interface
-.sub pos :method
- .param int pos :optional
- .param int got_pos :opt_flag
- unless got_pos goto get
- set:
- $P1 = new .Integer
- $P1 = pos
- setattribute self, "pos", $P1
- .return ($P1)
- get:
- $P2 = getattribute self, "pos"
- .return ($P2)
-.end
+The children of a node can be directly accessed using array indexes on
+the node object.
-.sub children :method
- $P2 = getattribute self, "children"
- .return ($P2)
+ $P2 = node[5]
+ node[0] = $P1
+
+=cut
+
+.sub '__get_pmc_keyed_int' :method
+ .param int key
+ $P0 = getattribute self, 'children'
+ $P0 = $P0[key]
+ .return ($P0)
.end
-# Get the source string and position offset from start of source
-# code for a PGE match node.
-.sub clone_pge :method
- .param pmc node
- $S1 = node
- self."source"($S1)
- $I1 = node.from()
- self."pos"($I1)
+.sub '__set_pmc_keyed_int' :method
+ .param int key
+ .param pmc val
+ $P0 = getattribute self, 'children'
+ $P0[key] = val
.return ()
.end
-# Get the source string and position offset from start of source
-# code for a tree node.
-.sub clone_node :method
+=head2 clone
+
+This method captures a few key elements from an existing node to
+populate a new node.
+
+ newnode.'clone'(oldnode)
+
+=cut
+
+.sub 'clone' :method
.param pmc node
- $S1 = node.'source'()
- self.'source'($S1)
- $I1 = node.'pos'()
- self.'pos'($I1)
+ $I0 = isa node, 'Node'
+ if $I0 goto clone_past
+ clone_pge:
+ $S0 = node
+ self.'source'($S0)
+ $I0 = node.'from'()
+ self.'pos'($I0)
+ .return ()
+ clone_past:
+ $S0 = node.'source'()
+ self.'source'($S0)
+ $I0 = node.'pos'()
+ self.'pos'($I0)
.return ()
.end
-.sub "dump" :method
+=head2 dump
+
+Recursively prints a formatted display of the contents of a node.
+
+ node.'dump'()
+
+=cut
+
+.sub 'dump' :method
.param int level :optional
.local string indent
indent = repeat " ", level # tab is 4 spaces here
@@ -109,7 +209,7 @@
# print attributes for this node
.local pmc iter
$P1 = self.'DUMPABLE'()
- iter = new Iterator, $P1
+ iter = new Iterator, $P1
iter = 0
iter_loop:
unless iter, iter_end
@@ -128,7 +228,51 @@
.return ()
.end
-.sub "dump_attribute" :method
+=head1 Utility Methods
+
+These methods provide functionality for the Node class and its
+subclasses, but aren't part of the external interface for the class.
+
+=head2 DUMPABLE
+
+This method returns a list of attributes that should be displayed in a
+'dump' operation. Subclasses can override it to return a different list.
+
+=cut
+
+.sub 'DUMPABLE' :method
+ $P1 = new .ResizableStringArray
+ push $P1, 'source'
+ push $P1, 'pos'
+ push $P1, 'children'
+ .return($P1)
+.end
+
+=head2 accessor
+
+Provides a generic accessor method that can be used by any subclass. It
+takes three arguments: the name of the attribute to access, a value for
+the attribute, and a binary flag that says whether to set the attribute.
+
+ self.'accessor'('attrname', value, setflag)
+
+=cut
+
+.sub 'accessor' :method
+ .param string attrname
+ .param pmc value
+ .param int setvalue
+ if setvalue goto set
+ value = getattribute self, attrname
+ unless null value goto end
+ value = new .Undef
+ set:
+ setattribute self, attrname, value
+ end:
+ .return (value)
+.end
+
+.sub 'dump_attribute' :method
.param string name
.param int level :optional
.local string indent
@@ -152,7 +296,7 @@
.return ()
.end
-.sub "dump_children" :method
+.sub 'dump_children' :method
.param int level :optional
.local string indent
indent = repeat " ", level # tab is 4 spaces here
@@ -177,49 +321,3 @@
print "]\n"
.return ()
.end
-
-.sub 'add_child' :method
- .param pmc child
- .local pmc children
- children = getattribute self, 'children'
- $I0 = defined children
- if $I0 goto children_exist
- children = new .ResizablePMCArray
- setattribute self, "children", children
- children_exist:
- push children, child
- .return()
-.end
-
-=head2 child_iter
-
-Return an iterator for the children of a node.
-
-=cut
-
-.sub 'child_iter' :method
- $P1 = getattribute self, "children"
- $P2 = new Iterator, $P1 # setup iterator for node
- $P2 = 0
- .return($P2)
-.end
-
-=head1 Utility Methods
-
-These methods provide functionality for the Node class and its
-subclasses, but aren't part of the external interface for the class.
-
-=head2 DUMPABLE
-
-This method returns a list of attributes that should be displayed in a
-'dump' operation. Subclasses can override it to return a different list.
-
-=cut
-
-.sub 'DUMPABLE' :method
- $P1 = new .ResizableStringArray
- push $P1, 'source'
- push $P1, 'pos'
- push $P1, 'children'
- .return($P1)
-.end
Modified: trunk/compilers/past/PAST/Op.pir
==============================================================================
--- trunk/compilers/past/PAST/Op.pir (original)
+++ trunk/compilers/past/PAST/Op.pir Wed May 3 23:42:12 2006
@@ -29,15 +29,7 @@
.end
.sub 'op' :method
- .param string op :optional
- .param int got_op :opt_flag
- unless got_op goto get
- set:
- $P1 = new .String
- $P1 = op
- setattribute self, "op", $P1
- .return ($P1)
- get:
- $P2 = getattribute self, "op"
- .return ($P2)
+ .param string op :optional
+ .param int passed_op :opt_flag
+ .return self.'accessor'('op', op, passed_op)
.end
Modified: trunk/compilers/past/PAST/Val.pir
==============================================================================
--- trunk/compilers/past/PAST/Val.pir (original)
+++ trunk/compilers/past/PAST/Val.pir Wed May 3 23:42:12 2006
@@ -28,30 +28,14 @@
.return($P1)
.end
-.sub value :method
- .param string value :optional
- .param int got_value :opt_flag
- unless got_value goto get
- set:
- $P1 = new .String
- $P1 = value
- setattribute self, "value", $P1
- .return ($P1)
- get:
- $P2 = getattribute self, "value"
- .return ($P2)
+.sub 'value' :method
+ .param string value :optional
+ .param int passed_value :opt_flag
+ .return self.'accessor'('value', value, passed_value)
.end
-.sub valtype :method
- .param string valtype :optional
- .param int got_valtype :opt_flag
- unless got_valtype goto get
- set:
- $P1 = new .String
- $P1 = valtype
- setattribute self, "valtype", $P1
- .return ($P1)
- get:
- $P2 = getattribute self, "valtype"
- .return ($P2)
+.sub 'valtype' :method
+ .param string valtype :optional
+ .param int passed_valtype :opt_flag
+ .return self.'accessor'('valtype', valtype, passed_valtype)
.end