tinypy: did i mention metaprogramming?
Monday, April 21st, 2008For 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!)