| Original | Python | |
|
Figure 4.1
The
eval–apply
cycle exposes the essence of a computer language.
|
Figure 4.2
The
evaluate–apply
cycle exposes the essence of a computer language.
|
In Python's strict mode—our preferred way to run Python programs—the name eval cannot be defined in user programs. It is reserved for a related but quite different predefined function. We opt for the complete word evaluate as a replacement.
Eval The function evaluate takes as arguments a program component—a statement or expression[1]—and an environment. and an environment. It classifies the expression component and directs its evaluation. Eval The function evaluate is structured as a case analysis of the syntactic type of the expression component to be evaluated. In order to keep the procedure function general, we express the determination of the type of an expression a component abstractly, making no commitment to any particular representation for the various types of expressions. components. Each type of expression component has a predicate syntax predicate that tests for it and an abstract means for selecting its parts. This abstract syntax makes it easy to see how we can change the syntax of the language by using the same evaluator, but with a different collection of syntax procedures. functions.
| Original | Python | |
|
|
For clarity, eval evaluate has been implemented as a case analysis using cond. conditional statements. The disadvantage of this is that our procedure function handles only a few distinguishable types of statements and expressions, and no new ones can be defined without editing the definition of eval. definition of evaluate. In most Lisp interpreter implementations, dispatching on the type of an expression a component is done in a data-directed style. This allows a user to add new types of expressions that eval components that evaluate can distinguish, without modifying the definition of eval definition of evaluate itself. (See exercise 4.3.)
The representation of names is handled by the syntax abstractions. Internally, the evaluator uses strings to represent names, and we refer to such strings as symbols. The function symbol_of_name used in evaluate extracts from a name the symbol by which it is represented.
Apply The function apply takes two arguments, a procedure function and a linked list of arguments to which the procedure function should be applied. Apply The function apply classifies procedures functions into two kinds: It calls apply-primitive-procedure apply_built_in_function to apply built-in functions; it applies compound procedures functions by sequentially evaluating the expressions that make up the body of the procedure. by evaluating the body of the function. The environment for the evaluation of the body of a compound procedure function is constructed by extending the base environment carried by the procedure function to include a frame that binds the parameters of the procedure function to the arguments to which the procedure function is to be applied. Here is the definition definition of apply:
| Original | Python |
| (define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure)))) (else (error "Unknown procedure type - - APPLY" procedure)))) | def apply(fun, arguments): if is_built_in_function(fun): return apply_built_in_function(fun, arguments) elif is_compound_function(fun): locals = function_locals(fun) globals = function_globals(fun) unassigned_markers = map(lambda symbol: '*unassigned*', locals) global_markers = map(lambda symbol: '*global*', globals) result = evaluate(function_body(fun), extend_environment( append(function_parameters(fun), append(locals, globals)), append(arguments, append(unassigned_markers, global_markers)), function_environment(fun))) if is_return_value(result): return return_value_content(result) else: return None else: return error('unknown function type -- apply', fun) |
| Original | Python | |
| In order to return a value, a Python function needs to evaluate a return statement. If a function terminates without evaluating a return statement, the value None is returned. To distinguish the two cases, the evaluation of a return statement will wrap the result of evaluating its return expression into a return value. If the evaluation of the function body yields such a return value, the content of the return value is retrieved; otherwise the value None is returned.[2] The frame of a call binds the parameters to the arguments, the local names of the function to the marker '*unassigned*', and the global names of the function to the marker '*global*'. The function carries its local and global names, which are determined when the function is defined (see eval_function_definition below). The marker '*global*' tells assignments and name lookups to go to the global environment, skipping all intermediate frames. Names that the function declares as nonlocal are left out of the frame, so that the ordinary search through the enclosing frames finds them. |
When eval evaluate processes a procedure function application, it uses list-of-values llist_of_values to produce the listlinked list of arguments to which the procedure function is to be applied. List-of-values The function llist_of_values takes as an argument the operands of the combination. argument expressions of the application. It evaluates each operand argument expression and returns a listlinked list of the corresponding values:[3][4]
| Original | Python |
| (define (list-of-values exps env) (if (no-operands? exps) '() (cons (eval (first-operand exps) env) (list-of-values (rest-operands exps) env)))) | def llist_of_values(exps, env): return map(lambda arg: evaluate(arg, env), exps) |
Eval-if The function eval_conditional evaluates the predicate part of an if expression a conditional component in the given environment. If the result is true, eval-if evaluates the consequent, otherwise it evaluates the alternative: the consequent is evaluated, otherwise the alternative is evaluated:
| Original | Python |
| (define (eval-if exp env) (if (true? (eval (if-predicate exp) env)) (eval (if-consequent exp) env) (eval (if-alternative exp) env))) | def eval_conditional(component, env): if is_truthy(evaluate(conditional_predicate(component), env)): return evaluate(conditional_consequent(component), env) else: return evaluate(conditional_alternative(component), env) |
The use of true? is_truthy in eval-if eval_conditional highlights the issue of the connection between an implemented language and an implementation language. The if-predicate conditional_predicate is evaluated in the language being implemented and thus yields a value in that language. The interpreter predicate true? is_truthy translates that value into a value that can be tested by the if conditional expression in the implementation language: The metacircular representation of truth might not be the same as that of the underlying SchemePython.[5]
| Original | Python | |
|
Eval-sequence is used by apply to evaluate the sequence of expressions in a procedure body and by eval to evaluate the sequence of expressions in a begin expression. It takes as arguments a sequence of expressions and an environment, and evaluates the expressions in the order in which they occur. The value returned is the value of the final expression. (define (eval-sequence exps env) (cond ((last-exp? exps) (eval (first-exp exps) env)) (else (eval (first-exp exps) env) (eval-sequence (rest-exps exps) env)))) |
The function eval_sequence is used by evaluate to evaluate a sequence of statements at the top level, in a function body, or in branch of a conditional statement. It takes as arguments a sequence of statements and an environment, and evaluates the statements in the order in which they occur. If the evaluation of any statement in the sequence yields a return value, that value is returned and the subsequent statements are ignored. def eval_sequence(stmts, env): if is_empty_sequence(stmts): return None elif is_last_statement(stmts): return evaluate(first_statement(stmts), env) else: first_stmt_value = evaluate(first_statement(stmts), env) if is_return_value(first_stmt_value): return first_stmt_value else: return eval_sequence(rest_statements(stmts), env) The function eval_return_statement is used to evaluate return statements. As seen in apply and the evaluation of sequences, the result of evaluation of a return statement needs to be identifiable so that the evaluation of a function body can return immediately, even if there are statements after the return statement. For this purpose, the evaluation of a return statement wraps the result of evaluating the return expression in a return value object.[6] def eval_return_statement(component, env): return make_return_value(evaluate(return_expression(component), env)) |
The following procedure function eval_assignment handles assignments to variables. names. It calls eval to find the value to be assigned and transmits the variable and the resulting value to set-variable-value! to be installed in the designated environment. The function eval_assignment calls evaluate on the value expression to find the value to be assigned and calls assignment_symbol to retrieve the symbol that represents the name from the assignment. The function eval_assignment transmits the symbol and the value to assign_symbol_value to be installed in the designated environment. An assignment is a statement, evaluated for its effect: the result of a function body is determined by return statements alone, so eval_assignment returns the value None.
| Original | Python |
| (define (eval-assignment exp env) (set-variable-value! (assignment-variable exp) (eval (assignment-value exp) env) env) 'ok) | def eval_assignment(component, env): value = evaluate(assignment_value_expression(component), env) assign_symbol_value(assignment_symbol(component), value, env) return None |
| Original | Python | |
|
Definitions of variables are handled in a similar manner.[7] (define (eval-definition exp env) (define-variable! (definition-variable exp) (eval (definition-value exp) env) env) 'ok) We have chosen here to return the symbol ok as the value of an assignment or a definition.[8] |
The function eval_function_definition creates the function in the current environment and assigns it to the name of the function definition. Like an assignment, a function definition is evaluated for its effect, and eval_function_definition returns the value None. def eval_function_definition(component, env): parameters = function_definition_parameter_symbols(component) body = function_definition_body(component) fun = make_function(parameters, local_variables(parameters, body), global_variables(body), body, env) assign_symbol_value(function_definition_symbol(component), fun, env) return None To find the names that a function body introduces, we scan the body for declarations and assignments, looking into sequences and conditionals but ignoring nested function definitions: their names are taken care of when the nested function definition is evaluated. The function declared_symbols collects the symbols from all statements that satisfy a given predicate, using the function target_symbols, which returns the linked list of symbols that a statement introduces or refers to: the symbols in a global or nonlocal declaration, the symbol on the left-hand side of an assignment, or the name of a defined function. The functions global_variables and nonlocal_variables use it to find the symbols in global and nonlocal declarations. The function assigned_variables uses it to find the symbols that represent the names from the assignments and function definitions. The local names of a function are its assigned variables, except for its parameters and for the names that the body declares as global or nonlocal; the function local_variables leaves these out. def declared_symbols(component, is_declaration): if is_sequence(component): return reduce(append, None, map(lambda statement: declared_symbols(statement, is_declaration), sequence_statements(component))) elif is_conditional(component): return append(declared_symbols(conditional_consequent(component), is_declaration), declared_symbols(conditional_alternative(component), is_declaration)) elif is_declaration(component): return target_symbols(component) else: return None def target_symbols(component): if is_assignment(component): return llist(assignment_symbol(component)) elif is_function_definition(component): return llist(function_definition_symbol(component)) else: return declaration_symbols(component) def global_variables(component): return declared_symbols(component, is_global_statement) def nonlocal_variables(component): return declared_symbols(component, is_nonlocal_statement) def assigned_variables(component): return declared_symbols( component, lambda statement: (is_assignment(statement) or is_function_definition(statement))) def local_variables(parameters, body): excluded = append(parameters, append(global_variables(body), nonlocal_variables(body))) return filter(lambda symbol: is_none(member(symbol, excluded)), assigned_variables(body)) |
| Original | Python | |
|
|