def run(self,n=1): self.tick += n for queue in self.itemQueues.keys(): if self.tick % queue == 0: if queue in self.itemIterators and self.itemIterators[queue]: finish(self.itemIterators[queue]) self.itemIterators[queue] = None self.itemIterators[queue] = self.runItemQueue(queue) for queue,iterator in self.itemIterators.iteritems(): step(iterator)
def visitBlock(self,block,randomBlock=False): if randomBlock: statement = random.choice(block.statements) call = self.visit(statement) while step(call): yield block.value = statement.value else: for statement in block.statements: call = self.visit(statement) while step(call): yield block.value = block.statements[-1].value
def testStrings(self): from pymud.mob import Mob finish(interpret("""hello there\n""",Mob(commands={},variables={}))) finish(interpret("""hello there line 2 line 3 variable $var """,Mob(commands={},variables={'var':5}))) call = interpret("""hello there line 2 line 3 variable $var """,Mob(commands={},variables={'var':5})) for x in xrange(10): print "%s:" % x step(call)
def sync(self,n=1): if not self.writeBackIterator: self.writeBackIterator = self.partialSync() if not step(self.writeBackIterator,n): self.writeBackIterator = None return True return False
def visitIfStatement(self,node,*args): yield finish(self.visit(node.condition)) if node.condition.value in self.conditions and\ self.conditions[node.condition.value]: call = self.visit(node.script) while step(call): yield
def visitLoopStatement(self,node,*args): yield try: while True: call = self.visit(node.script) while step(call): yield except BreakException,e: pass
def __call__(self,o): result = self.condition(self,o) if result: call = self.action(self,o,result) if isinstance(call,GeneratorType): while step(call): yield else: self.failAction(self,o)
def run(self,tick): Updatable.run(self,tick) try: while len(self.commandQueue) > 0: command = interpret(self.commandQueue.pop(0),self) if step(command): self.scriptsQueue.append(command) if not self.commandScript and len(self.scriptsQueue): self.commandScript = self.scriptsQueue.pop(0) if self.commandScript: if not step(self.commandScript): self.commandScript = None except BreakException: pass except GameException, e: self.sendMessage("exception",error=str(e)) self.runTrigger("failure")
def visitExpressionStatement(self,node,*args): """Handles the command statement case and the print statement case""" yield command = node.expressions[0] finish(self.visit(command)) if command.value in self.commands: for expression in node.expressions[1:]: finish(self.visit(expression)) func = self.commands[command.value] arguments = map(lambda x: x.value,node.expressions[1:]) if hasattr(func,'im_self') and func.im_self: call = func(*arguments) else: call =func(*[self.instance] + arguments) if isinstance(call,GeneratorType): while step(call): yield else: for expression in node.expressions[1:]: finish(self.visit(expression)) self.instance.default(map(lambda x: str(x.value),node.expressions))
def test2(self): def a(rule,o,result): o.x = 0 call = iter(xrange(5)) while step(call): o.x += 1 yield o = Struct() call = SteppableRule(Pass,a)(o) self.assert_(step(call)) self.assertEquals(o.x,1) self.assert_(step(call)) self.assertEquals(o.x,2) self.assert_(step(call)) self.assertEquals(o.x,3) self.assert_(step(call)) self.assertEquals(o.x,4) self.assert_(step(call)) self.assertEquals(o.x,5) self.assertFalse(step(call)) self.assertEquals(o.x,5)
#!/usr/bin/env python from pymud.coroutine import coroutine, step @coroutine def steppable(): for x in xrange(100): yield print x @coroutine def steppable2(): for y in ['a','b','c']: x = steppable() while step(x): yield print y #step(steppable(),50) #step(steppable(),-1) x = steppable2() while step(x): pass
def steppable2(): for y in ['a','b','c']: x = steppable() while step(x): yield print y
def interpret(scriptText,instance): block = script.block.parseString(scriptText) visitor = InterpreterVisitor(instance) call = visitor.walk(block[0]) while step(call):yield
def walk(self,ast): call = self.visit(ast) while step(call): yield
def visitRandomStatement(self,node,*args): yield call = self.visit(node.script,randomBlock=True) while step(call): yield
def visit(self,node,*args,**kwargs): fn = getattr(self, 'visit' + classname(node)) call = fn(node, *args,**kwargs) while step(call): yield
def a(rule,o,result): o.x = 0 call = iter(xrange(5)) while step(call): o.x += 1 yield