Motivation

I think I should better motivate this, to show that it's not an entirely esoterical approach...

With the following code I can easily simulate the semantics of List-Comprehensions which are quite popular in Python and Haskell.

{ my $list_ref=[]; sub gather (&){ my $code_ref=shift; my @list=(); my $safe=$list_ref; $list_ref=\@list; &$code_ref; $list_ref=$safe; return @list; } sub take { push @$list_ref, @_; return; } } @x= gather { for $c (1..10){ for $b (1..$c){ for $a (1..$b){ if ( $c**2 = += $b**2 + $a**2){ take [$c,$b,$a] } } } } }; }
(OK that's an extreme example for generating Pythagorean triples, but I hope the idea is clear and you don't blame me for the perl's need for curlies)

Now with the possibility to manipulate the call stack from within take(), it might be possible to use exactly the same syntax to produce a lazy iterator, just indicated by a switch. This could just be an additional attribute like @x=gather :lazy {...} to indicate a lazy generator.

An alterbnate approach would be to transform every opcode for calling "take()" into a "goto take()" followed by a reentry label. I tried this using B::Deparse and reevaluating the changed code into the symbol table, but that's not straightforwardly done, because of many special cases in perl's syntax, where you can't simply put the reentry-label just after the call...

That's why I beg for some peaces of the holy wisdom from the masters of opcodes ... ; )... before I start messing around by myself!

The potential of manipulating the call stack in this way would not only simplify this task, but Perl5 could get coroutines for free not talking about the cheaper "yield"-semantic in python and ruby.


In reply to Re: Callstack manipulation? by LanX
in thread Callstack manipulation? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.