why no "do : until"?

Robert Amesz rcameszREMOVETHIS at dds.removethistoo.nl
Fri Jan 12 21:28:17 EST 2001


D-Man wrote:

>On Fri, Jan 12, 2001 at 12:33:58AM +0000, Robert Amesz wrote:
>[snip]
>| 
>| Well, to put in my fl 0,02 (soon te be about E 0,01 <g>) I have to
>| say I feel that syntax is flawed: not only does it break the block
>| in two parts (whereas a dedent in other cases means the end of the
>| block is reached), but it's also visually ambiguous because a
>| 'while' could be the start of a loop, but it might also be
>| somewhere in the middle of one.
>| 
>| As I prefer programming languages to be as context-free as
>| possible, I'd like to make a counter-proposal:
>| 
>|      do:
>|           something
>|           until condition
>|           something else
>| 
>
>This also breaks the indent-denotes-block-structure style.  Is the
>"until" clause really a part of the "something" block?

Well, no, my point is that the entire block should run from 'something' 
to 'something else', following the normal flow of the program. The 
'until' is a statement for breaking out of the block, and therefore it 
should be part of it visually.

Also, splitting the block is a bad thing from the point of view of
the compiler: after a dedent the compiler will have to look ahead an
extra token to determine wether the loop just ends there, or
continues with an 'until'. Changing the lookahead in a parser can
cause a huge loss of performance, and older parser generators can
only cope with LL(0) grammars anyway. Also, such inconsistencies may
have the effect of making some error message more obscure and
unhelpful, and that will really make the syntactic sugar turn sour on
you. 

There's a way out of this dilemma, and that is to disallow 'do:' 
whithout an 'until' in the loop, but then we'd have to lose the ability 
to use 'do:' as an infinite loop and we'd still be stuck with the 
'while 1:' idiom.


>If it is adopted I think it ought to stand out more tying it more
>closely to the "do".  I wouldn't mind if, say, the until was to
>recieve a half-indent (or half-dedent depending on your view) as
>compared to the "something" and "something else" blocks.

If you want the 'until' to stand out more, just put an empty line after 
it. That's a lot easier than breaking Python's basic structure with 
half-dedents. Take a look:


      do:
           something
           until condition

           something else


That seems clear enough to me.


Also, one common case, when the 'until' is at the end of the loop, 
would be problematic if you dedent the until. You either get:


      do:
           something
           something else
      until condition

Oh, great, no colon or block after 'until': another special case to 
confuse the compiler and Python programmers alike.


Or - wait for it:

      do:
           something
           something else
      until condition:
           pass

Ugh, yacc![1], this is truly ugly. It's more like syntactic artificial 
sweetener this way.


In short, my proposal may be a compromise, but it gets the job doen, 
and would only call for a modest addition to Python's grammar, and thus 
would only require simple changes to the bytecode-compiler. It may be a 
low cost low impact proposal, but it would both add the 'do: until' 
idiom and provide an alternative for the 'while 1:' convention at the 
same time.


Robert Amesz
-- 
[1] Horrible pun alert. You may groan.



More information about the Python-list mailing list