Ejemplo n.º 1
0
def teval(exp, env, cont):
    """Evaluates an expression 'exp' in an environment 'env'.

    Exercise 4.3 asks us to rewrite this in a more natural
    data-directed manner.  Pychecker, also, doesn't like seeing
    so many 'return' statements in one function.  *grin*
    """
    if expressions.isSelfEvaluating(exp):
        return pogo.bounce(cont, exp)
    if expressions.isVariable(exp):
        return pogo.bounce(cont, environment.lookupVariableValue(exp, env))
    if expressions.isQuoted(exp):
        return evalQuoted(exp, env, cont)
    if expressions.isAssignment(exp):
        return evalAssignment(exp, env, cont)
    if expressions.isDefinition(exp):
        return evalDefinition(exp, env, cont)
    if expressions.isIf(exp):
        return evalIf(exp, env, cont)
    if expressions.isLambda(exp):
        return pogo.bounce(cont, expressions.makeProcedure
                          (expressions.lambdaParameters(exp),
                           expressions.lambdaBody(exp),
                           env))
    if expressions.isBegin(exp):
        return evalSequence(expressions.beginActions(exp), env, cont)
    if expressions.isApplication(exp):
        return evalApplication(exp, env, cont)
    raise SchemeError, "Unknown expression type -- eval " + str(exp)
Ejemplo n.º 2
0
def analyze(exp):
    """analyze(exp) -> lambda env, cont: ...
    Given an expression, returns a new lambda function that can be applied
    on an environment and continuation."""
    if expressions.isSelfEvaluating(exp):
        return analyzeSelfEvaluating(exp)
    if expressions.isVariable(exp):
        return analyzeVariable(exp)
    if expressions.isQuoted(exp):
        return analyzeQuoted(exp)
    if expressions.isAssignment(exp):
        return analyzeAssignment(exp)
    if expressions.isDefinition(exp):
        return analyzeDefinition(exp)
    if expressions.isIf(exp):
        return analyzeIf(exp)
    if expressions.isLambda(exp):
        return analyzeLambda(exp)
    if expressions.isBegin(exp):
        return analyzeBegin(exp)
    ## Application checking must come last, after all the special forms.
    ## have been tested.
    if expressions.isApplication(exp):
        return analyzeApplication(exp)

    ## And if we get here, bad things have happened.
    raise SchemeError, ("Unknown expression type -- eval " +
                        expressions.toString(exp))