def eval1 (term): if isinstance (term, Recordable): sleepy._debug ( "Recordable", term.__dict__['_sl_data']) return "ValueWrapper (x, vnames)" + eval1 (term.__dict__['_sl_data']) elif isinstance (term, sleepy.collection.Sequential): return "".join (self.optimise (term).map (eval1)) elif isinstance (term, CallTerm): return "(*[%s])" % ", ".join (eval1 (term.args).map (str)) elif isinstance (term, CallFuncTerm): sleepy._debug ("CallFunc", eval1 (term.args)) return "._sl_call ('%s', [%s])" % (term.func, ", ".join (eval1 (term.args).map (str))) elif isinstance (term, CallUserDefinedFuncTerm): return "._sl_call_ud (%s, [%s])" % (p (term.func), ", ".join (eval1 (term.args).map (str))) elif isinstance (term, CmpTerm): return "._sl_cmp (%s)" % eval1 (term.o) elif isinstance (term, GetAttrTerm): return "._sl_getattr ('%s')" % term.name elif isinstance (term, IfTerm): return "._sl_if (%s, x, %s, %s, %s)" % (p (self.eval), eval1 (term.cond), p (term.then), p (term._else)) elif isinstance (term, IntervalTerm): return "._sl_interval (%s, %s, '%s')" % (eval1 (term.a), eval1 (term.b), term.type) elif isinstance (term, LogOpTerm): return "._sl_logop (%s, x, '%s', %s)" % (p (self.eval), term.type, p (term.xs)) elif isinstance (term, Value): return eval1 (term.value) if isinstance (term.value, Recordable) else \ p (term.value) elif isinstance (term, ValueList): return term.values.map (eval1) else: raise ValueError (term) pass
def _sl_logop (self, eval1, input, typ, xs): sleepy._debug ("_sl_logop", typ, xs) def eval2 (x): return eval1 (sleepy.collection.List (x), -1, []) (*input) if type (input) is tuple else \ eval1 (sleepy.collection.List (x), 1, []) (input) if typ == "and": ret = True for x in xs: if not eval2 (x): ret = False break pass pass elif typ == "or": ret = False for x in xs: if eval2 (x): ret = True break pass pass elif typ == "xor": a = eval2 (xs[0]) b = eval2 (xs[1]) ret = ((a is True) and (b is False)) or ((a is False) and (b is True)) elif typ == "not": ret = not eval2 (xs[0]) else: raise ValueError return ret
def eval (self, terms, nargs, vnames): def eval1 (term): if isinstance (term, Recordable): sleepy._debug ( "Recordable", term.__dict__['_sl_data']) return "ValueWrapper (x, vnames)" + eval1 (term.__dict__['_sl_data']) elif isinstance (term, sleepy.collection.Sequential): return "".join (self.optimise (term).map (eval1)) elif isinstance (term, CallTerm): return "(*[%s])" % ", ".join (eval1 (term.args).map (str)) elif isinstance (term, CallFuncTerm): sleepy._debug ("CallFunc", eval1 (term.args)) return "._sl_call ('%s', [%s])" % (term.func, ", ".join (eval1 (term.args).map (str))) elif isinstance (term, CallUserDefinedFuncTerm): return "._sl_call_ud (%s, [%s])" % (p (term.func), ", ".join (eval1 (term.args).map (str))) elif isinstance (term, CmpTerm): return "._sl_cmp (%s)" % eval1 (term.o) elif isinstance (term, GetAttrTerm): return "._sl_getattr ('%s')" % term.name elif isinstance (term, IfTerm): return "._sl_if (%s, x, %s, %s, %s)" % (p (self.eval), eval1 (term.cond), p (term.then), p (term._else)) elif isinstance (term, IntervalTerm): return "._sl_interval (%s, %s, '%s')" % (eval1 (term.a), eval1 (term.b), term.type) elif isinstance (term, LogOpTerm): return "._sl_logop (%s, x, '%s', %s)" % (p (self.eval), term.type, p (term.xs)) elif isinstance (term, Value): return eval1 (term.value) if isinstance (term.value, Recordable) else \ p (term.value) elif isinstance (term, ValueList): return term.values.map (eval1) else: raise ValueError (term) pass def strip (x): return x.value if isinstance (x, ValueWrapper) else x p = Params ("__sl_p") args = "x" if nargs == 1 else "*x" s = ("lambda __sl_p, vnames, _sl_strip: lambda %s: _sl_strip (" % args) + eval1 (terms) + ")" sleepy._debug (s) sleepy._debug (p) return eval (s) (p, vnames, strip)
def _sl_if (self, eval1, x, cond, then, _else): sleepy._debug ("_sl_if", eval1, x, cond, then, _else) result = then if cond else _else val = eval1 (sleepy.collection.List (result), -1, []) (*x) if type (x) is tuple else \ eval1 (sleepy.collection.List (result), 1, []) (x) return ValueWrapper (val)