> Hey all,
> 
> I'm trying to get functions working
> in python, and I'm not sure the best way
> to do this.
> 
> What seems natural to me is to define 
> subroutines in the middle of the code 
> as I walk the parse tree:
> 
> .sub __main__
>     goto endsub
>     .sub _f
>         print ":(\n"
>         ret
>    .end
> endsub:
>    $I0 = addr _f
>    end
> .end
> 
> 
> But of course, this prints a sad face. :/
> 
> I've read imcc/docs/parsing.pod, so I know
> why it does this... But what's the alternative?
> 
> I can store all my subroutine definitions in 
> a list or something and then dump them out
> after the "__main__" routine. Is that the
> right approach? It seems strange to me,
> but I'm new at this.

I think your approach may be fine.  You can store them in a list and
dump them at the cost of a little extra memory (not a concern at this
point), but you can also put them inline, so long as you have
something like this at the beginning of the file:

    .sub __START__
        call __main__
    .end

So it emits that code right away, because it's the first compilation
unit imcc sees.

> ---
> 
> Incidentally, I spent all day working on pirate,
> and it now generates (and runs!) code for a bunch
> of python structures:
> 
>     - lists, strings, ints
>     - assignment and multi-assignment ( x,y=1,2 )
>     - while, for, if/elif/else, break, continue
>     - math operations (+, -, *, /, %)
>     - boolean logic (and, or, not)
>     - comparison operators

Very Cool. 

> It now runs amk's euclid.py perfectly now.
> Do we have a way to compare the speed vs python? :)

We just modify it to repeat 100,000 times or so, and compare that way.

Which I did.  Parrot comes in about 3x slower than python on euclid.
>From looking at the imcc code, though, I think it could be much much
better.

One of my questions is, why do you make so many PerlNums when there
isn't a trace of a floating point number to be found...?

In any case, great work!

Luke

> Also, I wrote a pretty-printer for the lists in
> parrot, and you can call it (and presumably
> other parrot subs) directly from python code:
> 
> if 1 > 2:
>     _pyprint("one is greater than two...")
>     print "neat,huh?"
> 
> If I could get this subroutine stuff figured
> out, you could call functions written in 
> python, too. :)
> 
>   http://sixthdev.versionhost.com/viewcvs.cgi/pirate/
> 
> Sincerely,
>  
> Michal J Wallace
> Sabren Enterprises, Inc.



> -------------------------------------
> contact: [EMAIL PROTECTED]
> hosting: http://www.cornerhost.com/
> my site: http://www.withoutane.com/
> --------------------------------------

Reply via email to