# New Ticket Created by  Cory Spencer 
# Please include the string:  [perl #64370]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=64370 >



The attached patch moves much of Range.pir's functionality into Range.pm 
in the setting.
>From b4a9fbdffccedd3c22b203f5fb1d403d588116f3 Mon Sep 17 00:00:00 2001
From: git <cspen...@sprocket.org>
Date: Wed, 1 Apr 2009 23:28:52 -0700
Subject: [PATCH] Moved much of Range.pir's functionality to Range.pm in the setting.

---
 build/Makefile.in     |    1 +
 src/classes/Range.pir |  181 +------------------------------------------------
 src/setting/Range.pm  |   65 ++++++++++++++++++
 3 files changed, 68 insertions(+), 179 deletions(-)
 create mode 100644 src/setting/Range.pm

diff --git a/build/Makefile.in b/build/Makefile.in
index be5fa6a..ad56a09 100644
--- a/build/Makefile.in
+++ b/build/Makefile.in
@@ -119,6 +119,7 @@ SETTING = \
   src/setting/Match.pm \
   src/setting/Object.pm \
   src/setting/Pair.pm \
+  src/setting/Range.pm \
   src/setting/Str.pm \
   src/setting/Whatever.pm \
 
diff --git a/src/classes/Range.pir b/src/classes/Range.pir
index da66482..d94e1bb 100644
--- a/src/classes/Range.pir
+++ b/src/classes/Range.pir
@@ -13,7 +13,7 @@ src/classes/Range.pir - methods for the Range class
 .sub '' :anon :load :init
     .local pmc p6meta, rangeproto
     p6meta = get_hll_global ['Perl6Object'], '$!P6META'
-    rangeproto = p6meta.'new_class'('Range', 'parent'=>'Any', 'attr'=>'$!from $!to $!from_exclusive $!to_exclusive')
+    rangeproto = p6meta.'new_class'('Range', 'parent'=>'Any', 'attr'=>'$!by $!from $!to $!from_exclusive $!to_exclusive')
     rangeproto.'!IMMUTABLE'()
 .end
 
@@ -21,89 +21,6 @@ src/classes/Range.pir - methods for the Range class
 
 =over 4
 
-=item ACCEPTS(topic)
-
-Determines if topic is within the range or equal to the range.
-
-=cut
-
-.sub 'ACCEPTS' :method
-    .param pmc topic
-
-    $I0 = isa topic, 'Range'
-    unless $I0 goto value_in_range_check
-    $I0 = self.'from'()
-    $I1 = topic.'from'()
-    if $I0 != $I1 goto false
-    $I0 = self.'to'()
-    $I1 = topic.'to'()
-    if $I0 != $I1 goto false
-    $P0 = getattribute self, "$!from_exclusive"
-    $P1 = getattribute topic, "$!from_exclusive"
-    if $P0 != $P1 goto false
-    $P0 = getattribute self, "$!to_exclusive"
-    $P1 = getattribute topic, "$!to_exclusive"
-    if $P0 != $P1 goto false
-    goto true
-
-  value_in_range_check:
-    $I0 = self.'!from_test'(topic)
-    unless $I0 goto false
-    $I0 = self.'!to_test'(topic)
-    unless $I0 goto false
-
-  true:
-    $P0 = get_hll_global ['Bool'], 'True'
-    .return ($P0)
-  false:
-    $P0 = get_hll_global ['Bool'], 'False'
-    .return ($P0)
-.end
-
-
-=item clone()   (vtable method)
-
-Create a clone of the Range.
-
-=cut
-
-.sub 'clone' :method :vtable
-     $P0 = self.'!cloneattr'('$!from $!to $!from_exclusive $!to_exclusive')
-     .return ($P0)
-.end
-
-=item from()
-
-=item to()
-
-Gets the beginning or end of the range.
-
-=cut
-
-.sub 'from' :method
-    $P0 = getattribute self, '$!from'
-    .return ($P0)
-.end
-
-.sub 'to' :method
-    $P0 = getattribute self, '$!to'
-    .return ($P0)
-.end
-
-
-=item iterator()  (vtable function)
-
-Return an iterator for the Range.  Since Ranges are already
-iterators, we can just return a clone.
-
-=cut
-
-.sub 'iterator' :method :vtable('get_iter')
-    $P0 = clone self
-    .return ($P0)
-.end
-
-
 =item list()
 
 Generate the Range in list context.  Currently we generate all
@@ -126,65 +43,13 @@ just return a clone of the Range.
 .end
 
 
