

Python Internals: Adding a new statement to Python - mnemonik
http://eli.thegreenplace.net/2010/06/30/python-internals-adding-a-new-statement-to-python/

======
brlewis
The Scheme version is not worthy of a blog post:

    
    
      (define-syntax until
        (syntax-rules ()
          ((until pred expr ...)
           (let loop ()
             (if (not pred)
    	     (begin
    	       (begin expr ...)
    	       (loop)))))))
    

In dialects with WHEN syntax (mzcheme, Kawa, probably many others) it's even
more concise:

    
    
      (define-syntax until
        (syntax-rules ()
          ((until pred expr ...)
           (let loop ()
    	 (when (not pred)
    	       (begin expr ...)
    	       (loop))))))

~~~
revorad
Do you use any lisp code in ourdoings?

~~~
brlewis
Yes, it's mostly Kawa Scheme, adapted for the web. You might notice
<http://brl.codesimply.net/> and <http://ourdoings.com/> have the same IP
address.

------
CWuestefeld
I will never, ever do this. But I still love this kind of article, that gives
a deeper insight into how things work.

~~~
Goladus
If you've never messed with real parsing and compiling before I highly
recommend it. It's like lifting programmer weights, so many other problems
seem easy afterwards.

~~~
CWuestefeld
Yes, I took compiler design in college, and even in the real world I've had
lex and yacc coming out my ears.

But specifically, I'm never going to add new statements to the python
interpreter.

------
draegtun
General observation about _until_. I think it works better "linguistically" as
a REPEAT/UNTIL (ala BASIC) and not as a negative _while_ loop.

So block of code always gets run at least once. For eg. in Perl:

    
    
        my $x;
        do { 
            print "guess the number? ";
            $x = readline;
        } until $x == 10;
    

Disclaimer: Can't recall using _until_ since I last played with BASIC. And
that was many years ago :)

------
jcsalterego
Great breakdown.

I tried something simpler once, trying to map str.format() to a binary
operator in Py3k.

<http://github.com/jcsalterego/py3k-atsign>

Needless to say it didn't pass the python-ideas (or was that python-dev?)
mailing list trial, mostly because it produced ambiguities that plagued the %
operator, which prompted this new str method in the first place.

------
mitko
Can anybody write a macro statement this way?

Something like:

macro square(x): return exec("#{x}*#{x}")

might be useful.

Edit: can anybody help me how about the comments markup. I tried searching
around but couldn't find anything.

------
zephjc
"There should be one-- and preferably only one --obvious way to do it."
'while' is it, 'until' is not.

The author even notes that 'until cond' is functionally identical to 'while
not condition'. A fun exercise for the author, if a bit silly.

~~~
brlewis
From the section "A language-advocacy digression":

 _This article doesn’t attempt to suggest the addition of an until statement
to Python. Although I think such a statement would make some code clearer, and
this article displays how easy it is to add, I completely respect Python’s
philosophy of minimalism. All I’m trying to do here, really, is gain some
insight into the inner workings of Python._

