Author: allison
Date: Tue May  2 12:15:33 2006
New Revision: 12482

Modified:
   trunk/   (props changed)
   trunk/compilers/past/Node.pir
   trunk/compilers/past/PAST/Op.pir
   trunk/compilers/past/PAST/Val.pir
   trunk/t/compilers/past/past.t

Log:
 [EMAIL PROTECTED]:  allison | 2006-05-02 12:14:19 -0700
 PAST: refactoring nodes so subclasses don't need to override 'dump',
 they only set an array of attributes to dump.


Modified: trunk/compilers/past/Node.pir
==============================================================================
--- trunk/compilers/past/Node.pir       (original)
+++ trunk/compilers/past/Node.pir       Tue May  2 12:15:33 2006
@@ -106,10 +106,21 @@
     print $S0
     print "> => { \n"
 
-    # print source for this node
-    self.dump_attribute("source", level)
-    self.dump_attribute("pos", level)
+    # print attributes for this node
+    .local pmc iter
+    $P1 = self.'DUMPABLE'()
+    iter = new Iterator, $P1
+    iter = 0
+  iter_loop:
+    unless iter, iter_end
+    $S1 = shift iter
+    if $S1 == 'children' goto children_dump
+    self.dump_attribute($S1, level)
+    goto iter_loop
+  children_dump:
     self.dump_children(level)
+    goto iter_loop
+  iter_end:
 
     # close off current node display
     print indent
@@ -192,3 +203,23 @@
     $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    Tue May  2 12:15:33 2006
@@ -19,27 +19,13 @@
     .return ()
 .end
 
-.sub "dump" :method
-    .param int level :optional
-    .local string indent
-    indent = repeat "    ", level # tab is 4 spaces here
-    level += 1 # set level for attributes
-    $S0 = typeof self
-    print indent
-    print "<"
-    print $S0
-    print "> => { \n"
-
-    # print source for this node
-    self.dump_attribute("source", level)
-    self.dump_attribute("pos", level)
-    self.dump_attribute("op", level)
-    self.dump_children(level)
-
-    # close off current node display
-    print indent
-    print "}\n"
-    .return ()
+.sub 'DUMPABLE' :method
+    $P1 = new .ResizableStringArray
+    push $P1, 'source'
+    push $P1, 'pos'
+    push $P1, 'op'
+    push $P1, 'children'
+    .return($P1)
 .end
 
 .sub 'op' :method

Modified: trunk/compilers/past/PAST/Val.pir
==============================================================================
--- trunk/compilers/past/PAST/Val.pir   (original)
+++ trunk/compilers/past/PAST/Val.pir   Tue May  2 12:15:33 2006
@@ -19,27 +19,13 @@
     .return ()
 .end
 
-.sub "dump" :method
-    .param int level :optional
-    .local string indent
-    indent = repeat "    ", level # tab is 4 spaces here
-    level += 1 # set level for attributes
-    $S0 = typeof self
-    print indent
-    print "<"
-    print $S0
-    print "> => { \n"
-
-    # print source for this node
-    self.dump_attribute("source", level)
-    self.dump_attribute("pos", level)
-    self.dump_attribute("value", level)
-    self.dump_attribute("valtype", level)
-
-    # close off current node display
-    print indent
-    print "}\n"
-    .return ()
+.sub 'DUMPABLE' :method
+    $P1 = new .ResizableStringArray
+    push $P1, 'source'
+    push $P1, 'pos'
+    push $P1, 'value'
+    push $P1, 'valtype'
+    .return($P1)
 .end
 
 .sub value :method

Modified: trunk/t/compilers/past/past.t
==============================================================================
--- trunk/t/compilers/past/past.t       (original)
+++ trunk/t/compilers/past/past.t       Tue May  2 12:15:33 2006
@@ -11,21 +11,25 @@
 CODE
 OUT
 
-foreach my $module (qw(Code Exp Stmts Stmt Sub Var)) {
+foreach my $name (qw(Code Exp Stmts Stmt Sub Var)) {
+my $module = "PAST::$name";
 my $code = <<'CODE'
 .sub _main
     load_bytecode 'PAST.pbc'
     .local pmc node
+    .local pmc node2
 CODE
 ;
 
-$code .= "    node = new 'PAST::$module'\n";
+$code .= "    node = new '$module'\n";
+$code .= "    node2 = new '$module'\n";
 $code .= <<'CODE'
-    $P0 = new .String
-    $P0 = 'bar'
     node.'source'('foo')
     node.'pos'(42)
-    node.'add_child'($P0)
+
+    node2.'source'('b')
+    node2.'pos'(9)
+    node.'add_child'(node2)
 
     $P1 = getattribute node, 'source'
     print $P1
@@ -33,19 +37,26 @@
     $P1 = getattribute node, 'pos'
     print $P1
     print "\n"
-    $P2 = getattribute node, 'children'
-    $P3 = $P2[0]
-    print $P3
-    print "\n"
+    node.dump()
     .return ()
 .end
 CODE
 ;
 
-pir_output_is($code, <<'OUT', "set attributes for PAST::$module via method");
+pir_output_is($code, <<"OUT", "set attributes for $module via method");
 foo
 42
-bar
+<$module> => { 
+    'source' => 'foo',
+    'pos' => '42',
+    'children' => [
+        <$module> => { 
+            'source' => 'b',
+            'pos' => '9',
+            'children' => []
+        }
+    ]
+}
 OUT
 
 }

Reply via email to