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 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 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 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 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 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 isTaggedList(exp, tag): """Returns true if the expression is tagged with the 'tag'.""" if pair.isList(exp) and pair.length(exp) > 0: return pair.car(exp) == tag return 0
def letBindingValues(bindings): if pair.isNull(bindings): return pair.list() return pair.cons(pair.cadr(pair.car(bindings)), letBindingValues(pair.cdr(bindings)))
def condPredicate(clause): return pair.car(clause)
def firstExp(seq): return pair.car(seq)
def operator(exp): if pair.length(exp) == 0: raise SchemeError, \ "No operator given for procedure application -- OPERATOR" return pair.car(exp)
def firstOperand(ops): return pair.car(ops)
def definitionVariable(exp): if isSymbol(pair.cadr(exp)): return pair.cadr(exp) return pair.car(pair.cadr(exp))
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