def __init__(self, key_type, iterable_or_fn, enumerate=True): """Initialize an empty Interpreter. key_type must be a Symbolic subclass. iterable_or_fn must be as for DynamicDict. If iterable_or_fn is a function, it will be applied to the literal key, not the expression. If enumerate is False, keys in this Interpreter will not be considered for path enumeration. """ if not issubclass(key_type, simsym.Symbolic): raise TypeError("key_type must be subclass of Symbolic, not %r" % simsym.strtype(key_type)) self.__key_type = key_type if isinstance(iterable_or_fn, collections.Iterable): it = iter(iterable_or_fn) self.__fn = lambda x: it.next() else: self.__fn = iterable_or_fn if enumerate: self.__realm = self else: self.__realm = simsym.REALM_IGNORE self.__map = {}
def model_unwrap(e): if isinstance(e, z3.FuncDeclRef): return e.name() if isinstance(e, z3.IntNumRef): return int(e.as_long()) if isinstance(e, z3.FuncInterp): return [model_unwrap(x) for x in e.as_list()] if isinstance(e, z3.BoolRef): return (str(e) == 'True') if isinstance(e, list): return [model_unwrap(x) for x in e] raise Exception('%s: unknown value type %s' % (e, simsym.strtype(e)))
def __getitem__(self, key): if not isinstance(key, self.__key_type): raise TypeError("key must be %r instance, not %r" % (self.__key_type.__name__, simsym.strtype(key))) if _is_literal(simsym.unwrap(key)): raise ValueError("key must be non-literal, not %r" % key) lit = key.eval(self.__realm) hlit = z3util.HashableAst(lit) if hlit not in self.__map: if self.__fn is None: raise ValueError("Interpreter has been read; cannot be extended") try: self.__map[hlit] = (key, self.__fn(lit)) except StopIteration: raise ValueError("Ran out of values for %r" % key) return self.__map[hlit][1]
def __getitem__(self, key): if not isinstance(key, self.__key_type): raise TypeError("key must be %r instance, not %r" % (self.__key_type.__name__, simsym.strtype(key))) if _is_literal(simsym.unwrap(key)): raise ValueError("key must be non-literal, not %r" % key) lit = key.eval(self.__realm) hlit = z3util.HashableAst(lit) if hlit not in self.__map: if self.__fn is None: raise ValueError( "Interpreter has been read; cannot be extended") try: self.__map[hlit] = (key, self.__fn(lit)) except StopIteration: raise ValueError("Ran out of values for %r" % key) return self.__map[hlit][1]