# New Ticket Created by Stephane Payrard # Please include the string: [perl #53016] # in the subject line of all future correspondence about this issue. # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=53016 >
rakudo/hash_composer.t # new file and folder languages/perl6/config/makefiles/root.in # add hash.pir to the file to be compiler languages/perl6/src/builtins/hash.pir # hash() implementation $ diff -u /dev/null languages/perl6/t/rakudo/hash_composer.t --- /dev/null 2008-04-17 13:12:21.000000000 +0200 +++ /Users/stef/svn/parrot/payrard/junk/languages/perl6/t/rakudo/hash_composer.t 2008-04-17 18:00:01.000000000 +0200 @@ -0,0 +1,19 @@ +use v6-alpha; +use Test; + +plan 1 + + +my @a = (1, 2); +my %a = (3, 4); +my %h = hash( @a, %a, 5, 6); +my $s = %h{1} ~ %h{3} ~ %h{5}; +ok $s == "246", "hash( @a, %a, 5, 6) works"; + +# dies_ok is not yet implemented + +# dies_ok { my %h = hash(1); }, 'malformed hash composer: odd sequence of scalar items at index 0'; +# dies_ok { 'my @a = (1); hash( @a)' }, 'malformed hash composer: list of odd size at index 0'; # triggers "list of odd size why????" +# dies_ok { 'my @a = ( 1, 2, 3); hash( @a)' }, 'malformed hash composer: list of odd size at index 0'; + + $ $ svn diff languages/perl6/config/makefiles/root.in Index: languages/perl6/config/makefiles/root.in =================================================================== --- languages/perl6/config/makefiles/root.in (revision 27018) +++ languages/perl6/config/makefiles/root.in (working copy) @@ -60,6 +60,7 @@ src/builtins/cmp.pir \ src/builtins/control.pir \ src/builtins/guts.pir \ + src/builtins/hash.pir \ src/builtins/io.pir \ src/builtins/match.pir \ src/builtins/math.pir \ $ diff -u /dev/null languages/perl6/src/builtins/hash.pir --- /dev/null 2008-04-17 13:12:21.000000000 +0200 +++ languages/perl6/src/builtins/hash.pir 2008-04-17 17:42:53.000000000 +0200 @@ -0,0 +1,95 @@ +# Copyright (C) 2008, The Perl Foundation. + + +.include "iterator.pasm" +.namespace + +=pod + +The C<hash> list operator is a hash composer as defined in S04 +It creates a Hash and iterates on the list to fill the hash + +=cut + +.sub hash +.param pmc list :slurpy +.local pmc aiter # list iterator +.local pmc item # list item +.local pmc iiter # item iterator +.local pmc hash # hash to be returned +.local pmc key, value +.local int bool, size, position +.local string errstr, s + hash = new 'Hash' + position = -1 + aiter = new 'Iterator', list + aiter = .ITERATE_FROM_START +nextitem: # iterate on the list + unless aiter goto done + item = shift aiter + position = position + 1 + bool = isa item, 'Pair' + unless bool goto notpair + # the list item is a Pair + key = item.'key'() + value = item.'value'() + hash[key] = value + goto nextitem +notpair: + bool = does item, 'hash' + unless bool goto nothash + # the list item is a hash. Iterate on it. + iiter = new 'Iterator', item +iterhash: + iiter = .ITERATE_FROM_START + unless iiter goto nextitem + key = shift iiter + value = item[key] + hash[key] = value + goto iterhash +nothash: + bool = does item, 'array' + unless bool goto notarray + # The list item is an array. Checks its size is even then iterate. + size = item + size = size % 2 + if size goto arrayerr + iiter = new Iterator, item + iiter = .ITERATE_FROM_START +iterarray: + unless iiter goto nextitem + key = shift iiter + value = shift iiter + hash[key] = value + goto iterarray +notarray: + # the list item 'does' neither list or hash. It must be scalar + # ## is there a scalar role to check? + unless aiter goto nrscalerr + position = position + 1 + value = shift aiter + hash[item] = value + goto nextitem +nrscalerr: + errstr = "odd sequence of scalar items" + goto err +arrayerr: + errstr = "list of odd size" +err: + errstr = concat "malformed hash composer: ", errstr + errstr = concat errstr, " at index " + s = position + errstr = concat errstr, s + die errstr +done: + .return (hash) + +.end + + +# Local Variables: +# mode: pir +# cperl-indent-level: 4 +# fill-column: 100 +# End: +# vim: expandtab shiftwidth=4: $ -- cognominal stef