-=item max()
-
-=item min()
-
-=item minmax()
-
-=cut
-
-.namespace ['Range']
-
-.sub 'max' :method
-    .tailcall self.'to'()
-.end
-
-.sub 'min' :method
-    .tailcall self.'from'()
-.end
-
-.sub 'minmax' :method
-    $P0 = self.'from'()
-    $P1 = self.'to'()
-    $P2 = get_hll_global 'list'
-    .tailcall $P2($P0, $P1)
-.end
-
-
-=item perl()
-
-Returns a Perl representation of the Range.
-
-=cut
-
-.sub 'perl' :method
-    .local string result, tmp
-    .local pmc from, fromexc, toexc, to
-    from = getattribute self, '$!from'
-    fromexc = getattribute self, '$!from_exclusive'
-    toexc = getattribute self, '$!to_exclusive'
-    to = getattribute self, '$!to'
-    result = from.'perl'()
-    unless fromexc goto dots
-    result .= '^'
-  dots:
-    result .= '..'
-    unless toexc goto end
-    result .= '^'
-  end:
-    tmp = to.'perl'()
-    result .= tmp
-    .return (result)
-.end
-
-
 =item pop()  (vtable_method)
 
 Generate the next element at the end of the Range.
 
 =cut
 
+.namespace ['Range']
 .sub 'pop' :method :vtable('pop_pmc')
     .local pmc to, toexc, value
     to = getattribute self, '$!to'
@@ -201,21 +66,6 @@ Generate the next element at the end of the Range.
 .end
 
 
-=item reverse()
-
-Generate the range in reverse sequence.  (This is wrong for now--
-really what should happen is that we invert .from and .to and
-switch the :by argument.)
-
-=cut
-
-.namespace ['Range']
-.sub 'reverse' :method
-    $P0 = self.'list'()
-    .tailcall $P0.'reverse'()
-.end
-
-
 =item shift()   (vtable_method)
 
 Generate the next element at the front of the Range.
@@ -238,25 +88,6 @@ Generate the next element at the front of the Range.
 .end
 
 
-=item true()
-
-Return true if there are any more values to iterate over.
-
-=cut
-
-.sub 'true' :method :vtable('get_bool')
-    .local pmc from, fromexc
-    from = getattribute self, '$!from'
-    fromexc = getattribute self, '$!from_exclusive'
-    unless fromexc goto have_value
-    from = clone from
-    'postfix:++'(from)
-  have_value:
-    $I0 = self.'!to_test'(from)
-    .return ($I0)
-.end
-
-
 =back
 
 =head2 Operators
@@ -405,8 +236,6 @@ honoring exclusive flags.
 
 =item VTABLE_get_number (vtable method)
 
-=item VTABLE_get_string (vtable method)
-
 =cut
 
 .sub '' :method :vtable('get_integer')
@@ -421,12 +250,6 @@ honoring exclusive flags.
     .return ($N0)
 .end
 
-.sub '' :method :vtable('get_string')
-    $P0 = self.'list'()
-    $S0 = $P0
-    .return ($S0)
-.end
-
 =back
 
 =cut
diff --git a/src/setting/Range.pm b/src/setting/Range.pm
new file mode 100644
index 0000000..acad6e3
--- /dev/null
+++ b/src/setting/Range.pm
@@ -0,0 +1,65 @@
+class Range is also {
+    has $.by = 1;
+    has $.from;
+    has $.from_exclusive = Bool::False;
+    has $.to;
+    has $.to_exclusive = Bool::False;
+
+    our Bool multi method ACCEPTS(Range $topic) {
+        ($.from == $topic.from) && ($.to == $topic.to) &&
+        ($.from_exclusive == $topic.from_exclusive) &&
+        ($.to_exclusive == $topic.from_exclusive) &&
+        ($.by == $topic.by)
+    }
+
+    our Bool multi method ACCEPTS($topic) {
+        self!from_test($topic) && self!to_test($topic)
+    }
+
+    our Range multi method clone() {
+        Range.new(:from($.from), :from_exclusive($.from_exclusive),
+                  :to($.to), :to_exclusive($.to_exclusive),
+                  :by($.by))
+    }
+
+    our Range multi method iterator() {
+        $.clone
+    }
+
+    multi method max() {
+        $.to
+    }
+
+    multi method min() {
+        $.from
+    }
+
+    multi method minmax() {
+        ($.from, $.to)
+    }
+
+    # TODO: Add support for the :by(..) adverbial modifier.
+    our Str multi method perl() {
+        [~] gather {
+            take $.from.perl;
+            take "^" if $.from_exclusive;
+            take "..";
+            take "^" if $.to_exclusive;
+            take $.to.perl
+        }
+    }
+
+    our Range multi method reverse() {
+        Range.new(:from($.to), :from_exclusive($.to_exclusive),
+                  :to($.from), :to_exclusive($.from_exclusive),
+                  :by(-$.by))
+    }
+
+    our Bool multi method true() {
+        self!to_test($.from_exclusive ?? ++($.from.clone) !! $.from)
+    }
+
+    our Str multi method Str() {
+        $.list
+    }
+}
-- 
1.6.0.6

Reply via email to