コード例 #1
0
ファイル: procedures.py プロジェクト: mbillingr/sdf
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)
コード例 #2
0
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)
コード例 #3
0
ファイル: fast_interpreter.py プロジェクト: mbillingr/sdf
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)

コード例 #4
0
ファイル: generic_interpreter.py プロジェクト: mbillingr/sdf
 def define_apply_handler(applicability, handler):
     define_generic_procedure_handler(g.apply, applicability, handler)
コード例 #5
0
ファイル: generic_interpreter.py プロジェクト: mbillingr/sdf
 def define_advance_handler(applicability, handler):
     define_generic_procedure_handler(g.advance, applicability, handler)
コード例 #6
0
ファイル: generic_interpreter.py プロジェクト: mbillingr/sdf
 def define_eval_handler(applicability, handler):
     define_generic_procedure_handler(g.eval, applicability, handler)
コード例 #7
0
ファイル: generic_interpreter.py プロジェクト: mbillingr/sdf
        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)),
)
コード例 #8
0
ファイル: cps_interpreter.py プロジェクト: mbillingr/sdf
    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)