def append(*lists): """Appends all lists together.""" appended_list = lists[-1] for i in xrange(len(lists) - 2, -1, -1): next_list = lists[i] appended_list = pogo.pogo( c_appendTwo(next_list, appended_list, pogo.land)) return appended_list
def testContinuationMapping(self): def c_square(x, cont): return pogo.bounce(cont, x**2) self.assertEquals(cons(4, 9), pogo.pogo(c_listMap(c_square, cons(2, 3), pogo.land, allow_improper_lists=True))) self.assertRaises(SchemeError, pogo.pogo, pogo.bounce(c_listMap, c_square, cons(2, 3), pogo.land)) self.assertEquals(list(1, 4, 9, 16), pogo.pogo(c_listMap(c_square, list(1, 2, 3, 4), pogo.land)))
def append(*lists): """Appends all lists together.""" appended_list = lists[-1] for i in xrange(len(lists)-2, -1, -1): next_list = lists[i] appended_list = pogo.pogo( c_appendTwo(next_list, appended_list, pogo.land)) return appended_list
def testContinuationMapping(self): def c_square(x, cont): return pogo.bounce(cont, x**2) self.assertEquals( cons(4, 9), pogo.pogo( c_listMap(c_square, cons(2, 3), pogo.land, allow_improper_lists=True))) self.assertRaises( SchemeError, pogo.pogo, pogo.bounce(c_listMap, c_square, cons(2, 3), pogo.land)) self.assertEquals( list(1, 4, 9, 16), pogo.pogo(c_listMap(c_square, list(1, 2, 3, 4), pogo.land)))
def apply(self, proc, args): """Call the scheme procedure""" # NOTE: This is specific to AnalyzerInterpreter (and MinimalInterpreter) # which we used. But RegularInterpreter should use evaluator.apply instead. try: return pogo.pogo( analyzer.apply(proc, self.toscheme(args, shallow=True), self._interp.get_environment(), pogo.land)) except Exception, e: self._parse_error(e)
def apply(self, proc, args): """Call the scheme procedure""" # NOTE: This is specific to AnalyzerInterpreter (and MinimalInterpreter) # which we used. But RegularInterpreter should use evaluator.apply instead. try: return pogo.pogo(analyzer.apply(proc, self.toscheme(args, shallow=True), self._interp.get_environment(), pogo.land)) except Exception, e: self._parse_error(e)
def parse(s): """Parse a single string. This is just a convenience function.""" return pogo.pogo(parseSingleExpression(tokenize(s), identity_cont))
def eval(exp, env): return pogo.pogo(teval(exp, env, pogo.land))
def toString(expr, quoteStrings=1): """Given a Scheme expression, returns a string that tries to nicely return it as a string. If quoteStrings is true, puts quotes around string expressions. Notes: this is a little tricky just because we have to account for loopy structures. We keep a set of expressions that we've already seen, to make sure we don't retrace any steps. """ seenExpressionIds = Set() def isLoopyPair(expr): return (pair.isPair(expr) and id(pair.cdr(expr)) in seenExpressionIds) def markExprAsSeen(expr): if not pair.isNull(expr): seenExpressionIds.add(id(expr)) 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 t_atomExpressionToString(expr, cont): """Converts an atomic expression (string, number) to a string. Written in CPS/trampolined form.""" if type(expr) is str and quoteStrings: return pogo.bounce(cont, "\"%s\"" % expr.replace('"', '\\"')) else: return pogo.bounce(cont, str(expr)) def t_procedureExpressionToString(expr, cont): """Converts a procedure expression to a string.""" if isPrimitiveProcedure(expr): return pogo.bounce( cont, ('(%s %s <procedure-env>)') % (Symbol('primitive-procedure'), primitiveImplementation(expr))) elif isCompoundProcedure(expr): def c_parameters(parameterString): def c_body(bodyString): return pogo.bounce( cont, '(%s %s %s <procedure-env>)' % (Symbol('compound-procedure'), parameterString, bodyString)) return t_expressionToString(procedureBody(expr), c_body) return t_expressionToString(procedureParameters(expr), c_parameters) def t_pairExpressionToString(expr, cont): """Converts a pair expression to a string.""" pieces = [] 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) markExprAsSeen(expr) return loop( expr, lambda pieces: pogo.bounce(cont, '(' + ' '.join(pieces) + ')')) return pogo.pogo(t_expressionToString(expr, pogo.land))
def eval(exp, env): """This version of eval calls analyze, and then texec()s it against the environment.""" analyzedExp = analyze(exp) return pogo.pogo(texec(analyzedExp, env, pogo.land))
def expand(self, expr): """Given an expression, tries to expand it and its subexpressions into core forms.""" return pogo.pogo(self.t_expand(expr, pogo.land))
def testAppendTwo(self): self.assertEquals( list(1, 2, 3, 4), pogo.pogo(c_appendTwo(list(1, 2), list(3, 4), pogo.land)))
def testAppendTwo(self): self.assertEquals(list(1, 2, 3, 4), pogo.pogo(c_appendTwo(list(1, 2), list(3, 4), pogo.land)))
def toString(expr, quoteStrings=1): """Given a Scheme expression, returns a string that tries to nicely return it as a string. If quoteStrings is true, puts quotes around string expressions. Notes: this is a little tricky just because we have to account for loopy structures. We keep a set of expressions that we've already seen, to make sure we don't retrace any steps. """ seenExpressionIds = Set() def isLoopyPair(expr): return (pair.isPair(expr) and id(pair.cdr(expr)) in seenExpressionIds) def markExprAsSeen(expr): if not pair.isNull(expr): seenExpressionIds.add(id(expr)) 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 t_atomExpressionToString(expr, cont): """Converts an atomic expression (string, number) to a string. Written in CPS/trampolined form.""" if type(expr) is str and quoteStrings: return pogo.bounce(cont, "\"%s\"" % expr.replace('"', '\\"')) else: return pogo.bounce(cont, str(expr)) def t_procedureExpressionToString(expr, cont): """Converts a procedure expression to a string.""" if isPrimitiveProcedure(expr): return pogo.bounce(cont, ('(%s %s <procedure-env>)') % ( Symbol('primitive-procedure'), primitiveImplementation(expr))) elif isCompoundProcedure(expr): def c_parameters(parameterString): def c_body(bodyString): return pogo.bounce(cont, '(%s %s %s <procedure-env>)' % (Symbol('compound-procedure'), parameterString, bodyString)) return t_expressionToString(procedureBody(expr), c_body) return t_expressionToString(procedureParameters(expr), c_parameters) def t_pairExpressionToString(expr, cont): """Converts a pair expression to a string.""" pieces = [] 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) markExprAsSeen(expr) return loop(expr, lambda pieces: pogo.bounce(cont, '(' + ' '.join(pieces) + ')')) return pogo.pogo(t_expressionToString(expr, pogo.land))