Phil Hassey - game dev blog
Phil Hassey as Syndrome
"Look, stores don't sell
costumes like this to just anyone."

tinypy: did i mention metaprogramming?

For the sake of this post, I’m going to pretend to know what metaprogramming* is.  Yeah, so tinypy** totally has that.  At least, since the parser and compiler in tinypy is written in tinypy, you are able to modify those modules on-the-fly and add new features into the tinypy language.  (Not that you’d want to, but certain other languages get so uppity about being able to do that, I figured I’d plug for tinypy here.)

For example, (at present) tinypy doesn’t have support for decorators.  I’ve always liked decorators, so I made this code (a zip of main.py, deco.py***, and test.py) so that if you have a main.py:

import deco
import test

When the deco module is loaded, it cleanly**** adds decorator support into the tokenize, parse, and encode modules of tinypy.  Then when the test module is loaded, it is able to use decorator syntax.  Yay!  This mostly thanks to the top down operator precedence implementation in tinypy.

So now, if say, you have some crazy idea for how the $ operator should be used in bigpy, you can go ahead and use metaprogramming to add it into tinypy and show all your friends how awful your new syntax looks and have a working proof-of-concept!  Yay!

* feel free to enlighten me
** it’s got a mailing list now, join in on all the fun!!
*** only 611 bytes :)  They were pretty simple to implement, since they really just mean: “given ‘@a \n def b …’ do ‘def b … \n b=a(b)'”
**** Since all the language features are stored in dictionaries, it’s “pretty easy” to add new symbols / operators.  (Or remove features, or whatever!)

9 Responses to “tinypy: did i mention metaprogramming?”

  1. Lennart Regebro Says:

    Ooh, nice. It’s always annoyed me that Python doesn’t have an until-statement, and that I can’t add one without changing the c-code. Not that I’m actually going to start using TinyPy just because of this, but it’s still nice. 🙂

  2. she Says:

    One problem is, IMO the word “meta” programming. To me it implies that there is so much magic involved that not even the programmer knows whats going on anymore

  3. tonetheman Says:

    Man that is cool

  4. Doug Napoleone Says:

    “Until you read ‘the art of the meta object protocol’, implemented the actuator, and have taught it to someone else, you do not know what meta-programming is. That said, once you do that, please explain it to me.” -Quote from prolog users mailing list.

    That is from my quotes.txt file that I have been building for the past 15 years. 😉

    @Lennart:

    As for the until statement, you can create that extension quite easily in python, it just looks weird:

    @until(lambda x: x==4)
    def loop():
    do some work
    return vars_to_be_tested

    Also there are better logic constructs that you can do with the ‘with’ statement as well. I feel that is a better meme than the until statement as it supports error handling and cleanup in a much more sane manner. @contextmanager rocks!

    To quote Tim Peters from 2000:

    To summarize 10 years of fruitless discussion on this issue: yes,

    while 1:

    if finished:
    break

    is the intended way to spell “do … until” in Python.

    “do … until F” is better than “do … while F” because the truth of F
    conjoined with the loop invariant and loop precondition should imply the
    loop postcondition, and the “while” form artificially negates the F of
    interest, thus making reasoning harder.

    “do … while F” is better than “do … until F” because the truth of F
    conjoined with the loop invariant and loop precondition should imply that
    the loop invariant still holds, and the “until” form artificially negates
    the F of interest, thus making reasoning harder.

    Python’s indentation rules do indeed not fit well with either form. If
    there were an obvious way to make them fit well, Guido would have done so a long time ago. In the meantime, if you think you see one, write a PEP!

    and-no-of-course-uninitialized-vrbls-exist-in-python-ly y’rs – tim

  5. damien Says:

    actually a do.. until can be done relatively easily, if you allow a “until cond” statements anywhere in the block and treat them as “if cond: break”

    better yet, just have a “do:” statement which is an infinite loop and break out of it with “if cond: break”

    do:
    stmt
    stmt
    until x > 1
    stmt
    until othercond

  6. philhassey Says:

    @nevinera replying to she: “you clearly only know languages in which it is difficult then.”

    I think in the case of tinypy, I’d categorize my “adding decorators by an import” as “hard enough that I had to think” but not so hard that it took me more than half an hour.

  7. philhassey Says:

    @Doug

    I feel enlightened. Sort of.

  8. a random John Says:

    Awesome. ‘$’ should be short for ‘self.’ similar to how ‘?’ was short for ‘print’ in basic.

  9. Lennart Regebro Says:

    I have to admit that I didn’t understand any of the answers. 🙂

    I don’t want a do until btw. Because that doesn’t work with the indentation in a reasonable way. I just want an until.

    until x:
    do something

    Would work like “while x” except that it would always loop once.