I think you should try to implement lamda through .Sub's.  Take a look at 
parrot/t/pmc/sub.t for some examples.  However, you might not be able to rely on IMCC 
to handle arguments and results so much, since I don't think IMCC uses the new cps 
calling style yet.  (but I'm not sure, so don't hold me to that :)

You also might want to take a look at how languages/perl6 generates them, at 
P6C::IMCC::Sub (actually in the file perl6/P6C/IMCC.pm), although it still uses an 
older calling style.

-----Original Message-----
Date: Fri 08/01/03  1:49 PM
From: Michal Wallace  <[EMAIL PROTECTED]>
To:  [EMAIL PROTECTED]
CC: 
Subject: imcc's "call" vs first class functions


Hey all,

I've got lambda (single-expression anonymous subroutine) 
working in pirate now, but I wasn't sure how to get it
to do the correct calling convention with IMCC's "call".

For example, pirate turns this:

   print (lambda x: x+1)(0)  # prints "1\n"

into this: (the commented line is the important one) 

.sub __main__
    setline 1
    $I00002 = addr _sub00001
    $I00001 = $I00002
    .local object arg00001
    arg00001 = new PerlInt
    arg00001 = 0
    .arg arg00001

    jsr $I00001     #### :( why can't I "call"?#######

    .result $P00001
    .arg $P00001
    call __py__print
    print "\n"
    end
.end
.sub _sub00001
    # lambda from line 1
    saveall
    .param object x
    .local object res00001
    $P00001 = new PerlInt
    $P00001 = x
    $P00002 = new PerlInt
    $P00002 = new PerlInt  # yes, this is a bug :)
    $P00002 = 1
    $P00003 = new PerlInt
    $P00003 = $P00001 + $P00002
    res00001 = $P00003
    .return res00001
    restoreall
    ret
.end


This code works, but it doesn't follow the convention.
I was hoping to use call for this, but I couldn't get it
to work, because I'm not directly calling _sub00001.

Actually, in this case I probably could do that, since
it's anonymous, but python has first-class functions,
which means you can do this:

   a = b = c = lambda x: x+1
   assert a(1) == b(1) == c(1)

And then you can call a() or b() or whatever. Basically,
python needs to look up the function's address at runtime
and I couldn't figure out how to make that happen with
call... So I just did jsr for now, hoping someone could
tell me how to fix it.

Is that even possible with "call"? If not, we can just
hard code the instructions, but it seems like it really
ought to be in imcc. :)


Sincerely,
 
Michal J Wallace
Sabren Enterprises, Inc.
-------------------------------------
contact: [EMAIL PROTECTED]
hosting: http://www.cornerhost.com/
my site: http://www.withoutane.com/
--------------------------------------






--
This message was sent using 3wmail.
Your fast free POP3 mail client at www.3wmail.com

Reply via email to