|
Am 18.01.2014 15:04 schröbte Antonio Vieiro:
C) Use closures for cons-cells. See [3].I would like to build a small s-_expression_ ([1]) library in Lua (5.1),
so I need to design cons-cells ([2]) first.
I would appreciate some advice in the design of the cons-cells. I am
thinking of two approaches:
A) Design cons-cells as Lua tables, in plain Lua, with a "car" and a
"cdr" being other Lua objects (possibly other cons-cells).
B) Design cons-cells as a C structure (Lua allocated and garbage
collected with a __gc metamethod), that keeps references to other Lua
objects using luaL_ref and luaL_unref (possibly with references to other
cons-cells).
D) Similar to A, but use the keys `1` and `2` (i.e. the array part of the table) for head and tail.
`cons`, `car`, `cdr`, `setcar`, and `setcdr` are the only functions that need to know the difference, so you can even change the implementation later ...