Author: coke Date: Mon Apr 30 14:01:27 2007 New Revision: 18360 Added: trunk/runtime/parrot/library/Range.pir trunk/t/library/range.t (contents, props changed) Modified: trunk/ (props changed)
Log: Barebones first pass at S03 Range objects. Added: trunk/runtime/parrot/library/Range.pir ============================================================================== --- (empty file) +++ trunk/runtime/parrot/library/Range.pir Mon Apr 30 14:01:27 2007 @@ -0,0 +1,139 @@ +=head1 Range + +http://perlcabal.org/syn/S03.html#Range_semantics + +=cut + +.HLL 'parrot', '' +.namespace [ 'Range' ] + +.macro exhausted_check() + .sym pmc exhausted + exhausted = getattribute self, 'exhausted' + unless exhausted goto .$more + .sym pmc exception + exception = new 'Exception' + exception[0] = 'Exhausted Range' + throw exception +.local $more: +.endm + +.macro exhausted() + .sym pmc exhausted + exhausted = new 'Boolean' + exhausted = 1 + setattribute self, 'exhausted', exhausted +.endm + + +.sub __class_init :anon :load + + $P1 = newclass 'Range' + + addattribute $P1, 'from' + addattribute $P1, 'to' + addattribute $P1, 'by' + addattribute $P1, 'reversed' + addattribute $P1, 'exhausted' +.end + +# XXX :method is only specified here so we can get 'self' +.sub 'init' :vtable :method + $P1 = new 'Integer' + $P1 = 1 + setattribute self, 'by', $P1 + $P1 = new 'Boolean' + $P1 = 0 + setattribute self, 'reversed', $P1 + $P1 = new 'Boolean' + $P1 = 0 + setattribute self, 'exhausted', $P1 +.end + + +# XXX handle reversed. +.sub get_from :method + .local pmc from + from = getattribute self, 'from' + .return (from) +.end + +# XXX handle reversed. +.sub get_to :method + .local pmc to + to = getattribute self, 'to' + .return (to) +.end + +# XXX handle reversed. +.sub get_min :method + .local pmc from + from = getattribute self, 'from' + .return (from) +.end + +# XXX handle reversed. +.sub get_max :method + .local pmc to + to = getattribute self, 'to' + .return (to) +.end + +.sub get_minmax :method + $P1 = new .Array + $P1 = 2 + .local pmc min, max + min = self.'get_min'() + max = self.'get_max'() + $P1[0] = min + $P1[1] = max + .return ($P1) +.end + +.sub shift :method + .exhausted_check() + + .local pmc exhausted, from, to, by + from = getattribute self, 'from' + to = getattribute self, 'to' + by = getattribute self, 'by' + $P0 = clone from + $P0 += by + setattribute self, 'from', $P0 + if $P0 <= to goto done + .exhausted() +done: + .return (from) +.end + +.sub pop :method + .exhausted_check() + # can we pull a number off the end? + # return it. + + .local pmc from, to, by + from = getattribute self, 'from' + to = getattribute self, 'to' + by = getattribute self, 'by' + $P0 = clone to + $P0 -= by + setattribute self, 'to', $P0 + if $P0 >= from goto done + .exhausted() +done: + .return (to) +.end + +.sub reverse :method + .local pmc reversed + reversed = getattribute self, 'reversed' + reversed = not reversed + setattribute self, 'reversed', reversed + .return(self) +.end + +# Local Variables: +# mode: pir +# fill-column: 100 +# End: +# vim: expandtab shiftwidth=4: Added: trunk/t/library/range.t ============================================================================== --- (empty file) +++ trunk/t/library/range.t Mon Apr 30 14:01:27 2007 @@ -0,0 +1,73 @@ +#! parrot +# Copyright (C) 2007, The Perl Foundation. +# $Id: $ + +=head1 NAME + +range.t + +=head1 SYNOPSIS + + % prove range.t + +=head1 DESCRIPTION + +Tests the Range class. + +=cut + +.sub main :main + # load this library + load_bytecode 'library/Test/More.pir' + + # get the testing functions + .local pmc exports, curr_namespace, test_namespace + curr_namespace = get_namespace + test_namespace = get_namespace [ "Test::More" ] + exports = split " ", "plan diag ok is is_deeply like isa_ok" + + test_namespace."export_to"(curr_namespace, exports) + + load_bytecode 'Range.pir' # XXX eventually convert to pbc. + + plan(5) + + # XXX Convert this to the PDD15 way of creating an object + # at the same time you specify the attributes... + + $P0 = new 'Range' + $P1 = new 'Integer' + $P1 = 1 + setattribute $P0, 'from', $P1 + $P1 = new 'Integer' + $P1 = 3 + setattribute $P0, 'to', $P1 + ok(1, 'Created new range') + $I0 = $P0.'shift'() + is($I0, 1, '1 - 1st') + $I0 = $P0.'shift'() + is($I0, 2, '1 - 2nd') + $I0 = $P0.'shift'() + is($I0, 3, '1 - 3rd') + + push_eh test_1_catch + $I0 = $P0.'shift'() + ok(0,'1 - exhausted') + goto test_2 + clear_eh + +test_1_catch: + # XXX should have more thorough exception check + ok(1,'1 - exhuasted') + +test_2: + + +.end + +# Local Variables: +# mode: cperl +# cperl-indent-level: 4 +# fill-column: 100 +# End: +# vim: expandtab shiftwidth=4:
