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 apply(procedure, arguments, env, cont): """Applies a procedure on a list of arguments.""" if expressions.isPrimitiveProcedure(procedure): return applyPrimitiveProcedure(procedure, arguments, env, cont) elif expressions.isContinuationProcedure(procedure): return applyContinuationProcedure(procedure, arguments) if expressions.isCompoundProcedure(procedure): newEnv = environment.extendEnvironment( expressions.procedureParameters(procedure), arguments, expressions.procedureEnvironment(procedure)) return evalSequence(expressions.procedureBody(procedure), newEnv, cont) raise SchemeError, "Unknown procedure type -- apply " + str(procedure)
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