Example #1
0
 def execute(self, stmt, context={}):
     if isinstance(stmt, Statement):
         replace = stmt.replace(context, self.defs)
         if replace:
             new_stmt, new_context = replace
             context = updated_context(context, new_context)
             return self.execute(new_stmt, context)
         
         self.add_stmt(stmt)
                     
     elif isinstance(stmt, Command):
         replace = stmt.replace(context, self.defs)
         if replace:
             new_stmt, new_context = replace
             context = updated_context(context, new_context)
             return self.execute(new_stmt, context)
     
         stmt.execute(self, context)
         
     elif hasattr(stmt, '__iter__'):
         for substmt in stmt:
             self.execute(substmt, context)
         return
         
     else:
         raise BeaverException('Unrecognized statement: %s' % stmt)
Example #2
0
 def execute(self, graph, context={}):
     if graph.verbose: print str(self)
     f, args = self.f, self.args.vars
     
     for n, arg in enumerate(args):
         if isinstance(arg, Variable):
             result, new_match = def_match(arg, (context, graph.defs))
             if result:
                 self.args.vars[n] = new_match
                 return self.execute(graph, context)     
         
     for varset in (context, graph.defs):
         if f in varset:
             defs = varset[f]
             for (pattern, definition) in defs:
                 if pattern is None: continue
                 need_to_match = pattern.vars
                 if len(need_to_match) == len(args):
                     if all(match(arg, m) for arg, m in zip(args, need_to_match)):
                         # a match was found; return a function call with a new context
                         new_context = {}
                         
                         for arg, m in zip(args, need_to_match):
                             if isinstance(m, Variable):
                                 new_context[m] = [(None, arg)]
                                 
                         new_context = updated_context(context, new_context)
                         return graph.execute(copy(definition), new_context)
     
     # no match
     if len(args) == 2: graph.execute(Statement(f, [(args[0], [args[1]])]), context)
     else: raise BeaverException('Undefined function: %s %s' % (f, self.args))
Example #3
0
 def execute(self, graph, context={}):
     if graph.verbose: print str(self)
     for var in self.sequence.vars:
         expr = [copy(x) for x in self.expression]
         iter_context = {self.ident: [(None, var)]}
         new_context = updated_context(context, iter_context)
         graph.execute(expr, new_context)