# New Ticket Created by Cory Spencer
# Please include the string: [perl #64574]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=64574 >
The attached patch makes the following changes to Range.pir and Range.pm:
- Moves !from_test, !flatten to Range.pm
- Changes $.from/$.to to private attributes and adds accessor methods
- Removes get_number/get_integer vtables methods from Range.pir and
moves them to Object.pir so that classes can numify properly
- Adds a Num method to Range.pm
Cory
>From e901f7b305df42b524520b1577ca1511ed00a400 Mon Sep 17 00:00:00 2001
From: git <cspen...@sprocket.org>
Date: Wed, 8 Apr 2009 21:10:12 -0700
Subject: [PATCH] commit b10555cdf1969d087d9b196be4cd4d2bfc4aab45
Author: git <cspen...@sprocket.org>
Date: Wed Apr 8 20:20:24 2009 -0700
Moved class definition to Range.pm, changed from/to to private attributes, added accessor methods for them, moved !flatten to Range.pm, added get_number/get_integer methods to Object.pir, removed them from Range.pir, added Num method to Range.pm, moved !from_test from Range.pir to Range.pm
---
src/classes/Object.pir | 10 +++++++
src/classes/Range.pir | 62 +----------------------------------------------
src/setting/Range.pm | 48 ++++++++++++++++++++++++++-----------
3 files changed, 46 insertions(+), 74 deletions(-)
diff --git a/src/classes/Object.pir b/src/classes/Object.pir
index 17f972e..0498bfa 100644
--- a/src/classes/Object.pir
+++ b/src/classes/Object.pir
@@ -712,6 +712,16 @@ Helper for doing calls on the metaclass.
.tailcall self.'Iterator'()
.end
+.sub '' :vtable('get_integer') :method
+ $I0 = self.'int'()
+ .return ($I0)
+.end
+
+.sub '' :vtable('get_number') :method
+ $N0 = self.'Num'()
+ .return ($N0)
+.end
+
.sub '' :vtable('get_string') :method
$S0 = self.'Str'()
.return ($S0)
diff --git a/src/classes/Range.pir b/src/classes/Range.pir
index 6f46fa9..6e70419 100644
--- a/src/classes/Range.pir
+++ b/src/classes/Range.pir
@@ -8,15 +8,6 @@ src/classes/Range.pir - methods for the Range class
=cut
-.namespace ['Range']
-
-.sub '' :anon :load :init
- .local pmc p6meta, rangeproto
- p6meta = get_hll_global ['Perl6Object'], '$!P6META'
- rangeproto = p6meta.'new_class'('Range', 'parent'=>'Any', 'attr'=>'$!by $!from $!to $!from_exclusive $!to_exclusive')
- rangeproto.'!IMMUTABLE'()
-.end
-
=head2 Methods
=over 4
@@ -29,6 +20,7 @@ just return a clone of the Range.
=cut
+.namespace ['Range']
.sub '' :method('list')
.local pmc range_it, result
range_it = self.'iterator'()
@@ -171,38 +163,13 @@ Return $x.HOW.
=over 4
-=item !flatten()
-
-=cut
-
-.namespace ['Range']
-.sub '!flatten' :method
- .tailcall self.'list'()
-.end
-
-=item !from_test(topic)
-
=item !to_test(topic)
-Returns true if C<topic> is greater than C<.from> / less than C<.to>,
-honoring exclusive flags.
+Returns true if C<topic> less than C<.to>, honoring exclusive flags.
=cut
.namespace ['Range']
-.sub '!from_test' :method
- .param pmc topic
- .local pmc from, fromexc
- from = getattribute self, '$!from'
- fromexc = getattribute self, '$!from_exclusive'
- if fromexc goto exclusive_test
- $I0 = isge topic, from
- .return ($I0)
- exclusive_test:
- $I0 = isgt topic, from
- .return ($I0)
-.end
-
.sub '!to_test' :method
.param pmc topic
.local pmc to, toexc
@@ -228,30 +195,6 @@ honoring exclusive flags.
=back
-=head2 Vtable functions
-
-=over
-
-=item VTABLE_get integer (vtable method)
-
-=item VTABLE_get_number (vtable method)
-
-=cut
-
-.sub '' :method :vtable('get_integer')
- $P0 = self.'list'()
- $I0 = $P0
- .return ($I0)
-.end
-
-.sub '' :method :vtable('get_number')
- $P0 = self.'list'()
- $N0 = $P0
- .return ($N0)
-.end
-
-=back
-
=cut
# Local Variables:
@@ -259,4 +202,3 @@ honoring exclusive flags.
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
-
diff --git a/src/setting/Range.pm b/src/setting/Range.pm
index e670832..59ba590 100644
--- a/src/setting/Range.pm
+++ b/src/setting/Range.pm
@@ -1,12 +1,12 @@
-class Range is also {
+class Range is Any {
has $.by = 1;
- has $.from;
+ has $!from is rw;
has $.from_exclusive = Bool::False;
- has $.to;
+ has $!to is rw;
has $.to_exclusive = Bool::False;
our Bool multi method ACCEPTS(Range $topic) {
- ($.from == $topic.from) && ($.to == $topic.to) &&
+ ($!from == $topic.from) && ($!to == $topic.to) &&
($.from_exclusive == $topic.from_exclusive) &&
($.to_exclusive == $topic.from_exclusive) &&
($.by == $topic.by)
@@ -17,41 +17,45 @@ class Range is also {
}
our Range multi method clone() {
- Range.new(:from($.from), :from_exclusive($.from_exclusive),
- :to($.to), :to_exclusive($.to_exclusive),
+ Range.new(:from($!from), :from_exclusive($.from_exclusive),
+ :to($!to), :to_exclusive($.to_exclusive),
:by($.by))
}
+ multi method from() {
+ $!from
+ }
+
our Range multi method iterator() {
$.clone
}
multi method max() {
- $.to
+ $!to
}
multi method min() {
- $.from
+ $!from
}
multi method minmax() {
- ($.from, $.to)
+ ($!from, $!to)
}
# TODO: Add support for the :by(..) adverbial modifier.
our Str multi method perl() {
if $.by == 1 {
[~]
- $.from.perl,
+ $!from.perl,
("^" if $.from_exclusive),
"..",
("^" if $.to_exclusive),
- $.to.perl;
+ $!to.perl;
} else {
'Range.new('
~ join(', ',
- 'from => ' ~ $.from.perl,
- 'to => ' ~ $.to.perl,
+ 'from => ' ~ $!from.perl,
+ 'to => ' ~ $!to.perl,
'by => ' ~ $.by.perl,
'from_exclusive => ' ~ $.from_exclusive.perl,
'to_exclusive => ' ~ $.to_exclusive.perl,
@@ -64,11 +68,27 @@ class Range is also {
@.list.reverse;
}
+ multi method to() {
+ $!to
+ }
+
our Bool multi method true() {
- self!to_test($.from_exclusive ?? ++($.from.clone) !! $.from)
+ self!to_test($.from_exclusive ?? ++($!from.clone) !! $!from)
+ }
+
+ our Num multi method Num() {
+ +$.list
}
our Str multi method Str() {
$.list
}
+
+ multi method !flatten() {
+ $.list
+ }
+
+ multi method !from_test($topic) {
+ $.from_exclusive ?? $topic > $!from !! $topic >= $!from
+ }
}
--
1.6.0.6