基本的 Lisp 数据结构,用 C 语言术语表示,可能如下所示:
/* A value is a "discriminated union". */
typedef struct value {
/* It has a type field. */
enum type { t_cons, t_symbol, t_fixnum, t_character /* , ... */ } type;
/* And then one of several payloads, overlaid in the same space,
which one being there depending on the type field. */
union {
struct symbol *sym;
struct cons *cons;
int fixnum; /* unboxed integer: no heap allocation */
/* ... */
} u;
} value;
/* This is the heap-allocated part of a cons cell;
not the complete cons cell value, which is actually
of "struct value" type. See cons()
function below which makes a cons value. */
struct cons {
struct value car, cdr;
};
static value nil = { t_symbol };
value cons(value a, value d)
{
value retv;
struct cons *c = allocate_cons(); /* from special cons heap */
c->car = a;
c->cdr = c;
retv.type = t_cons;
retv.u.cons = c;
return retv;
}
int is_nil(value v)
{
return (v.type == t_symbol && v.sym == NULL);
}
value cons(value a, value d)
{
struct value retv;
struct cons *c = allocate_cons(); /* from special cons heap */
c->car = a;
c->cdr = c;
retv.type = t_cons;
retv.u.cons = c;
return retv;
}
value car(value arg)
{
switch (arg.type) {
case t_cons:
return arg.u.cons->car;
case t_symbol:
if (is_nil(arg)) /* (car nil) -> nil */
return nil;
/* fallthrough */
default:
/* This function generates a Lisp exception somehow */
throw_error("car: not applicable to ~s", arg);
}
}
Lisp 概念没有详细说明数据结构。
实际的 Lisp 实现通常会做一些更聪明的事情来更紧凑地表示 value。一种常见的技术是使用机器字(现在通常是指针大小)作为 Lisp 值,并在该字中使用几个 标记位 来指示它是否是指向堆上某物的指针,或者整数的直接表示。 (这意味着 Lisp fixnum 整数不使用所有可用的 32 或 64 位,但可能只有 30 或 62。更大的整数是不同的类型 bignum,并且是堆分配的。)
然而,将结构体用于值而不是指针,为浮点值创造了按值语义的机会,这对数字代码来说是一个胜利。这意味着浮点对象不必在堆中分配,而是存储在一个值中。
用 C 编写的 Lisp 实现可以做这种诡计,但它会导致 ISO C 未定义的行为,并且声明和代码对于说明目的来说并不那么好。
对于这种类型的表示,一个很好的细节是为 Lisp 符号 nil 使用 C 空指针。然后,任何用 C 编写的内部例程都可以使用与 Lisp 相同的约定以符合人体工程学的方式编写:nil 既是假的又是空列表。
C 受 Lisp 的影响很大,因为它基于返回值的表达式,并且空指针是错误的。 C 中的 a?b:c 三元运算符在某种程度上是对 Lisp 的 (if a b c) 的仿冒。
要引导一些看起来像 Lisp 语义的东西需要相当多的 C 代码行,并且它的许多方面都有多种设计选择。因此,最好尝试将 Lisp 理解为一种抽象,而不是通过特定详细的数据结构和执行模型设计,更不用说用 C 来表达了。