Phil Hassey - game dev blog
Phil Hassey as Rambo
".. I've been there,
I know what it's like,
and I'll do it again if I have to."

tinypy 1.0 – MIT License and swell OpenGL demo :)

Its been a long two months getting this project to the 1.0 state. Here it is:

tinypy1.zip | tinypy.tgz | svn://www.imitationpickles.org/tinypy/tags/1.0.0

$ python build.py
$ ./tinypy-sdl julia.py *
$ ./tinypy your-program-goes-here.py

The win32.zip includes the tinypy.exe and tinypy-sdl.exe binaries, so you can skip the building process.

tp-mandel.png

Features include the ability to parse and compile to bytecode a pretty decent subset of python code. Its got a pretty simple API, incremental garbage collection, and a handful of builtin functions. The vm supports strings, dicts, lists, numbers, functions, methods, and custom data types. tinypy even has exotic features like list comprehensions, variable and named function arguments, inheritance, and exceptions with tracebacks. All this in just 64k** of code!!

ld105-philshot.png

I also had some fun this past weekend testing tinypy “in the field” making an OpenGL tetris knock-off. My apologies to Alexey Pajitnov for my misguided contribution of tinypy to the open source stack. win32.zip | svn://www.imitationpickles.org/ld105/trunk

I hope you enjoy checking out tinypy – I’ve had a pretty wild ride building it.

*julia.py included is a low-res “realtime” jula fractal demo. I felt like rendering a mandelbrot for this article.

**See README.txt for my definition of 64k. It’s close enough.

21 Responses to “tinypy 1.0 – MIT License and swell OpenGL demo :)”

  1. mark dufour Says:

    hi phil,

    congratulations on getting to 0.1!

  2. mark dufour Says:

    that was not meant as a bad joke – of course I meant 1.0! 🙂

  3. Dave Says:

    Big pat on the back man!

  4. Tj Says:

    Rock on! This is great!

  5. Ido Yehieli Says:

    Good job! This is awesome.

  6. philhassey Says:

    Thanks 🙂

  7. Ido Yehieli Says:

    Phil – how can i run sdl programs directly on the vm? i tried:

    $ ./vm py2bc.tpc julia.py julia.tpc
    $ ./vm julia.tpc
    320 240

    File “julia.py”, line 26, in ?
    set_mode(SW,SH)

    Exception:
    tp_get: KeyError: set_mode

  8. philhassey Says:

    Ido – you’d have to build a vm with the SDL functions built in. Take tinypy-sdl.c and tweak the main function to be more like the main function from vmmain.c. Doing it that way would be useful for distributing a game where you don’t want to support any in-game scripting, or in a case where you really don’t want all that bytecode in memory for some reason.

  9. Ido Yehieli Says:

    Thanks.

    Does that mean that if I use ‘eval’ in my python program i have to provide the tinypy compiler as well as the vm?

  10. shaurz Says:

    Nice, a nod to “Top Down Operator Precedence” in your parser 🙂

  11. philhassey Says:

    Ido: Yeah, to use eval or similar stuff the compiler must be included so it can dynamically compile the code.

    shaurz: Indeed – http://javascript.crockford.com/tdop/tdop.html – was what got me through 🙂 I tried some other approaches, but TDOP really suited me.

  12. philhassey Says:

    Ido: just to clarify, tinypy includes the VM, so you don’t have to have both “vm.exe” and “tinypy.exe” available for things to work.

  13. Nelson Castillo Says:

    Nice piece of work 🙂

  14. Harry Roberts Says:

    Wow, that’s pretty cool. I can get the VM executable down to about 17k (-Os + strip + upx).

    I’m thinking of a few things I can add to this, the first thing that comes it mind is transparent .zip file access via zziplib (http://zziplib.sourceforge.net/zzip-file.html) so a project can be distributed as vm + (compiled) code.zip

    I’m also pondering an implementation of tasklets/channels/io ala Stackless Python, abusing libevent as a kernel.

    Also – where can I send patches in?

  15. philhassey Says:

    Harry – Cool stuff, I’d love to see some interesting stuff done with this. Patches can be sent to my name as one word @ gmail.com … Sometime soon I should set up some kind of “project” thing for this. But I haven’t gotten around to it yet.

  16. Code Bus Says:

    […] recently stumbled across TinyPy by Phil Hassey, what an amazing little project – a Python compiler & virtual machine that weighs in […]

  17. Mike Cantelon Says:

    Damn… I get a funk error when attemptng to build on OS X:

    gcc -Wall -g vmmain.c -lm -o vm
    /usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libm.dylib unknown flags (type) of section 6 (__TEXT,__literal16) in load command 0
    /usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libSystem.dylib unknown flags (type) of section 6 (__TEXT,__literal16) in load command 0
    collect2: ld returned 1 exit status
    exit 256

  18. philhassey Says:

    Hmmn. I just tried tinypy on my OSX system and didn’t have any trouble. I’m not really sure what that error means, but if you figure it out, please tell me 🙂

  19. John Says:

    Sorry for what I am sure in a noob question but how do I go about outputing to the console as it seems to output to file by default?

  20. philhassey Says:

    John – check out the latest svn, there is a fix for windows that fixes that.

  21. Philip Semanchuk Says:

    @Mike Cantelon: I got this same error because I had upgraded to 10.5 from 10.4 (Ocelot or whatever it was called) without upgrading XCode. Once I installed the latest XCode (v3 I believe) from the Leopard install disc, I no longer got the error “libSystem.dylib unknown flags (type) of section 6 (__TEXT,__literal16) in load command 0”.

    HTH