execute a function after each source code line ?

Steve Howell showell30 at yahoo.com
Fri Jun 1 05:53:27 EDT 2007


--- stef <s.mientki at id.umcn.nl> wrote:
> doing a simulation of another language (JAL),
> I translate the other language into Python code,
> then I execute this converted Python code.
> [...]
> (btw the whole program is running as an graphical
> (wxPython) application)

I worked on an open source project that uses Python to
intrepret another language (an educational language
called Guido van Robot), and it also uses wxPython to
allow the user to step through the program one line at
a time, and the user gets quite visual feedback. 
Check out these screen shots, which hopefully
illustrate something along the lines of what you're
trying to achieve. 

http://gvr.sourceforge.net/screen_shots/

During the project, we definitely had some lessons
learned.  The Python-based interpreter originally
translated the GvR source into a Python program, and
this worked great when the interface was curses.

When we went to wxPython, it eventually became clear
that it would be very difficult to make this approach
work inside of wxPython.  I'm not saying it's
impossible, but in wxPython, or really any GUI app,
it's hard to have your interpreted program drive
things, without turing the event loop inside out.  It
might become easier to turn your own program inside
out.  I think you know where this is going...

You need to build a virtual machine.  

It might not be quite as hard as it sounds, although
it certainly depends on the complexity of the
interpreted language.

We ended up making the translater create an AST tree,
which was essentially a tree of tiny little Python
objects.  We didn't create bytecode per se; the
virtual machine acted directly on the AST.

Then we had a stepper class step through the code.

Here's the code for the stepper:

http://gvr.cvs.sourceforge.net/gvr/GvR/TESTstepper.py?revision=1.28&view=markup

http://gvr.cvs.sourceforge.net/gvr/GvR/stepper.py?revision=1.36&view=markup

Here's the code for the translator:

http://gvr.cvs.sourceforge.net/gvr/GvR/TESTgvrparser.py?revision=1.23&view=markup

http://gvr.cvs.sourceforge.net/gvr/GvR/gvrparser.py?revision=1.53&view=markup

When you call into the stepper, it has no notion of
the GUI around it; it just has a world object that it
can call simple methods on.

http://gvr.cvs.sourceforge.net/gvr/GvR/guiWorld.py?revision=1.5&view=markup

There's a little more code here, but hopefully the
above provides a sketch.

http://gvr.cvs.sourceforge.net/gvr/GvR/

It's a pretty small project, so you may just want to
download all the source and check it out in action. 
It should run on Linux and Windows, and I know we had
it running on the Macs at one point.




 
____________________________________________________________________________________
TV dinner still cooling? 
Check out "Tonight's Picks" on Yahoo! TV.
http://tv.yahoo.com/



More information about the Python-list mailing list