def condToIf_Rec(exp): test = exp.car.car action = exp.car.cdr.car if exp.getLength() == 2: l = [sexprs.Symbol('IF'), test, action, exp.cdr.car.cdr.car] return reader.makePair(l, sexprs.Nil()) else: l = [sexprs.Symbol('IF'), test, action, condToIf_Rec(exp.cdr)] return reader.makePair(l, sexprs.Nil())
def letStar_To_Let_recursive(bindings, body): if sexprs.getClass(bindings) == 'Nil': return body else: l = [ sexprs.Symbol('LET'), reader.makePair([bindings.car], sexprs.Nil()), letStar_To_Let_recursive(bindings.cdr, body) ] return reader.makePair(l, sexprs.Nil())
def and_To_If_rec(exp): if sexprs.getClass(exp)=='Nil': l=[sexprs.Symbol('IF'), sexprs.Boolean(True), sexprs.Boolean(True), sexprs.Boolean(True)] return reader.makePair(l,sexprs.Nil()) if exp.getLength()==1: l=[sexprs.Symbol('IF'), exp.car, exp.car, sexprs.Boolean(False)] return reader.makePair(l,sexprs.Nil()) else: l = [sexprs.Symbol('IF'), exp.car, and_To_If_rec(exp.cdr), sexprs.Boolean(False)] return reader.makePair(l,sexprs.Nil())
def makeDefine(exp): if sexprs.getClass(exp.cdr.car)!='Pair': symbol = AbstractSchemeExpr.handleParsed(exp.cdr.car) value = AbstractSchemeExpr.handleParsed(exp.cdr.cdr.car) return Def(symbol, value) else: symbol = exp.cdr.car.car lambda_args = exp.cdr.car.cdr lambda_body = exp.cdr.cdr.car lambda_components = [sexprs.Symbol('LAMBDA'), lambda_args, lambda_body] l= [exp.car, symbol, reader.makePair(lambda_components, sexprs.Nil())] return AbstractSchemeExpr.handleParsed(reader.makePair(l, sexprs.Nil()))
def letrec_To_Yag(exp): lambda_variabls, lambda_values = get_let_vars(exp.cdr.car),get_let_vals(exp.cdr.car) lambda_variabls = sexprs.Pair(gensym(), lambda_variabls) body = exp.cdr.cdr.car l=[reader.makePair([sexprs.Symbol('LAMBDA'), lambda_variabls, body], sexprs.Nil())] while sexprs.getClass(lambda_values)!='Nil': lambda_l = [sexprs.Symbol('LAMBDA'), lambda_variabls, lambda_values.car] l.append(reader.makePair(lambda_l, sexprs.Nil())) lambda_values = lambda_values.cdr final_l = [sexprs.Symbol('Yag')]+l return AbstractSchemeExpr.handleParsed(reader.makePair(final_l, sexprs.Nil()))
def makeDefine(exp): if sexprs.getClass(exp.cdr.car) != 'Pair': symbol = AbstractSchemeExpr.handleParsed(exp.cdr.car) value = AbstractSchemeExpr.handleParsed(exp.cdr.cdr.car) return Def(symbol, value) else: symbol = exp.cdr.car.car lambda_args = exp.cdr.car.cdr lambda_body = exp.cdr.cdr.car lambda_components = [sexprs.Symbol('LAMBDA'), lambda_args, lambda_body] l = [exp.car, symbol, reader.makePair(lambda_components, sexprs.Nil())] return AbstractSchemeExpr.handleParsed(reader.makePair( l, sexprs.Nil()))
def and_To_If_rec(exp): if sexprs.getClass(exp) == 'Nil': l = [ sexprs.Symbol('IF'), sexprs.Boolean(True), sexprs.Boolean(True), sexprs.Boolean(True) ] return reader.makePair(l, sexprs.Nil()) if exp.getLength() == 1: l = [sexprs.Symbol('IF'), exp.car, exp.car, sexprs.Boolean(False)] return reader.makePair(l, sexprs.Nil()) else: l = [ sexprs.Symbol('IF'), exp.car, and_To_If_rec(exp.cdr), sexprs.Boolean(False) ] return reader.makePair(l, sexprs.Nil())
def letrec_To_Yag(exp): lambda_variabls, lambda_values = get_let_vars(exp.cdr.car), get_let_vals( exp.cdr.car) lambda_variabls = sexprs.Pair(gensym(), lambda_variabls) body = exp.cdr.cdr.car l = [ reader.makePair([sexprs.Symbol('LAMBDA'), lambda_variabls, body], sexprs.Nil()) ] while sexprs.getClass(lambda_values) != 'Nil': lambda_l = [ sexprs.Symbol('LAMBDA'), lambda_variabls, lambda_values.car ] l.append(reader.makePair(lambda_l, sexprs.Nil())) lambda_values = lambda_values.cdr final_l = [sexprs.Symbol('Yag')] + l return AbstractSchemeExpr.handleParsed( reader.makePair(final_l, sexprs.Nil()))
def quasiquote_To_Quote_rec(exp): if isUnquote(exp): return exp.cdr.car elif isUnquoteSplicing(exp): raise IllegalQQuoteLocation() elif sexprs.getClass(exp)=='Pair': first = exp.car rest = exp.cdr if isUnquoteSplicing(first): return reader.makePair([sexprs.Symbol('APPEND'), first.cdr.car, quasiquote_To_Quote_rec(rest)], sexprs.Nil()) elif isUnquoteSplicing(rest): return reader.makePair([sexprs.Symbol('CONS'), quasiquote_To_Quote_rec(first), rest.cdr.car], sexprs.Nil()) else: return reader.makePair([sexprs.Symbol('CONS'), quasiquote_To_Quote_rec(first), quasiquote_To_Quote_rec(rest)], sexprs.Nil()) elif sexprs.getClass(exp)=='Vector': l=[sexprs.Symbol('LIST->VECTOR'), quasiquote_To_Quote_rec(reader.makePair([sexprs.Symbol('VECTOR->LIST'), exp], sexprs.Nil()))] return reader.makePair(l, sexprs.Nil()) elif sexprs.getClass(exp)=='Symbol' or sexprs.getClass(exp)=='Nil': l = [sexprs.Symbol('quote'), exp] return reader.makePair(l, sexprs.Nil()) else: return exp
def quasiquote_To_Quote_rec(exp): if isUnquote(exp): return exp.cdr.car elif isUnquoteSplicing(exp): raise IllegalQQuoteLocation() elif sexprs.getClass(exp) == 'Pair': first = exp.car rest = exp.cdr if isUnquoteSplicing(first): return reader.makePair([ sexprs.Symbol('APPEND'), first.cdr.car, quasiquote_To_Quote_rec(rest) ], sexprs.Nil()) elif isUnquoteSplicing(rest): return reader.makePair([ sexprs.Symbol('CONS'), quasiquote_To_Quote_rec(first), rest.cdr.car ], sexprs.Nil()) else: return reader.makePair([ sexprs.Symbol('CONS'), quasiquote_To_Quote_rec(first), quasiquote_To_Quote_rec(rest) ], sexprs.Nil()) elif sexprs.getClass(exp) == 'Vector': l = [ sexprs.Symbol('LIST->VECTOR'), quasiquote_To_Quote_rec( reader.makePair([sexprs.Symbol('VECTOR->LIST'), exp], sexprs.Nil())) ] return reader.makePair(l, sexprs.Nil()) elif sexprs.getClass(exp) == 'Symbol' or sexprs.getClass(exp) == 'Nil': l = [sexprs.Symbol('quote'), exp] return reader.makePair(l, sexprs.Nil()) else: return exp
def letStar_To_Let_recursive(bindings, body): if sexprs.getClass(bindings)=='Nil': return body else: l = [sexprs.Symbol('LET'), reader.makePair([bindings.car], sexprs.Nil()), letStar_To_Let_recursive(bindings.cdr,body)] return reader.makePair(l,sexprs.Nil())