Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

[perl #53016] [PATCH] hash() composer function as perl S04
# 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_compos
er.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 +020
0
@@ -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


Report this thread to moderator Post Follow-up to this message
Old Post
Stephane Payrard (via RT)
04-18-08 12:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Perl 6 Compiler archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:23 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.