Re: Automated indentation - proposal

Donald Beaudry (don@vicorp.com)
Fri, 13 May 94 09:57:41 EDT

There at least seems to be agreement on why the current indentation
scheme is both good and bad. And since nearly everyone else has
summerized, I thought I would do the same.

The current scheme is good because of the following features:
1) You cannot be fooled incorrect indentation.
2) It is very clean and easy to read.

But it has some problems.
1) It makes editing difficult (or at least requires a
different mind set from that required by other languages)
2) It makes it difficult or impossible to manipulate Python
code with tools, such as m4, that don't respect indentation.
3) It makes it impossible to write complete statments in a
single line. (remember that multi-line lambda thread...)

Unfortunately, any solution to problems 2 and 3 would negate feature
1, so some sort of compromise is in order. I have already posted not
only a proposal, but also an implementation (ok, ok, a hack) that
addresses problems 2 and 3 but at the expense of both features 1 and 2.

Marc Wachowitz has made a proposal which addresses problem 1, but not without
problems of its own.

m> [mw@ipx2.rz.uni-mannheim.de (Marc Wachowitz)]
m> Where the language now accepts a colon and an indented code block,
m> the colon may be replaced by the keyword "do" and the block be
m> closed with "end" "???" on the level of the block statement
m> (i.e. not the indented code) where "???" is the keyword of the
m> block statement ("if", "while", "try" etc.). Using the keyword "do"
m> also requires the closing statement, whereas using a colon does not
m> allow the end bracket. Different statements need not use the same
m> style.

This proposal comes real close to what I have been thinking about, but
fails to capture some important details. For instance, what happens
with else and elif statements?

if a > b do
print a
end if

is fine, but is this?

if a > b do
print a
else:
print b

or do you need to say,

if a > b do
print a
else do
print b
end if

or is it,

if a > b do
print a
else do
print b
end else

I have been hacking at the grammar again and with luck I should have
an implementation "real soon now". Here is what I have been trying to
achieve:

Any colon that open a block following a compound statement, (if,
while, for, try, def, class) can be omitted. If the colon is omitted,
the block must be closed by either an 'else', 'elif', 'except',
'finally', or 'end' followed by either 'while', 'for', 'try', 'def',
or 'class' depending on what statement opened the block.

So the example given above would be written as:

if a > b
print a
else
print b
end if

And since this colon is optional, it could be written a few other ways
as well. Such as,

if a > b
print a
else:
print b

or,
if a > b
print a
else:
print b
end if

(note that in this case, the 'end if' is purly optional because the
else is followed by a colon)

or even,

if a > b:
print a
else:
print b

But, if the colon is omitted, it would be a syntax error if the code
was not indented properly.

Admittedly, there are some problems here (at least I expect some,
since I haven't thought it all though yet). An automated formater
would have to be pretty smart about dealing with the cases where the
old style is mixed with the new.

I am not claiming that it is even possible to restructure the grammar
so as to allow this style of coding (I havn't done it yet), but this
should at least give you some idea about where I would like to see
things go.

--Don

______ ______
\_\_\_\ /#/#/#/
\_\_\_\ ______
\_\_\_V#/#/#/ Donald Beaudry don@vicorp.com
\_\_/#/#/#/ V. I. Corporation uunet!vicorp!don
\_/#/#/#/ 47 Pleasant Street PHONE: (413) 586-4144
V#/#/#/ Northampton, MA 01060 FAX: (413) 586-3805