The evaluation of expressions in Emacs Lisp is performed by the
Lisp interpreter---a program that receives a Lisp object as input
and computes its value as an expression. How it does this depends
on the data type of the object, according to rules described in this
chapter. The interpreter runs automatically to evaluate portions of
your program, but can also be called explicitly via the Lisp primitive
function eval
.
A Lisp object that is intended for evaluation is called an expression or a form. The fact that expressions are data objects and not merely text is one of the fundamental differences between Lisp-like languages and typical programming languages. Any object can be evaluated, but in practice only numbers, symbols, lists and strings are evaluated very often.
It is very common to read a Lisp expression and then evaluate the
expression, but reading and evaluation are separate activities, and
either can be performed alone. Reading per se does not evaluate
anything; it converts the printed representation of a Lisp object to the
object itself. It is up to the caller of read
whether this
object is a form to be evaluated, or serves some entirely different
purpose. See section Input Functions.
Do not confuse evaluation with command key interpretation. The
editor command loop translates keyboard input into a command (an
interactively callable function) using the active keymaps, and then
uses call-interactively
to invoke the command. The execution of
the command itself involves evaluation if the command is written in
Lisp, but that is not a part of command key interpretation itself.
See section Command Loop.
Evaluation is a recursive process. That is, evaluation of a form may
call eval
to evaluate parts of the form. For example, evaluation
of a function call first evaluates each argument of the function call,
and then evaluates each form in the function body. Consider evaluation
of the form (car x)
: the subform x
must first be evaluated
recursively, so that its value can be passed as an argument to the
function car
.
Evaluation of a function call ultimately calls the function specified in it. See section Functions. The execution of the function may itself work by evaluating the function definition; or the function may be a Lisp primitive implemented in C, or it may be a byte-compiled function (see section Byte Compilation).
The evaluation of forms takes place in a context called the environment, which consists of the current values and bindings of all Lisp variables.(2) Whenever a form refers to a variable without creating a new binding for it, the value of the variable's binding in the current environment is used. See section Variables.
Evaluation of a form may create new environments for recursive
evaluation by binding variables (see section Local Variables). These
environments are temporary and vanish by the time evaluation of the form
is complete. The form may also make changes that persist; these changes
are called side effects. An example of a form that produces side
effects is (setq foo 1)
.
The details of what evaluation means for each kind of form are described below (see section Kinds of Forms).
Go to the first, previous, next, last section, table of contents.