def QuoteGen(prefix, name): return ps.parser(pc.pcWord(prefix)) \ .pack(removeCatenStar) \ .delayed_parser(lambda: pSexpr) \ .caten() \ .pack(lambda m: sexprs.Pair(sexprs.Symbol(name), sexprs.Pair(m[1], sexprs.Nil()))) \ .done()
def andToIf(s_exp): if isinstance(s_exp,sexprs.Nil): return sexprs.true() if isinstance(s_exp,sexprs.Pair): if isinstance(s_exp.second,sexprs.Nil): return s_exp.first return sexprs.Pair(sexprs.Symbol("IF"),sexprs.Pair(s_exp.first,sexprs.Pair( sexprs.Pair(sexprs.Symbol("AND"),s_exp.second) ,sexprs.Pair(sexprs.false(),sexprs.Nil()))))
def __init__(self,s_exp): if isinstance(s_exp,sexprs.Pair): if isinstance(s_exp.first,sexprs.Pair): self.var=AbstractSchemeExpr.parse_after_reader(s_exp.first.first) rest=sexprs.Pair(s_exp.first.second,s_exp.second) rest=sexprs.Pair(sexprs.Symbol('LAMBDA'),rest) self.body=AbstractSchemeExpr.parse_after_reader(rest) else: self.var=AbstractSchemeExpr.parse_after_reader(s_exp.first) self.body=AbstractSchemeExpr.parse_after_reader(s_exp.second.first)
def let_To_Application(exp): lambde_variabls, lambde_values = get_let_vars(exp.cdr.car), get_let_vals( exp.cdr.car) lambda_body = exp.cdr.cdr.car return AbstractSchemeExpr.handleParsed( sexprs.Pair( sexprs.Pair( sexprs.Symbol('LAMBDA'), sexprs.Pair(lambde_variabls, sexprs.Pair(lambda_body, sexprs.Nil()))), lambde_values))
def condToIf (s_exp): if isinstance(s_exp,sexprs.Pair): if isinstance(s_exp.second, sexprs.Nil): if isinstance(s_exp.first.first,sexprs.Symbol)and s_exp.first.first.string=="ELSE": return s_exp.first.second.first return sexprs.Pair(sexprs.Symbol("IF"), sexprs.Pair(s_exp.first.first, sexprs.Pair(s_exp.first.second.first, sexprs.Nil()))) return sexprs.Pair(sexprs.Symbol("IF"),sexprs.Pair(s_exp.first.first,sexprs.Pair(s_exp.first.second.first,sexprs.Pair( sexprs.Pair(sexprs.Symbol("COND"),s_exp.second) ,sexprs.Nil()))))
def letStarToApplic(s_exp): if isinstance(s_exp, sexprs.Pair) and isinstance(s_exp.second, sexprs.Pair): body = s_exp.second.first if isinstance(s_exp.first, sexprs.Nil): return body if isinstance(s_exp.first, sexprs.Pair): param = s_exp.first.first.first value = s_exp.first.first.second.first rest = s_exp.first.second return sexprs.Pair( sexprs.Pair(sexprs.Symbol("LAMBDA"), sexprs.Pair( sexprs.Pair(param, sexprs.Nil()) , sexprs.Pair( sexprs.Pair(sexprs.Symbol("LET*"), sexprs.Pair( rest , sexprs.Pair( body , sexprs.Nil()))) , sexprs.Nil()))) , sexprs.Pair( value , 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 letToApplic(s_exp): param=[] values=[] temp=s_exp.first while isinstance(temp,sexprs.Pair): param.append(temp.first.first) values.append(temp.first.second.first) temp=temp.second body=s_exp.second.first s_param=sexprs.Nil() for x in reversed (param): s_param=sexprs.Pair(x,s_param) s_values=sexprs.Nil() for y in reversed(values): s_values=sexprs.Pair(y,s_values) return sexprs.Pair( sexprs.Pair(sexprs.Symbol('LAMBDA'), sexprs.Pair(s_param, sexprs.Pair(body, sexprs.Nil()))), s_values )
def list_to_pair(s, a): if s: return sexprs.Pair(s[0], list_to_pair(s[1:], a)) else: return a
.disj() \ .done() vector_p = ps.parser(pc.const(lambda x: x=='#')) \ .parser(pc.const(lambda x: x=='(')) \ .delayed_parser(lambda: pSexpr) \ .star() \ .parser(pc.const(lambda x: x==')')) \ .catens(4) \ .pack(lambda m: sexprs.Vector(m[2])) \ .done() quoted_p = ps.parser(pc.const(lambda x: x=="'")) \ .delayed_parser(lambda: pSexpr) \ .caten() \ .pack(lambda m: sexprs.Pair(sexprs.Symbol("quote"),sexprs.Pair(m[1],sexprs.Nil()))) \ .done() qquoted_p = ps.parser(pc.const(lambda x: x=="`")) \ .delayed_parser(lambda: pSexpr) \ .caten() \ .pack(lambda m: sexprs.Pair(sexprs.Symbol("quasiquote"),sexprs.Pair(m[1],sexprs.Nil()))) \ .done() unquoted_spliced_p = ps.parser(pc.pcWord(",@")) \ .delayed_parser(lambda: pSexpr) \ .caten() \ .pack(lambda m: sexprs.Pair(sexprs.Symbol("unquote-splicing"),sexprs.Pair(m[1],sexprs.Nil()))) \ .done() unquoted_p = ps.parser(pc.const(lambda x: x==",")) \
def get_let_vals(exp): if sexprs.getClass(exp) == 'Nil': return sexprs.Nil() else: return sexprs.Pair(exp.car.cdr.car, get_let_vals(exp.cdr))
def makePair(car,cdr): if car: return classes.Pair(car[0],makePair(car[1:],cdr)) else: return cdr
def vectorToList(s_exp): elem=s_exp.elements if len(elem)==0: return sexprs.Nil() return sexprs.Pair(elem[0],vectorToList(elem[1:]))
def QQ_expend(s_exp): if isinstance(s_exp,sexprs.Pair): if isinstance(s_exp.first,sexprs.Symbol) and isinstance(s_exp.second,sexprs.Pair): if s_exp.first.string=="unquote": return s_exp.second.first elif s_exp.first.string=="unquote-splicing": raise (Exception("unquote-splicing here makes no sense!")) a=s_exp.first b=s_exp.second if isinstance(a,sexprs.Pair) and isinstance(a.first,sexprs.Symbol) and a.first.string=="unquote-splicing": if isinstance(b, sexprs.Nil): return a.second.first return sexprs.Pair(sexprs.Symbol('APPEND'), sexprs.Pair(a.second.first , sexprs.Pair(QQ_expend(b), sexprs.Nil()))) elif isinstance(b,sexprs.Pair) and isinstance(b.first,sexprs.Symbol) and b.first.string=="unquote-splicing": return sexprs.Pair(sexprs.Symbol('CONS'), sexprs.Pair(QQ_expend(a) , sexprs.Pair(b.second.first, sexprs.Nil()))) return sexprs.Pair(sexprs.Symbol('CONS'), sexprs.Pair(QQ_expend(a) , sexprs.Pair(QQ_expend(b), sexprs.Nil()))) elif isinstance(s_exp,sexprs.Vector): return sexprs.Pair(sexprs.Symbol("LIST->VECTOR") , sexprs.Pair(QQ_expend(vectorToList(s_exp)),sexprs.Nil())) elif isinstance(s_exp,sexprs.Nil) or isinstance(s_exp,sexprs.Symbol): return sexprs.Pair(sexprs.Symbol("quote"), sexprs.Pair(s_exp,sexprs.Nil())) return s_exp
def letToYagApplic(s_exp): body=s_exp.second.first if isinstance(s_exp.first,sexprs.Nil): return body generatedSymbol=genSym.newGenSym() param=[]; values=[]; temp=s_exp.first while isinstance(temp,sexprs.Pair): param.append(temp.first.first) values.append(temp.first.second.first) temp=temp.second s_param=sexprs.Nil() for x in reversed (param): s_param=sexprs.Pair(x,s_param) s_param=sexprs.Pair(generatedSymbol,s_param) s_values=[] for y in values: s_values.append(y) firstLambda=sexprs.Pair( sexprs.Symbol("LAMBDA"), sexprs.Pair( s_param, sexprs.Pair( body, sexprs.Nil() ) ) ) list_of_lambdas=[] for x in s_values: list_of_lambdas.append( sexprs.Pair( sexprs.Symbol("LAMBDA"), sexprs.Pair( s_param, sexprs.Pair( x, sexprs.Nil( ) ))) ) pairedListofLambdas=sexprs.Nil() for x in reversed(list_of_lambdas): pairedListofLambdas=sexprs.Pair(x,pairedListofLambdas) pairedListofLambdas=sexprs.Pair(firstLambda,pairedListofLambdas) return sexprs.Pair( sexprs.Symbol("YAG"), pairedListofLambdas )
def ListToPair(myList, endOfList): if myList: return sexprs.Pair(myList[0], ListToPair(myList[1:], endOfList)) else: return endOfList
pQuoteSign = ps.const(lambda c: c=='\'')\ .const(lambda c: c=='`')\ .word(',@')\ .const(lambda c: c==',')\ .disjs(4)\ .pack(lambda s: classes.Symbol({ '`':'quasiquote',\ ',':'unquote',\ '\'':'quote',\ ',@':'unquote-splicing'}["".join(s)]))\ .done() pQuoted = ps.parser(pQuoteSign)\ .delayed_parser(lambda: pSexpr)\ .caten()\ .pack(lambda s: classes.Pair(s[0], classes.Pair(s[1],classes.Nil())))\ .done() pSexpr = ps.parser(pSkip)\ .parser(pNum)\ .parser(boolean)\ .parser(pPair)\ .parser(pNil)\ .parser(pChar)\ .parser(pVector)\ .parser(pString)\ .parser(pQuoted)\ .parser(symbol)\ .disjs(9)\ .parser(pSkip)\ .catens(3)\