[svn:parrot] r39216 - in trunk/compilers/nqp: bootstrap src src/Grammar t

bacek at svn.parrot.org bacek at svn.parrot.org
Thu May 28 12:11:43 UTC 2009


Author: bacek
Date: Thu May 28 12:11:42 2009
New Revision: 39216
URL: https://trac.parrot.org/parrot/changeset/39216

Log:
[nqp] Cherry-pick handling of "self" from pmc_pct branch.

Added:
   trunk/compilers/nqp/t/29-self.t
Modified:
   trunk/compilers/nqp/bootstrap/actions.pm
   trunk/compilers/nqp/src/Grammar.pg
   trunk/compilers/nqp/src/Grammar/Actions.pir

Modified: trunk/compilers/nqp/bootstrap/actions.pm
==============================================================================
--- trunk/compilers/nqp/bootstrap/actions.pm	Thu May 28 08:50:24 2009	(r39215)
+++ trunk/compilers/nqp/bootstrap/actions.pm	Thu May 28 12:11:42 2009	(r39216)
@@ -339,6 +339,8 @@
     my $params := $past[0];
     if $<declarator> eq 'method' {
         $past.blocktype('method');
+        unshift $past, PAST::Op.new('inline'=>'.lex "self", self',
+            'pasttype'=>'inline');
     }
     for $<signature>[0] {
         my $parameter := $($_<parameter>);
@@ -703,7 +705,12 @@
 
 
 method noun($/, $key) {
-    make $($/{$key});
+    if $key eq 'self' {
+        make PAST::Var.new('name'=>'self', 'node'=>$/);
+    }
+    else {
+        make $($/{$key});
+    }
 }
 ##.sub 'noun' :method
 ##    .param pmc match

Modified: trunk/compilers/nqp/src/Grammar.pg
==============================================================================
--- trunk/compilers/nqp/src/Grammar.pg	Thu May 28 08:50:24 2009	(r39215)
+++ trunk/compilers/nqp/src/Grammar.pg	Thu May 28 12:11:42 2009	(r39216)
@@ -287,6 +287,7 @@
     | <circumfix> {*}                                      #= circumfix
     | <variable> {*}                                       #= variable
     | <subcall> {*}                                        #= subcall
+    | 'self' >> {*}                                        #= self
     | <value> {*}                                          #= value
     | <name> {*}                                           #= name
 }

Modified: trunk/compilers/nqp/src/Grammar/Actions.pir
==============================================================================
--- trunk/compilers/nqp/src/Grammar/Actions.pir	Thu May 28 08:50:24 2009	(r39215)
+++ trunk/compilers/nqp/src/Grammar/Actions.pir	Thu May 28 12:11:42 2009	(r39216)
@@ -377,6 +377,7 @@
 ##        my $params := $past[0];
 ##        if $<declarator> eq 'method' {
 ##            $past.blocktype('method');
+##            $past.push(PAST::Op.new('inline'=>'.lex 'self', self);
 ##        }
 ##        for $<signature>[0] {
 ##            my $parameter := $($_<parameter>);
@@ -400,6 +401,9 @@
     $S0 = match['declarator']
     if $S0 != 'method' goto add_signature
     past.'blocktype'('method')
+    $P3 = get_hll_global ['PAST'], 'Op'
+    $P4 = $P3.'new'('inline'=>'    .lex "self", self', 'pasttype'=>'inline')
+    unshift past, $P4
   add_signature:
     $P0 = match['signature']
     $P0 = $P0[0]
@@ -738,14 +742,26 @@
 
 
 ##    method noun($/, $key) {
-##        make $($/{$key});
+##        if $key eq 'self' {
+##            make PAST::Var.new('name'=>'self', 'node'=>$/);
+##        }
+##        else {
+##            make $($/{$key});
+##        }
 ##    }
 .sub 'noun' :method
     .param pmc match
     .param pmc key
+
     $P0 = match[key]
+    if key == 'self' goto make_self
     $P1 = $P0.'ast'()
     match.'!make'($P1)
+    .return()
+  make_self:
+    $P9 = get_hll_global ['PAST'], 'Var'
+    $P1 = $P9.'new'('scope'=>'lexical', 'name'=>'self', 'node'=>$P0)
+    match.'!make'($P1)
 .end
 
 

Added: trunk/compilers/nqp/t/29-self.t
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/compilers/nqp/t/29-self.t	Thu May 28 12:11:42 2009	(r39216)
@@ -0,0 +1,28 @@
+#!./parrot nqp.pbc
+
+plan(3);
+
+class Foo {
+    method foo() { 1 };
+
+    method uno() {
+        self.foo();
+    };
+    
+    method des() {
+        if 1 {
+            self.foo();
+        }
+    };
+    
+    method tres($a) {
+        if 1 {
+            self.foo();
+        }
+    };
+};
+
+ok(Foo.new.uno, "Can access self within method");
+ok(Foo.new.des, "Can access self within sub-block");
+ok(Foo.new.tres(42), "Can access self within method with signature");
+


More information about the parrot-commits mailing list