def expandClauses(clauses): if pair.isNull(clauses): return false first = pair.car(clauses) rest = pair.cdr(clauses) if isCondElseClause(first): if pair.isNull(rest): return sequenceToExp(condActions(first)) raise SchemeError, "else clause isn't last -- condToIf" + clauses return makeIf(condPredicate(first), sequenceToExp(condActions(first)), expandClauses(rest))
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 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 analyzeSequence(exps): def sequentially(analyzedFirst, analyzedSecond): def c(env, cont): def c_first_exec(ignoredVal): return pogo.bounce(analyzedSecond, env, cont) return pogo.bounce(analyzedFirst, env, c_first_exec) return c if pair.isNull(exps): raise SchemeError, "Empty sequence -- ANALYZE" analyzedSeqs = analyze(expressions.firstExp(exps)) exps = expressions.restExps(exps) while not pair.isNull(exps): analyzedSeqs = sequentially(analyzedSeqs, analyze(expressions.firstExp(exps))) exps = expressions.restExps(exps) return analyzedSeqs
def execRands(rands, env, cont): """Executes each operand and constructs a new list.""" def eval_first_cont(headVal): def eval_rest_cont(tailVal): return pogo.bounce(cont, pair.cons(headVal, tailVal)) return execRands(expressions.restOperands(rands), env, eval_rest_cont) if pair.isNull(rands): return pogo.bounce(cont, pair.NIL) return texec(expressions.firstOperand(rands), env, eval_first_cont)
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 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 loop(loop_exp, loop_cont): if pair.isNull(loop_exp): return pogo.bounce(loop_cont, pieces) elif pair.isDottedPair(loop_exp) or isLoopyPair(loop_exp): def c_car(carString): def c_cdr(cdrString): pieces.append(carString) pieces.append(".") pieces.append(cdrString) return pogo.bounce(loop_cont, pieces) return t_expressionToString(pair.cdr(loop_exp), c_cdr) return t_expressionToString(pair.car(loop_exp), c_car) else: def c_car(carString): pieces.append(carString) return pogo.bounce(loop, pair.cdr(loop_exp), loop_cont) return pogo.bounce(t_expressionToString, pair.car(loop_exp), c_car)
def schemeNullQuestion(a): return schemeBooleanize(pair.isNull(a)) def schemeDisplay(thing):
def markExprAsSeen(expr): if not pair.isNull(expr): seenExpressionIds.add(id(expr))
def ifAlternative(exp): if not pair.isNull(pair.cdddr(exp)): return pair.cadddr(exp) return false
def isNoOperands(ops): return pair.isNull(ops)
def sequenceToExp(seq): if pair.isNull(seq): return seq if isLastExp(seq): return firstExp(seq) return makeBegin(seq)
def isLastExp(seq): return pair.isNull(pair.cdr(seq))
def isSelfEvaluating(exp): """Returns True if the expression is self-evaluating.""" return isNumber(exp) or isString(exp) or pair.isNull(exp)
def letBindingValues(bindings): if pair.isNull(bindings): return pair.list() return pair.cons(pair.cadr(pair.car(bindings)), letBindingValues(pair.cdr(bindings)))