def OR_handler(exp): """We have to consider three cases: (OR) ===> #t (OR e1) ===> e1 (OR e1 e2 ...) ===> (let ((temp-val e1)) (IF temp-val temp-val (OR e2 ...))) ===> ((lambda (temp-val) (IF temp-val temp-val (OR e2 ...))) e1) """ clauses = and_clauses(exp) if pair.length(clauses) == 0: return symbol.true elif pair.length(clauses) == 1: return pair.car(clauses) else: temporarySymbol = symbol.makeUniqueTemporary() lambdaVal = expressions.makeLambda( pair.list(temporarySymbol), pair.list(expressions.makeIf(temporarySymbol, temporarySymbol, makeOr(pair.cdr(clauses))))) return expressions.makeApplication( lambdaVal, pair.list(pair.car(clauses)))
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)
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 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 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 AND_handler(exp): """We have to consider three cases: (AND) ===> #f (AND e1) ===> e1 (AND e1 e2 ...) ===> (IF e1 (AND e2 ...) #f) """ clauses = and_clauses(exp) if pair.length(clauses) == 0: return symbol.false elif pair.length(clauses) == 1: return pair.car(clauses) else: return expressions.makeIf(pair.car(clauses), makeAnd(pair.cdr(clauses)), symbol.false)
def definitionValue(exp): if isSymbol(pair.cadr(exp)): return pair.caddr(exp) return makeLambda(pair.cdr(pair.cadr(exp)), pair.cddr(exp))
def beginActions(exp): return pair.cdr(exp)
def letBindingValues(bindings): if pair.isNull(bindings): return pair.list() return pair.cons(pair.cadr(pair.car(bindings)), letBindingValues(pair.cdr(bindings)))
def letBody(exp): return pair.cdr(pair.cdr(exp))
def condClauses(exp): return pair.cdr(exp)
def isLoopyPair(expr): return (pair.isPair(expr) and id(pair.cdr(expr)) in seenExpressionIds)
def operands(exp): return pair.cdr(exp)
def restExps(seq): return pair.cdr(seq)
def restOperands(ops): return pair.cdr(ops)
def c_car(carString): pieces.append(carString) return pogo.bounce(loop, pair.cdr(loop_exp), loop_cont)
def isLastExp(seq): return pair.isNull(pair.cdr(seq))
def condActions(clause): return pair.cdr(clause)
def and_clauses(exp): return pair.cdr(exp)