Code block literals

Bengt Richter bokr at oz.net
Mon Oct 13 00:18:43 EDT 2003


On Fri, 10 Oct 2003 16:28:11 GMT, Alex Martelli <aleax at aleax.it> wrote:

>Bengt Richter wrote:
>   ...
>> This way lambda would only be needed for backwards compatibility, and
>> since "def(" is a syntax error now, IWT it could be introduced cleanly.
>
>In theory, yes, I think it could (and wrt my similar idea with 'do' has
>the advantage of not requiring a new keyword).  In practice, trying to
>hack the syntax to allow it seems a little nightmare.  Wanna try your
>hand at it?  I'm thinking of Grammar/Grammar and Modules/parsermodule.c ...
>
Also tokenizer.c, so as not to ignore indentation when tokenizing a nameless
def inside a bracketed expression where (in|de)dents are otherwise ignored.

The thing is, the current tokenizer doesn't know def from foo, just that they're
names. So either indenting has to be generated all the time, and the job of
ignoring it passed on upwards, or the single keyword 'def' could be recognized
by the parser in a bracketed context, and it would generate a synthetic indent token
in front of the def name token as wide as if all spaces preceded the def, and then
continue doing indent/dedent generation like for a normal def, until the def suite closed,
at which point it would resume ordinary expression processing (if it was within brackets --
otherwise is would just be a discarded expression evaluated in statement context, and
in/de/dent processing would be on anyway. (This is speculative until really getting into it ;-)
Special-casing on a keyword in the tokenizer might be a practical implementation shortcut,
but it wouldn't be very aesthetic ;-/

IWT changes also in the compiler/code generator so it can handle generating code
for an anonymous def which will plug in like lambda in an expression, as
opposed to binding a name in a statement context, hopefully a slight change,
since it would be stacking a code object and calling makefunction either way.
The anonymous def just won't do the store to bind a name. Generating the code
object for the nameless def should be identical to the normal def IWT,
almost by definition ;-)

But the whole thing will be a bit of a chore I'm sure ;-)
Would it have a chance of getting adopted, do you think?

(Recent [cross]postings from the friendly :-)))) people got me to
playing with writing a little toy scheme environment in python, which is sooo
pleasant compared to using MASM on a 16-mhz 386 with 2mb ram (whoo, time flies).

Regards,
Bengt Richter




More information about the Python-list mailing list