[svn:parrot] r47514 - in branches/gsoc_past_optimization: . config/gen/makefiles runtime/parrot/library/PAST runtime/parrot/library/PAST/Pattern t/library

tcurtis at svn.parrot.org tcurtis at svn.parrot.org
Wed Jun 9 19:57:52 UTC 2010


Author: tcurtis
Date: Wed Jun  9 19:57:52 2010
New Revision: 47514
URL: https://trac.parrot.org/parrot/changeset/47514

Log:
PAST::Pattern.transform(PAST::Node, invokable) implemented and partially tested!

Added:
   branches/gsoc_past_optimization/runtime/parrot/library/PAST/Pattern/Transformer.nqp   (contents, props changed)
Modified:
   branches/gsoc_past_optimization/MANIFEST
   branches/gsoc_past_optimization/MANIFEST.SKIP
   branches/gsoc_past_optimization/config/gen/makefiles/root.in
   branches/gsoc_past_optimization/runtime/parrot/library/PAST/Pattern/   (props changed)
   branches/gsoc_past_optimization/runtime/parrot/library/PAST/Pattern.nqp
   branches/gsoc_past_optimization/t/library/pastpattern.t

Modified: branches/gsoc_past_optimization/MANIFEST
==============================================================================
--- branches/gsoc_past_optimization/MANIFEST	Wed Jun  9 18:31:20 2010	(r47513)
+++ branches/gsoc_past_optimization/MANIFEST	Wed Jun  9 19:57:52 2010	(r47514)
@@ -1,7 +1,7 @@
 # ex: set ro:
 # $Id$
 #
-# generated by tools/dev/mk_manifest_and_skip.pl Wed Jun  9 04:27:12 2010 UT
+# generated by tools/dev/mk_manifest_and_skip.pl Wed Jun  9 18:58:33 2010 UT
 #
 # See below for documentation on the format of this file.
 #
@@ -1173,6 +1173,7 @@
 runtime/parrot/library/PAST/Pattern/Constant.nqp            [library]
 runtime/parrot/library/PAST/Pattern/Match.nqp               [library]
 runtime/parrot/library/PAST/Pattern/Node.nqp                [library]
+runtime/parrot/library/PAST/Pattern/Transformer.nqp         [library]
 runtime/parrot/library/PAST/Pattern/augment-past.pir        [library]
 runtime/parrot/library/PAST/Transformer.pir                 [library]
 runtime/parrot/library/PAST/Transformer/Dynamic.pir         [library]

Modified: branches/gsoc_past_optimization/MANIFEST.SKIP
==============================================================================
--- branches/gsoc_past_optimization/MANIFEST.SKIP	Wed Jun  9 18:31:20 2010	(r47513)
+++ branches/gsoc_past_optimization/MANIFEST.SKIP	Wed Jun  9 19:57:52 2010	(r47514)
@@ -1,6 +1,6 @@
 # ex: set ro:
 # $Id$
-# generated by tools/dev/mk_manifest_and_skip.pl Mon Jun  7 19:19:25 2010 UT
+# generated by tools/dev/mk_manifest_and_skip.pl Wed Jun  9 18:58:33 2010 UT
 #
 # This file should contain a transcript of the svn:ignore properties
 # of the directories in the Parrot subversion repository. (Needed for
@@ -606,6 +606,8 @@
 ^runtime/parrot/library/PAST/Pattern/Match\.pir/
 ^runtime/parrot/library/PAST/Pattern/Node\.pir$
 ^runtime/parrot/library/PAST/Pattern/Node\.pir/
+^runtime/parrot/library/PAST/Pattern/Transformer\.pir$
+^runtime/parrot/library/PAST/Pattern/Transformer\.pir/
 # generated from svn:ignore of 'runtime/parrot/library/PAST/Transformer/'
 ^runtime/parrot/library/PAST/Transformer/.*\.pbc$
 ^runtime/parrot/library/PAST/Transformer/.*\.pbc/

