class CompoundProcedure: def __init__(self, vars, bproc, env): self.vars = vars self.bproc = bproc self.env = env def is_compound_procedure(obj): return isinstance(obj, CompoundProcedure) def make_compound_procedure(vars, bproc, env): return CompoundProcedure(vars, bproc, env) def procedure_parameters(cproc): return cproc.vars def procedure_body(cproc): return cproc.bproc def procedure_environment(cproc): return cproc.env procedure_parameter_name = simple_generic_procedure("parameter-name", 1, lambda x: x) define_generic_procedure_handler(procedure_parameter_name, match_args(is_pair), car)
from chapter03.generic_procedures import ( define_generic_procedure_handler, match_args, simple_generic_procedure, ) from chapter05.common.pairs import car, cdr, is_null, is_pair display = simple_generic_procedure("display", 1, lambda obj: print(obj, end="")) def display_pair(p, parens=True): if parens: print("(", end="") display(car(p)) if is_null(cdr(p)): # no more items pass elif is_pair(cdr(p)): print(" ", end="") display_pair(cdr(p), parens=False) else: print(" . ", end="") display(cdr(p)) if parens: print(")", end="") define_generic_procedure_handler(display, match_args(is_pair), display_pair)
def analyze_application(expression): operator_exec = analyze(operator(expression)) operand_execs = tuple(map(analyze, operands(expression))) def execute_application(environment): return x.apply(x.advance(operator_exec(environment)), operand_execs, environment) return execute_application def analyze_self_evaluating(expression): return lambda environment: expression define_generic_procedure_handler(x.analyze, match_args(is_boolean), analyze_self_evaluating) define_generic_procedure_handler(x.analyze, match_args(is_number), analyze_self_evaluating) define_generic_procedure_handler(x.analyze, match_args(is_string), analyze_self_evaluating) def analyze_quoted(expression): qval = text_of_quotation(expression) return lambda environment: qval define_generic_procedure_handler(x.analyze, match_args(is_quoted), analyze_quoted)
def define_apply_handler(applicability, handler): define_generic_procedure_handler(g.apply, applicability, handler)
def define_advance_handler(applicability, handler): define_generic_procedure_handler(g.advance, applicability, handler)
def define_eval_handler(applicability, handler): define_generic_procedure_handler(g.eval, applicability, handler)
extend_environment(names, arguments, procedure_environment(procedure))) g.define_apply_handler( match_args(is_general_compound_procedure, is_operands, is_environment), apply_general_compound_procedure, ) def is_operand(_exp): return True define_generic_procedure_handler( g.handle_operand, match_args(is_lazy, is_operand, is_environment), lambda parameter, operand, environment: postpone(operand, environment), ) define_generic_procedure_handler( g.handle_operand, match_args(is_lazy_memo, is_operand, is_environment), lambda parameter, operand, environment: postpone_memo( operand, environment), ) g.define_advance_handler( match_args(is_postponed), lambda object: g.advance(g.eval(object.expression, object.environment)), )
operand_execs = tuple(map(analyze, operands(expression))) return lambda env, succeed, fail: execute_strict( operator_exec, env, lambda procedure, fail2: continue_with( a.apply, procedure, operand_execs, env, succeed, fail2), fail, ) def analyze_self_evaluating(expression): return lambda env, succeed, fail: continue_with(succeed, expression, fail) define_generic_procedure_handler(a.analyze, match_args(is_boolean), analyze_self_evaluating) define_generic_procedure_handler(a.analyze, match_args(is_number), analyze_self_evaluating) define_generic_procedure_handler(a.analyze, match_args(is_string), analyze_self_evaluating) def analyze_quoted(expression): qval = text_of_quotation(expression) return lambda env, succeed, fail: continue_with(succeed, qval, fail) define_generic_procedure_handler(a.analyze, match_args(is_quoted), analyze_quoted)