#lang scribble/doc @(require "utils.ss") @title{Evaluation} A Scheme S-expression is evaluated by calling @cppi{scheme_eval}. This function takes an S-expression (as a @cpp{Scheme_Object*}) and a namespace and returns the value of the expression in that namespace. The function @cppi{scheme_apply} takes a @cpp{Scheme_Object*} that is a procedure, the number of arguments to pass to the procedure, and an array of @cpp{Scheme_Object *} arguments. The return value is the result of the application. There is also a function @cppi{scheme_apply_to_list}, which takes a procedure and a list (constructed with @cppi{scheme_make_pair}) and performs the Scheme @scheme[apply] operation. The @cppi{scheme_eval} function actually calls @cppi{scheme_compile} followed by @cppi{scheme_eval_compiled}. @; ---------------------------------------------------------------------- @section[#:tag "topleveleval"]{Top-level Evaluation Functions} The functions @cpp{scheme_eval}, @cpp{scheme_apply}, etc., are @defterm{top-level evaluation functions}. Continuation invocations are confined to jumps within a top-level evaluation. The functions @cppi{_scheme_eval_compiled}, @cppi{_scheme_apply}, etc. (with a leading underscore) provide the same functionality without starting a new top-level evaluation; these functions should only be used within new primitive procedures. Since these functions allow full continuation hops, calls to non-top-level evaluation functions can return zero or multiple times. Currently, escape continuations and primitive error escapes can jump out of all evaluation and application functions. For more information, see @secref["exceptions"]. @; ---------------------------------------------------------------------- @section{Tail Evaluation} @section-index{tail recursion} All of Scheme's built-in functions and syntax support proper tail-recursion. When a new primitive procedure or syntax is added to Scheme, special care must be taken to ensure that tail recursion is handled properly. Specifically, when the final return value of a function is the result of an application, then @cppi{scheme_tail_apply} should be used instead of @cppi{scheme_apply}. When @cppi{scheme_tail_apply} is called, it postpones the procedure application until control returns to the Scheme evaluation loop. For example, consider the following implementation of a @scheme[thunk-or] primitive, which takes any number of thunks and performs @scheme[or] on the results of the thunks, evaluating only as many thunks as necessary. @verbatim[#<