Modified: branches/gsoc_past_optimization/config/gen/makefiles/root.in
==============================================================================
--- branches/gsoc_past_optimization/config/gen/makefiles/root.in	Wed Jun  9 18:31:20 2010	(r47513)
+++ branches/gsoc_past_optimization/config/gen/makefiles/root.in	Wed Jun  9 19:57:52 2010	(r47514)
@@ -285,6 +285,7 @@
     $(LIBRARY_DIR)/PAST/Pattern/Constant.pbc \
     $(LIBRARY_DIR)/PAST/Pattern/Match.pbc \
     $(LIBRARY_DIR)/PAST/Pattern/Node.pbc \
+    $(LIBRARY_DIR)/PAST/Pattern/Transformer.pbc \
     $(LIBRARY_DIR)/PAST/Pattern/augment-node.pbc \
     $(LIBRARY_DIR)/PAST/Transformer.pbc \
     $(LIBRARY_DIR)/PAST/Transformer/Dynamic.pbc \
@@ -1127,6 +1128,15 @@
 	$(NQP_RX) --target=pir $(LIBRARY_DIR)/PAST/Pattern/Node.nqp \
 	> $@
 
+$(LIBRARY_DIR)/PAST/Pattern/Transformer.pbc: \
+	$(LIBRARY_DIR)/PAST/Pattern/Transformer.pir
+	$(PARROT) -o $@ $(LIBRARY_DIR)/PAST/Pattern/Transformer.pir
+
+$(LIBRARY_DIR)/PAST/Pattern/Transformer.pir: \
+	$(LIBRARY_DIR)/PAST/Pattern/Transformer.nqp $(NQP_RX)
+	$(NQP_RX) --target=pir $(LIBRARY_DIR)/PAST/Pattern/Transformer.nqp \
+	> $@
+
 #
 # Profiling runcore test supporting code
 #

Modified: branches/gsoc_past_optimization/runtime/parrot/library/PAST/Pattern.nqp
==============================================================================
--- branches/gsoc_past_optimization/runtime/parrot/library/PAST/Pattern.nqp	Wed Jun  9 18:31:20 2010	(r47513)
+++ branches/gsoc_past_optimization/runtime/parrot/library/PAST/Pattern.nqp	Wed Jun  9 19:57:52 2010	(r47514)
@@ -23,6 +23,20 @@
         }
         $result;
     }
+
+    method transform ($past, $transform) {
+        my &transSub;
+        if ($transform ~~ PAST::Transformer) {
+            &transSub := sub ($node) { $transformer.walk($node); };
+        } elsif (pir::does__iPS($transform, 'invokable')) {
+            &transSub := $transform;
+        } else {
+            pir::die('$transform must be invokable or a PAST::Transformer.');
+        }
+        my $transformer :=
+          PAST::Pattern::Transformer.new(self, &transSub);
+        $transformer.walk($past);
+    }
 }
 
 
@@ -35,6 +49,7 @@
     pir::load_bytecode('PAST/Pattern/Node.pbc');
 
     pir::load_bytecode('PAST/Pattern/augment-past.pbc');
+    pir::load_bytecode('PAST/Pattern/Transformer.pbc');
 }
 
 # Local Variables:

