def isAList(lst): """\ Check whether the pyscheme list lst is an associate list """ while not pair.isNull(lst): if not pair.isPair(lst): return False if not pair.isPair(pair.car(lst)): return False lst = pair.cdr(lst) return True
def type(self, val): "Get the Python type of a Scheme value." if expressions.isCompoundProcedure(val): return schemepy.types.Lambda if expressions.isPrimitiveProcedure(val): fun = expressions.primitiveImplementation(val) orig = fun.__dict__.get("_orig", None) if orig and callable(orig): return types.FunctionType return schemepy.types.Lambda if symbol.isSymbol(val): if val == symbol.true or val == symbol.false: return bool return schemepy.types.Symbol if pair.isNull(val): return list if isAList(val): return dict if pair.isList(val): return list if pair.isPair(val): return schemepy.types.Cons t = type(val) if t not in (int, complex, float, long, str, unicode): return object return t
def t_expressionToString(expr, cont): """Helper function for toString: written in CPS/trampolined form to avoid growing control context.""" if id(expr) in seenExpressionIds: return pogo.bounce(cont, "[...]") if pair.isNull(expr): return pogo.bounce(cont, "()") if not pair.isPair(expr): return t_atomExpressionToString(expr, cont) elif isPrimitiveProcedure(expr) or isCompoundProcedure(expr): return t_procedureExpressionToString(expr, cont) else: return t_pairExpressionToString(expr, cont)
def fromscheme(self, val, shallow=False): "Convert a Scheme value to a Python value." if expressions.isCompoundProcedure(val): return schemepy.types.Lambda(val, self, shallow) if expressions.isPrimitiveProcedure(val): fun = expressions.primitiveImplementation(val) orig = fun.__dict__.get("_orig", None) if orig and callable(orig): return orig return schemepy.types.Lambda(val, self, shallow) if symbol.isSymbol(val): if val == symbol.true: return True if val == symbol.false: return False return schemepy.types.Symbol(str(val)) if pair.isNull(val): return [] if isAList(val): dic = {} while not pair.isNull(val): el = pair.car(val) key = self.fromscheme(pair.car(el)) value = pair.cdr(el) if not shallow: value = self.fromscheme(value) dic[key] = value val = pair.cdr(val) return dic if pair.isList(val): lst = [] while not pair.isNull(val): el = pair.car(val) if not shallow: el = self.fromscheme(el) lst.append(el) val = pair.cdr(val) return lst if pair.isPair(val): car = pair.car(val) cdr = pair.cdr(val) if not shallow: car = self.fromscheme(car) cdr = self.fromscheme(cdr) return schemepy.types.Cons(car, cdr) return val
def makeFrame(var_pairs, val_pairs): """Note: here I diverge from SICP's implementation: instead of using a cons, I use a Python list. The selectors frameVariables() and frameValues() reflect this.""" car, cdr, isNull = pair.car, pair.cdr, pair.isNull frame = {} while not isNull(var_pairs): if pair.isPair(var_pairs): if not isNull(val_pairs): frame[car(var_pairs)] = car(val_pairs) var_pairs, val_pairs = cdr(var_pairs), cdr(val_pairs) else: raise SchemeError, "Too few arguments supplied" else: frame[var_pairs] = val_pairs var_pairs, val_pairs = pair.NIL, pair.NIL if isNull(val_pairs): return frame else: raise SchemeError, "Too many arguments supplied"
def schemePairQuestion(L): return schemeBooleanize(pair.isPair(L))
def isLoopyPair(expr): return (pair.isPair(expr) and id(pair.cdr(expr)) in seenExpressionIds)
def get_keyword_tag(self, expr): """Tries to return the keyword. If no keyword exists, returns None.""" if pair.isPair(expr) and symbol.isSymbol(pair.car(expr)): return str(pair.car(expr)).lower() return None