[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[pygame] Interpreter
Pete Shinners <shredwheat@mediaone.net>:
>
> hookup to python interpreter :]
Here's a simple approach:
from miniconsole import *
import sys, traceback
class Interpreter(MiniConsole):
def __init__(self, *args, **kwds):
self.locals = {}
self.globals = {}
MiniConsole.__init__(self, *args, **kwds)
def eval(self, text):
print >> self, self.prompt + text
l, g = self.locals.copy(), self.globals.copy()
value = None; sys.stdout = self
try:
try:
value = eval(text, l, g)
except SyntaxError:
exec text in l, g
except:
traceback.print_exc(file=self)
else:
self.locals, self.globals = l, g
sys.stdout = sys.__stdout__
if value is not None: print >> self, repr(value)
def write(self, text):
text = text.strip()
if not text: return
print >> sys.__stdout__, 'TEXT="%s"'%text
text = self.font.render(text, 0, self.textcolor)
self.buffer = self.buffer[1:] + [text]
This can be tested with almost the same executable part... Just change
the USEREVENT handler to:
elif event.type == USEREVENT:
console.eval(event.command)
It seems to work for the basics, at least. It doesn't yet tackle
multiline stuff... I guess the easiest way to add that would be to use
trailing backslashes and feed the interpreter gradually until you get
a line with no trailing backslash, and then evaluate the result.
(Probably a trivial extension.) Detecting whether an expression isn't
terminater properly and then continuing to the next line etc. would
probably be more difficult.
--
Magnus Lie Hetland The Anygui Project
http://hetland.org http://anygui.org
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org