Hi Jeff,

On Tue, May 20, 2008 at 2:15 AM, Jeff Saremi <[EMAIL PROTECTED]> wrote:
> could someone tell me how to read this please? I gave up on the
> corresponding Java section.
>
> from streams.py part of the python runtime
>
>     def addToSortedRewriteList(self, *args):
>
>        ... a whole bunch of lines
>
>         # first insert position for operation
>         for pos, searchOp in enumerate(rewrites):
>
>        ... a whole bunch of lines
>
>         else:
>             # new op is past any existing op, append to end
>             rewrites.append(op)
>
> I don't know if "else" corresponds to "for"? I used a ruler on my monitor to
> check. It looks like they line up.
> If so, what does an else mean at the end of a for? Is that when for has no
> elements to loop for?

The else block is executed when the for loop finished to it's end. So
there's another important (in this context) statement in the for loop,
something like
     if some_condition:
          break
(I wrote the code, but I'm too lazy to look it up right now ;) )
So if this break is not triggered, the else block triggers.

Usually I stayed very close to the Java implementation when porting it
to Python. IIRC this is one of the cases where my version looked very
different from Java, because Python has a much more powerful built-in
support for lists. There are a few occasions where I boiled down 30
lines of Java code to 2 or 3 Python lines ;)

> If you happen to be the one who wrote this I'd appreciate if you enlightened
> me with the whole logic of this method and what it tries to do.
> I just can't make anything out of the comments and code (no offence to you.
> I know you tried to preserve the original Java code).

This methods inserts a new rewrite operation into the list of already
existing rewrites. This list is sorted, so it has to find this right
position for it (after the first op with index>=newindex IIRC) and
also do the correct thing when rewrites get overwritten. If it ran
over the list without finding a proper place to put it, then it
appends it to the end (that's what the else clause is for).


HTH

-Ben
_______________________________________________
antlr-dev mailing list
[email protected]
http://www.antlr.org:8080/mailman/listinfo/antlr-dev

Reply via email to