Added: branches/gsoc_past_optimization/runtime/parrot/library/PAST/Pattern/Transformer.nqp
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ branches/gsoc_past_optimization/runtime/parrot/library/PAST/Pattern/Transformer.nqp	Wed Jun  9 19:57:52 2010	(r47514)
@@ -0,0 +1,76 @@
+#!./parrot-nqp
+# Copyright (C) 2010, Parrot Foundation.
+# $Id$
+
+INIT {
+    pir::load_bytecode('PAST/Transformer.pbc');
+}
+
+class PAST::Pattern::Transformer is PAST::Transformer {
+    has $pattern;
+    has $transform;
+
+    method new ($pattern?, $transform?) {
+        my $class := pir::getattribute__PPS(self.HOW(),
+                                           'parrotclass');
+        my $self := pir::new__PP($class);
+        $self.pattern($pattern);
+        $self.transform($transform);
+        $self;
+    }
+
+    method pattern ($pattern?) {
+        my $result;
+        if pir::defined__iP($pattern) {
+            pir::setattribute(self, '$pattern', $pattern);
+        }
+        else {
+            $result := pir::getattribute__PPs(self, '$pattern');
+        }
+        $result;
+    }
+
+    method transform ($transform?) {
+        my $result;
+        if pir::defined__iP($transform) {
+            pir::setattribute(self, '$transform', $transform);
+        }
+        else {
+            $result := pir::getattribute__PPs(self, '$transform');
+        }
+        $result;
+    }
+
+}
+
+module PAST::Walker {
+    our multi sub walk (PAST::Pattern::Transformer $walker,
+                    PAST::Node $node) {
+        my $pattern := $walker.pattern();
+        my $shouldTransform;
+        if ($pattern ~~ PAST::Pattern::Node) {
+            $shouldTransform := $pattern.ACCEPTSEXACTLY($node);
+        }
+        else {
+            $shouldTransform := $pattern.ACCEPTS($node);
+        }
+        my $result;
+        if ($shouldTransform) {
+            $result := $walker.transform()($node);
+        }
+        else {
+            $result := $node;
+        }
+        my $newChildren := walkChildren($walker, $result);
+        replaceChildren($result, $newChildren);
+        $result;
+    }
+
+}
+
+# Local Variables:
+#   mode: cperl
+#   cperl-indent-level: 4
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4:

Modified: branches/gsoc_past_optimization/t/library/pastpattern.t
==============================================================================
--- branches/gsoc_past_optimization/t/library/pastpattern.t	Wed Jun  9 18:31:20 2010	(r47513)
+++ branches/gsoc_past_optimization/t/library/pastpattern.t	Wed Jun  9 19:57:52 2010	(r47514)
@@ -5,7 +5,7 @@
 pir::load_bytecode('PCT.pbc');
 pir::load_bytecode('PAST/Pattern.pbc');
 
-plan(2075);
+plan(2080);
 
 test_type_matching();
 test_attribute_exact_matching();
@@ -18,6 +18,8 @@
 
 test_match_result();
 
+test_transform();
+
 test_match_method();
 
 sub node_with_attr_set ($class, $attr, $val) {
@@ -714,6 +716,46 @@
        '$/[0] is correct for PAST::Pattern::VarList.');
 }
 
+sub test_transform () {
+    test_transform_sub();
+}
+
+sub test_transform_sub () {
+    sub isComparison ($opName) {
+        ($opName eq 'isgt' ||
+         $opName eq 'islt');
+    }
+    sub flipComparison ($op) {
+        my $temp := $op[0];
+        $op[0] := $op[1];
+        $op[1] := $temp;
+        $op;
+    }
+
+    my $pattern := PAST::Pattern::Op.new(:pirop(isComparison));
+    my $past := 
+      PAST::Stmts.new(PAST::Op.new(:pirop<isgt>,
+                                   PAST::Val.new(:value(2)),
+                                   PAST::Val.new(:value(1))),
+                      PAST::Op.new(:pirop<islt>,
+                                   PAST::Op.new(:pirop<islt>,
+                                                PAST::Val.new(:value(1)),
+                                                PAST::Val.new(:value(2))),
+                                   PAST::Val.new(:value(1))));
+    my $result := $pattern.transform($past, flipComparison);
+
+    ok($result ~~ PAST::Stmts,
+       'Non-matched nodes left unchanged.');
+    ok($result[0][0].value() == 1,
+       'Matched node 1 is changed.');
+    ok($result[1][0] ~~ PAST::Val,
+       'Matched node 2 is changed. 1');
+    ok($result[1][0].value() == 1,
+       'Matched node 2 is changed. 2');
+    ok($result[1][1][0].value() == 2,
+       'Matched nodes within other matched nodes are changed.');
+}
+
 sub test_match_method () {
     my $pattern := PAST::Pattern::Block.new();
     my $past := PAST::Block.new();


More information about the parrot-commits mailing list