Re: Perl6 and Duff's Device

2005-03-22 Thread TOGoS
> : I believe Perl 6 hasn't changed its policy on
> : labels, so you should be
> : able to write that in Perl 6.  But your behavior
> : might be undefined.
> : It's weird to jump into the middle of a loop.
> : We may only allow you to
> : jump outwards from your dynamic scope.
> 
> Perl 5's policy is to prohibit goto into
> anything that has to execute code to set up
> execution of the block, so you can't jump into a
> foreach loop, but you can jump into more basic
> sorts of loops.

In the last compiler I wrote, every node in the syntax
tree had a set of blocks of code that needed to be
emitted for various events. Just as finally (using
Java terminology, here, haven't kept up enough to know
Perl6's names for these things) blocks get executed
when falling out of a block, there were blocks for
entering, blocks for entering at the beginning
('falling in', so to speak), blocks for exiting,
blocks for exiting by 'falling out', blocks for
exiting via return, and blocks for exiting via
exception. When the compiler generated code for a
goto, it would simply emit exit code for all the
blocks it had to leave, and enter code for all the
blocks it went into. Setting up and tearing down pads
were part of the unconditional enter and exit blocks,
and these blocks could also be specified by the
language programmer like so:

  do {
stuff
  } enter {
set-up-code
  } exit {
tear-down-code
  } rescue {
exception-handling-code
  }

It had a nice symmetry to it.



__ 
Do you Yahoo!? 
Read only the mail you want - Yahoo! Mail SpamGuard. 
http://promotions.yahoo.com/new_mail 


Re: Perl6 and Duff's Device

2005-03-21 Thread Larry Wall
On Sun, Mar 20, 2005 at 05:27:56PM -0700, Luke Palmer wrote:
: I believe Perl 6 hasn't changed its policy on labels, so you should be
: able to write that in Perl 6.  But your behavior might be undefined.
: It's weird to jump into the middle of a loop.  We may only allow you to
: jump outwards from your dynamic scope.  

Perl 5's policy is to prohibit goto into anything that has to
execute code to set up execution of the block, so you can't jump
into a foreach loop, but you can jump into more basic sorts of loops.
Perl 5 preallocates pad entries for loops within the outer sub's pad,
so if you bypass a "my" you just get an undefined value but not a
core dump.  With the possibility of user-defined control structures,
we may have to limit goto to blocks that we know we can inline,
unless we define a goto interface for them.  (On the other hand,
we've been talking about multiple entry points for routines based on
whether you've already done the argument type checking, and that's
sort of the same idea.  So it might just fall out naturally from that.
I'm not holding my breath on it, though.)

Larry


Re: Perl6 and Duff's Device

2005-03-20 Thread Luke Palmer
Gaal Yahas writes:
> It looks like Duff's Device 
> won't be possible in Perl6. This is a shame.
> 
>  sub duff ($from) {
>  # real life would use reference here, this is a demo
>  # dummy: simulate write to serial i/o port
>  my $to;
>  my $i = 0;
>  
>  my ($n, $count);
>  $count = $from.chars;
>  
>  $n = ($count + 7) / 8; # use integer in effect

* There's no such thing as `use integer`.  You just declare $n and
  $count to be ints.

   my int ($n, $count);

>  
>  %MY::Â{"l" ~ $count % 8}Â.goto();
>  l0: do {$to ~= (substr $from, $i++, 1);
>  l7: $to ~= (substr $from, $i++, 1);
>  l6: $to ~= (substr $from, $i++, 1);
>  l5: $to ~= (substr $from, $i++, 1);
>  l4: $to ~= (substr $from, $i++, 1);
>  l3: $to ~= (substr $from, $i++, 1);
>  l2: $to ~= (substr $from, $i++, 1);
>  l1: $to ~= (substr $from, $i++, 1);
>  } while (--$n>0);

* `do ... while` is now named `loop ... while`.

>  return $to;
>  }
> 
> In Pugs at least, labels may only only be assigned to blocks.

I believe Perl 6 hasn't changed its policy on labels, so you should be
able to write that in Perl 6.  But your behavior might be undefined.
It's weird to jump into the middle of a loop.  We may only allow you to
jump outwards from your dynamic scope.  

Don't cry though.  Duff's device is an optimization technique, which
would probably do nothing for a Perl 6 program.  You have to optimize
dynamic languages in very different ways from how you optimize hardware.

Luke