Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sub parse_name rewritten in NQP #1573

Closed
p6rt opened this issue Mar 4, 2010 · 5 comments
Closed

sub parse_name rewritten in NQP #1573

p6rt opened this issue Mar 4, 2010 · 5 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Mar 4, 2010

Migrated from rt.perl.org#73278 (status was 'resolved')

Searchable as RT73278$

@p6rt
Copy link
Author

p6rt commented Mar 4, 2010

From jaume.martif@gmail.com

Hi!

I have rewritten the sub "parse_name" in NQP.
hope it helps.

Jaume.

@p6rt
Copy link
Author

p6rt commented Mar 4, 2010

From jaume.martif@gmail.com

0001-sub-parse_name-rewritten-in-NQP.patch
From c068139d8e379d83876bc51f4695b86cf0b287e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaume=20Mart=C3=AD?= <jaume.martif@gmail.com>
Date: Thu, 4 Mar 2010 01:20:26 -0800
Subject: [PATCH] sub parse_name rewritten in NQP

---
 src/Perl6/Grammar.pm |   77 +++++++++++++++++++++----------------------------
 1 files changed, 33 insertions(+), 44 deletions(-)

diff --git a/src/Perl6/Grammar.pm b/src/Perl6/Grammar.pm
index 41e1672..c3bb1e5 100644
--- a/src/Perl6/Grammar.pm
+++ b/src/Perl6/Grammar.pm
@@ -1239,50 +1239,39 @@ grammar Perl6::Regex is Regex::P6Regex::Grammar {
 
 
 sub parse_name($name) {
-    # XXX Some enterprising soul could re-write this in NQP. ;-)
-    Q:PIR {
-        .local string name
-        $P0 = find_lex '$name'
-        name = $P0
-        ##  remove any type parameterization for now
-        .local string type_param
-        type_param = ''
-        $I0 = index name, '['
-        if $I0 == -1 goto type_param_done
-        type_param = substr name, $I0
-        name = substr name, 0, $I0
-      type_param_done:
-        ##  divide name based on ::
-        .local pmc list
-        list = split '::', name
-        ##  move any leading sigil to the last item
-        .local string sigil
-        $S0 = list[0]
-        sigil = substr $S0, 0, 1
-        $I0 = index '$@%&', sigil
-        if $I0 < 0 goto sigil_done
-        substr $S0, 0, 1, ''
-        list[0] = $S0
-        $S0 = list[-1]
-        $S0 = concat sigil, $S0
-        list[-1] = $S0
-      sigil_done:
-        ##  remove any empty items from the list
-        $P0 = iter list
-        list = new 'ResizablePMCArray'
-      iter_loop:
-        unless $P0 goto iter_done
-        $S0 = shift $P0
-        unless $S0 goto iter_loop
-        push list, $S0
-        goto iter_loop
-      iter_done:
-        if type_param == '' goto no_add_type_param
-        $S0 = pop list
-        concat $S0, type_param
-        push list, $S0
-      no_add_type_param:
-        .return (list)
+    my %is_sigil;
+    %is_sigil{'$'}:=1;
+    %is_sigil{'@'}:=1;
+    %is_sigil{'%'}:=1;
+    %is_sigil{'&'}:=1;
+    
+    my $type_param:='';
+    my $sep := pir::index__ISS($name,'['); 
+    if ($sep>-1) {
+      $type_param:= pir::substr__SSII($name,$sep);
+      $name:=pir::substr__SSII($name,0,$sep);
+    }
+    
+    my @parts:=pir::split__PSS('::',$name);
+    my $sigil:=pir::substr__SSII(@parts[0],0,1); 
+    if %is_sigil{$sigil} { 
+	@parts[0]:=pir::substr__SSII(@parts[0], 1);
+	my $last_part:=@parts.pop();
+	$last_part:=~$sigil ~ ~$last_part;
+	@parts.push($last_part);
+    }
+
+    my @result;
+    for @parts {
+	@result.push($_) if ($_);
+    }
+
+    if $type_param {
+	my $last_part:=@result.pop();
+	$last_part:=~$last_part ~ ~$type_param;
+	@result.push($last_part);
     }
+
+   @result;
 }
 
-- 
1.6.4.4

@p6rt
Copy link
Author

p6rt commented Mar 9, 2010

From @moritz

Thanks for your patch, I've committed it as
e4e1a0d5c197d5127dc9d4cc01e407216969cd62 with some changes​:

1) the %is_sigil hash is now a package variable, and initialized only
once at INIT time (suggested by pmichaud)
2) Lots of whitespace fixes​: although we're probably not very explicit
about that in our docs, we do stick to certain code layout standards​: 4
spaces for each indentation level (no tab), no trailing whitespaces,
blanks around most infix operator (except the comma, where we only put a
blank after it, as in ordinary typography).
3) concatenation already implies coercion to string, so ~$a ~ ~$b is
needlessly redundant -- $a ~ $b works just fine.

Cheers,
Moritz

@p6rt
Copy link
Author

p6rt commented Mar 9, 2010

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Mar 9, 2010

@moritz - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Mar 9, 2010
@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant