| 1 |
@node Manipulating Lisp objects |
| 2 |
@section Manipulating Lisp objects |
| 3 |
|
| 4 |
@menu |
| 5 |
* Objects representation:: |
| 6 |
* Constructing objects:: |
| 7 |
@end menu |
| 8 |
|
| 9 |
If you want to extend, fix or simply customize ECL for your own needs, |
| 10 |
you should understand how the implementation works. |
| 11 |
|
| 12 |
@cppdef cl_lispunion |
| 13 |
@deftp @cind{} cl_lispunion { cons big ratio SF DF longfloat gencomplex csfloat cdfloat clfloat symbol pack hash array vector base_string string stream random readtable pathname bytecodes bclosure cfun cfunfixed cclosure d instance process queue lock rwlock condition_variable semaphore barrier mailbox cblock foreign frame weak sse } |
| 14 |
|
| 15 |
Union containing all first-class ECL types. |
| 16 |
@end deftp |
| 17 |
|
| 18 |
@node Objects representation |
| 19 |
@subsection Objects representation |
| 20 |
In ECL a lisp object is represented by a type called |
| 21 |
@code{cl_object}. This type is a word which is long enough to host both |
| 22 |
an integer and a pointer. The least significant bits of this word, also |
| 23 |
called the tag bits, determine whether it is a pointer to a C structure |
| 24 |
representing a complex object, or whether it is an immediate data, such |
| 25 |
as a fixnum or a character. |
| 26 |
|
| 27 |
@float Figure,fig:immediate_types |
| 28 |
@caption{Immediate types} |
| 29 |
@image{figures/immediate-types,,1in} |
| 30 |
@end float |
| 31 |
|
| 32 |
The topic of the immediate values and bit fiddling is nicely described |
| 33 |
in |
| 34 |
@url{http://www.more-magic.net/posts/internals-data-representation.html, |
| 35 |
Peter Bex's blog} describing @url{http://www.call-cc.org/,Chicken |
| 36 |
Scheme} internal data representation. We could borrow some ideas from it |
| 37 |
(like improving @code{fixnum} bitness and providing more immediate |
| 38 |
values). All changes to code related to immediate values should be |
| 39 |
carefully @strong{benchmarked}. |
| 40 |
|
| 41 |
The @code{fixnums} and characters are called immediate data types, |
| 42 |
because they require no more than the @code{cl_object} datatype to store |
| 43 |
all information. All other ECL objects are non-immediate and they are |
| 44 |
represented by a pointer to a cell that is allocated on the heap. Each |
| 45 |
cell consists of several words of memory and contains all the |
| 46 |
information related to that object. By storing data in multiples of a |
| 47 |
word size, we make sure that the least significant bits of a pointer are |
| 48 |
zero, which distinguishes pointers from immediate data. |
| 49 |
|
| 50 |
In an immediate datatype, the tag bits determine the type of the |
| 51 |
object. In non-immediate datatypes, the first byte in the cell contains |
| 52 |
the secondary type indicator, and distinguishes between different types |
| 53 |
of non immediate data. The use of the remaining bytes differs for each |
| 54 |
type of object. For instance, a cons cell consists of three words: |
| 55 |
|
| 56 |
@verbatim |
| 57 |
+---------+----------+ |
| 58 |
| CONS | | |
| 59 |
+---------+----------+ |
| 60 |
| car-pointer | |
| 61 |
+--------------------+ |
| 62 |
| cdr-pointer | |
| 63 |
+--------------------+ |
| 64 |
@end verbatim |
| 65 |
|
| 66 |
@cfindex --enable-small-cons [YES|no] |
| 67 |
|
| 68 |
Note, that this is one of the possible implementation of |
| 69 |
@code{cons}. The second one (currently default) uses the immediate value |
| 70 |
for the @code{list} and consumes two words instead of three. Such |
| 71 |
implementation is more memory and speed efficient (according to the |
| 72 |
comments in the source code): |
| 73 |
|
| 74 |
@verbatim |
| 75 |
/* |
| 76 |
* CONSES |
| 77 |
* |
| 78 |
* We implement two variants. The "small cons" type carries the type |
| 79 |
* information in the least significant bits of the pointer. We have |
| 80 |
* to do some pointer arithmetics to find out the CAR / CDR of the |
| 81 |
* cons but the overall result is faster and memory efficient, only |
| 82 |
* using two words per cons. |
| 83 |
* |
| 84 |
* The other scheme stores conses as three-words objects, the first |
| 85 |
* word carrying the type information. This is kept for backward |
| 86 |
* compatibility and also because the oldest garbage collector does |
| 87 |
* not yet support the smaller datatype. |
| 88 |
* |
| 89 |
* To make code portable and independent of the representation, only |
| 90 |
* access the objects using the common macros below (that is all |
| 91 |
* except ECL_CONS_PTR or ECL_PTR_CONS). |
| 92 |
*/ |
| 93 |
@end verbatim |
| 94 |
|
| 95 |
@cppdef cl_object |
| 96 |
@deftp @cind{} cl_object |
| 97 |
This is the type of a lisp object. For your C/C++ program, a cl_object |
| 98 |
can be either a fixnum, a character, or a pointer to a union of |
| 99 |
structures (See @code{cl_lispunion} in the header object.h). The actual |
| 100 |
interpretation of that object can be guessed with the macro |
| 101 |
@code{ecl_t_of}. |
| 102 |
|
| 103 |
@subsubheading Example |
| 104 |
@exindex @code{cl_object} checking the type with @code{ecl_t_of} |
| 105 |
|
| 106 |
For example, if x is of type cl_object, and it is of type fixnum, we may |
| 107 |
retrieve its value: |
| 108 |
|
| 109 |
@example |
| 110 |
if (ecl_t_of(x) == t_fixnum) |
| 111 |
printf("Integer value: %d\n", ecl_fixnum(x)); |
| 112 |
@end example |
| 113 |
|
| 114 |
@subsubheading Example |
| 115 |
@exindex Accessing underlying @code{cl_object} structure |
| 116 |
|
| 117 |
If @code{x} is of type @code{cl_object} and it does not contain an |
| 118 |
immediate datatype, you may inspect the cell associated to the lisp |
| 119 |
object using @code{x} as a pointer. For example: |
| 120 |
|
| 121 |
@example |
| 122 |
if (ecl_t_of(x) == t_vector) |
| 123 |
printf("Vector's dimension is: %d\n", x->vector.dim); |
| 124 |
@end example |
| 125 |
|
| 126 |
You should see the following sections and the header object.h to learn |
| 127 |
how to use the different fields of a @code{cl_object} pointer. |
| 128 |
@end deftp |
| 129 |
|
| 130 |
@deftp @cind{} cl_type |
| 131 |
Enumeration type which distinguishes the different types of lisp |
| 132 |
objects. The most important values are: |
| 133 |
|
| 134 |
@cppdef t_start |
| 135 |
@cppdef t_list |
| 136 |
@cppdef t_character |
| 137 |
@cppdef t_fixnum |
| 138 |
@cppdef t_bignum |
| 139 |
@cppdef t_ratio |
| 140 |
@c @cppdef t_shortfloat |
| 141 |
@cppdef t_singlefloat |
| 142 |
@c #ifdef ECL_LONG_FLOAT |
| 143 |
@cppdef t_longfloat |
| 144 |
@c #endif |
| 145 |
@cppdef t_complex |
| 146 |
@c #ifdef ECL_COMPLEX_FLOAT |
| 147 |
@cppdef t_csfloat |
| 148 |
@cppdef t_cdfloat |
| 149 |
@cppdef t_clfloat |
| 150 |
@c #endif |
| 151 |
@cppdef t_symbol |
| 152 |
@cppdef t_package |
| 153 |
@cppdef t_hashtable |
| 154 |
@cppdef t_array |
| 155 |
@cppdef t_vector |
| 156 |
@c #ifdef ECL_UNICODE |
| 157 |
@cppdef t_string |
| 158 |
@c #endif |
| 159 |
@cppdef t_base_string |
| 160 |
@cppdef t_bitvector |
| 161 |
@cppdef t_stream |
| 162 |
@cppdef t_random |
| 163 |
@cppdef t_readtable |
| 164 |
@cppdef t_pathname |
| 165 |
@cppdef t_bytecodes |
| 166 |
@cppdef t_bclosure |
| 167 |
@cppdef t_cfun |
| 168 |
@cppdef t_cfunfixed |
| 169 |
@cppdef t_cclosure |
| 170 |
@cppdef t_instance |
| 171 |
@cppdef t_structure = t_instance |
| 172 |
@c #ifdef ECL_THREADS |
| 173 |
@cppdef t_process |
| 174 |
@cppdef t_lock |
| 175 |
@cppdef t_rwlock |
| 176 |
@cppdef t_condition_variable |
| 177 |
@cppdef t_semaphore |
| 178 |
@cppdef t_barrier |
| 179 |
@cppdef t_mailbox |
| 180 |
@c #endif |
| 181 |
@cppdef t_codeblock |
| 182 |
@cppdef t_foreign |
| 183 |
@cppdef t_frame |
| 184 |
@cppdef t_weak_pointer |
| 185 |
@c #ifdef ECL_SSE2 |
| 186 |
@cppdef t_sse_pack |
| 187 |
@c #endif |
| 188 |
@cppdef t_end |
| 189 |
@cppdef t_other |
| 190 |
@cppdef t_contiguous – contiguous block |
| 191 |
|
| 192 |
@code{t_cons} @code{t_fixnum}, @code{t_character}, @code{t_bignum}, |
| 193 |
@code{t_ratio}, @code{t_singlefloat}, @code{t_doublefloat}, |
| 194 |
@code{t_complex}, @code{t_symbol}, @code{t_package}, @code{t_hashtable}, |
| 195 |
@code{t_array}, @code{t_vector}, @code{t_string}, @code{t_bitvector}, |
| 196 |
@code{t_stream}, @code{t_random}, @code{t_readtable}, @code{t_pathname}, |
| 197 |
@code{t_bytecodes}, @code{t_cfun}, @code{t_cclosure}, @code{t_gfun}, |
| 198 |
@code{t_instance}, @code{t_foreign} and @code{t_thread}. |
| 199 |
@end deftp |
| 200 |
|
| 201 |
@cppdef ecl_t_of |
| 202 |
@deftypefun cl_type ecl_t_of (cl_object x) |
| 203 |
If @var{x} is a valid lisp object, @code{ecl_t_of(x)} returns an integer |
| 204 |
denoting the type that lisp object. That integer is one of the values of |
| 205 |
the enumeration type @code{cl_type}. |
| 206 |
@end deftypefun |
| 207 |
|
| 208 |
@cppdef ECL_CHARACTERP |
| 209 |
@cppdef ECL_BASE_CHAR_P |
| 210 |
@cppdef ECL_BASE_CHAR_CODE_P |
| 211 |
@cppdef ECL_NUMBER_TYPE_P |
| 212 |
@cppdef ECL_COMPLEXP |
| 213 |
@cppdef ECL_REAL_TYPE_P |
| 214 |
@cppdef ECL_FIXNUMP |
| 215 |
@cppdef ECL_BIGNUMP |
| 216 |
@cppdef ECL_SINGLE_FLOAT_P |
| 217 |
@cppdef ECL_DOUBLE_FLOAT_P |
| 218 |
@cppdef ECL_LONG_FLOAT_P |
| 219 |
@cppdef ECL_CONSP |
| 220 |
@cppdef ECL_LISTP |
| 221 |
@cppdef ECL_ATOM |
| 222 |
@cppdef ECL_SYMBOLP |
| 223 |
@cppdef ECL_ARRAYP |
| 224 |
@cppdef ECL_VECTORP |
| 225 |
@cppdef ECL_BIT_VECTOR_P |
| 226 |
@cppdef ECL_STRINGP |
| 227 |
@cppdef ECL_HASH_TABLE_P |
| 228 |
@cppdef ECL_RANDOM_STATE_P |
| 229 |
@cppdef ECL_PACKAGEP |
| 230 |
@cppdef ECL_PATHNAMEP |
| 231 |
@cppdef ECL_READTABLEP |
| 232 |
@cppdef ECL_FOREIGN_DATA_P |
| 233 |
@cppdef ECL_SSE_PACK_P |
| 234 |
|
| 235 |
@deftypefun bool ECL_CHARACTERP (cl_object o) |
| 236 |
@deftypefunx bool ECL_BASE_CHAR_P (cl_object o) |
| 237 |
@deftypefunx bool ECL_BASE_CHAR_CODE_P (ecl_character o) |
| 238 |
@deftypefunx bool ECL_NUMBER_TYPE_P (cl_object o) |
| 239 |
@deftypefunx bool ECL_COMPLEXP (cl_object o) |
| 240 |
@deftypefunx bool ECL_REAL_TYPE_P (cl_object o) |
| 241 |
@deftypefunx bool ECL_FIXNUMP (cl_object o) |
| 242 |
@deftypefunx bool ECL_BIGNUMP (cl_object o) |
| 243 |
@deftypefunx bool ECL_SINGLE_FLOAT_P (cl_object o) |
| 244 |
@deftypefunx bool ECL_DOUBLE_FLOAT_P (cl_object o) |
| 245 |
@deftypefunx bool ECL_LONG_FLOAT_P (cl_object o) |
| 246 |
@deftypefunx bool ECL_CONSP (cl_object o) |
| 247 |
@deftypefunx bool ECL_LISTP (cl_object o) |
| 248 |
@deftypefunx bool ECL_ATOM (cl_object o) |
| 249 |
@deftypefunx bool ECL_SYMBOLP (cl_object o) |
| 250 |
@deftypefunx bool ECL_ARRAYP (cl_object o) |
| 251 |
@deftypefunx bool ECL_VECTORP (cl_object o) |
| 252 |
@deftypefunx bool ECL_BIT_VECTOR_P (cl_object o) |
| 253 |
@deftypefunx bool ECL_STRINGP (cl_object o) |
| 254 |
@deftypefunx bool ECL_HASH_TABLE_P (cl_object o) |
| 255 |
@deftypefunx bool ECL_RANDOM_STATE_P (cl_object o) |
| 256 |
@deftypefunx bool ECL_PACKAGEP (cl_object o) |
| 257 |
@deftypefunx bool ECL_PATHNAMEP (cl_object o) |
| 258 |
@deftypefunx bool ECL_READTABLEP (cl_object o) |
| 259 |
@deftypefunx bool ECL_FOREIGN_DATA_P (cl_object o) |
| 260 |
@deftypefunx bool ECL_SSE_PACK_P (cl_object o) |
| 261 |
|
| 262 |
Different macros that check whether @var{o} belongs to the specified |
| 263 |
type. These checks have been optimized, and are preferred over several |
| 264 |
calls to @code{ecl_t_of}. |
| 265 |
@end deftypefun |
| 266 |
|
| 267 |
@cppdef ECL_IMMEDIATE |
| 268 |
@deftypefun bool ECL_IMMEDIATE (cl_object o) |
| 269 |
Tells whether @var{x} is an immediate datatype. |
| 270 |
@end deftypefun |
| 271 |
|
| 272 |
@node Constructing objects |
| 273 |
@subsection Constructing objects |
| 274 |
|
| 275 |
On each of the following sections we will document the standard |
| 276 |
interface for building objects of different types. For some objects, |
| 277 |
though, it is too difficult to make a C interface that resembles all of |
| 278 |
the functionality in the lisp environment. In those cases you need to |
| 279 |
|
| 280 |
@enumerate |
| 281 |
@item build the objects from their textual representation, or |
| 282 |
@item use the evaluator to build these objects. |
| 283 |
@end enumerate |
| 284 |
|
| 285 |
The first way makes use of a C or Lisp string to construct an |
| 286 |
object. The two functions you need to know are the following ones. |
| 287 |
|
| 288 |
@cppdef c_string_to_object |
| 289 |
@cppdef ecl_read_from_cstring |
| 290 |
@cppdef si_string_to_object |
| 291 |
@defun si::string-to-object string &optional (err-value nil) |
| 292 |
@end defun |
| 293 |
@deftypefun cl_object si_string_to_object (cl_narg narg, cl_object str, ...) |
| 294 |
@deftypefunx cl_object ecl_read_from_cstring (const char *s) |
| 295 |
@coderef{ecl_read_from_cstring} builds a lisp object from a C string |
| 296 |
which contains a suitable representation of a lisp |
| 297 |
object. @coderef{si_string_to_object} performs the same task, but uses a |
| 298 |
lisp string, and therefore it is less useful. |
| 299 |
|
| 300 |
@itemize @bullet |
| 301 |
@item @strong{DEPRECATED} @coderef{c_string_to_object} – equivalent to @coderef{ecl_read_from_cstring} |
| 302 |
@end itemize |
| 303 |
|
| 304 |
@subsubheading Example |
| 305 |
@exindex @coderef{ecl_read_from_cstring} constructing Lisp objects in C |
| 306 |
|
| 307 |
Using a C string |
| 308 |
@example |
| 309 |
cl_object array1 = ecl_read_from_cstring("#(1 2 3 4)"); |
| 310 |
@end example |
| 311 |
|
| 312 |
Using a Lisp string |
| 313 |
@example |
| 314 |
cl_object string = make_simple_base_string("#(1 2 3 4)"); |
| 315 |
cl_object array2 = si_string_to_object(string); |
| 316 |
@end example |
| 317 |
@end deftypefun |
| 318 |
|
| 319 |
@subheading Integers |
| 320 |
Common-Lisp distinguishes two types of integer types: @code{bignum}s and |
| 321 |
@code{fixnum}s. A fixnum is a small integer, which ideally occupies only |
| 322 |
a word of memory and which is between the values |
| 323 |
@coderef{MOST-NEGATIVE-FIXNUM} and @coderef{MOST-POSITIVE-FIXNUM}. A |
| 324 |
@code{bignum} is any integer which is not a @code{fixnum} and it is only |
| 325 |
constrained by the amount of memory available to represent it. |
| 326 |
|
| 327 |
In ECL a @code{fixnum} is an integer that, together with the tag bits, |
| 328 |
fits in a word of memory. The size of a word, and thus the size of a |
| 329 |
@code{fixnum}, varies from one architecture to another, and you should |
| 330 |
refer to the types and constants in the ecl.h header to make sure that |
| 331 |
your C extensions are portable. All other integers are stored as |
| 332 |
@code{bignum}s, they are not immediate objects, they take up a variable |
| 333 |
amount of memory and the GNU Multiprecision Library is required to |
| 334 |
create, manipulate and calculate with them. |
| 335 |
|
| 336 |
@cppdef cl_fixnum |
| 337 |
@deftp @cind{} cl_fixnum |
| 338 |
This is a C signed integer type capable of holding a whole @code{fixnum} |
| 339 |
without any loss of precision. The opposite is not true, and you may |
| 340 |
create a @code{cl_fixnum} which exceeds the limits of a fixnum and |
| 341 |
should be stored as a @code{bignum}. |
| 342 |
@end deftp |
| 343 |
|
| 344 |
@cppdef cl_index |
| 345 |
@deftp @cind{} cl_index |
| 346 |
This is a C unsigned integer type capable of holding a non-negative |
| 347 |
@code{fixnum} without loss of precision. Typically, a @code{cl_index} is |
| 348 |
used as an index into an array, or into a proper list, etc. |
| 349 |
@end deftp |
| 350 |
|
| 351 |
@cppdef MOST_NEGATIVE_FIXNUM |
| 352 |
@cppdef MOST_POSITIVE_FIXNUM |
| 353 |
@lspdef MOST-NEGATIVE-FIXNUM |
| 354 |
@lspdef MOST-POSITIVE-FIXNUM |
| 355 |
@defvr {Constant} MOST_NEGATIVE_FIXNUM |
| 356 |
@defvrx {Constant} MOST_POSITIVE_FIXNUM |
| 357 |
These constants mark the limits of a @code{fixnum}. |
| 358 |
@end defvr |
| 359 |
|
| 360 |
@cppdef ecl_fixnum_lower |
| 361 |
@cppdef ecl_fixnum_greater |
| 362 |
@cppdef ecl_fixnum_leq |
| 363 |
@cppdef ecl_fixnum_geq |
| 364 |
@cppdef ecl_fixnum_plusp |
| 365 |
@cppdef ecl_fixnum_minusp |
| 366 |
@deftypefun bool ecl_fixnum_lower (cl_fixnum a, cl_fixnum b) |
| 367 |
@deftypefunx bool ecl_fixnum_greater (cl_fixnum a, cl_fixnum b) |
| 368 |
@deftypefunx bool ecl_fixnum_leq (cl_fixnum a, cl_fixnum b) |
| 369 |
@deftypefunx bool ecl_fixnum_geq (cl_fixnum a, cl_fixnum b) |
| 370 |
@deftypefunx bool ecl_fixnum_plusp (cl_fixnum a) |
| 371 |
@deftypefunx bool ecl_fixnum_minusp (cl_fixnum a) |
| 372 |
Operations on @code{fixnums} (comparison and predicates). |
| 373 |
@end deftypefun |
| 374 |
|
| 375 |
@cppindex ecl_make_fixnum |
| 376 |
@cppindex ecl_fixnum |
| 377 |
@cppindex MAKE_FIXNUM |
| 378 |
@cppindex fix |
| 379 |
@deftypefun cl_object ecl_make_fixnum (cl_fixnum n) |
| 380 |
@deftypefunx cl_fixnum ecl_fixnum (cl_object o) |
| 381 |
@code{ecl_make_fixnum} converts from an integer to a lisp object, while |
| 382 |
the @code{ecl_fixnum} does the opposite (converts lisp object fixnum to |
| 383 |
integer). These functions do @strong{not} check their arguments. |
| 384 |
@itemize @bullet |
| 385 |
@item @strong{DEPRECATED} @code{MAKE_FIXNUM} – equivalent to @code{ecl_make_fixnum} |
| 386 |
@item @strong{DEPRECATED} @code{fix} – equivalent to @code{ecl_fixnum} |
| 387 |
@end itemize |
| 388 |
@end deftypefun |
| 389 |
|
| 390 |
@cppindex cl_fixnum |
| 391 |
@cppindex cl_index |
| 392 |
@cppdef fixint |
| 393 |
@cppdef fixnint |
| 394 |
@deftypefun cl_fixnum fixint (cl_object o) |
| 395 |
@deftypefunx cl_index fixnint (cl_object o) |
| 396 |
Safe conversion of a lisp @code{fixnum} to a C integer of the |
| 397 |
appropriate size. Signals an error if @var{o} is not of fixnum type. |
| 398 |
|
| 399 |
@coderef{fixnint} additionally ensure that @var{o} is not negative. |
| 400 |
@end deftypefun |
| 401 |
|
| 402 |
@subheading Characters |
| 403 |
|
| 404 |
@cfindex --enable-unicode [32|16|no] |
| 405 |
|
| 406 |
ECL has two types of characters – one fits in the C type char, while |
| 407 |
the other is used when ECL is built with a configure option |
| 408 |
@code{--enable-unicode} which defaults to 32 (characters are stored in |
| 409 |
32bit variable and codepoints have 21-bits). |
| 410 |
|
| 411 |
@cppindex ecl_character |
| 412 |
@cppindex ecl_base_char |
| 413 |
@deftp @cind{} ecl_character |
| 414 |
Immediate type @code{t_character}. If ECL built with Unicode support, |
| 415 |
then may be either base or extended character, which may be |
| 416 |
distinguished with the predicate @coderef{ECL_BASE_CHAR_P}. |
| 417 |
|
| 418 |
Additionally we have @coderef{ecl_base_char} for base strings, which is an |
| 419 |
equivalent to the ordinary char. |
| 420 |
|
| 421 |
@subsubheading Example |
| 422 |
@exindex distinguishing between base and Unicode character |
| 423 |
@example |
| 424 |
if (ECL_CHARACTERP(o) && ECL_BASE_CHAR_P(o)) |
| 425 |
printf("Base character: %c\n", ECL_CHAR_CODE(o)); |
| 426 |
@end example |
| 427 |
@end deftp |
| 428 |
|
| 429 |
@cppindex ECL_CHAR_CODE_LIMIT |
| 430 |
@cppindex CHAR_CODE_LIMIT |
| 431 |
@defvr {Constant} ECL_CHAR_CODE_LIMIT |
| 432 |
Each character is assigned an integer code which ranges from 0 to |
| 433 |
(ECL_CHAR_CODE_LIMIT-1). |
| 434 |
@itemize @bullet |
| 435 |
@item @strong{DEPRECATED} @code{CHAR_CODE_LIMIT} – equivalent to @code{ECL_CHAR_CODE_LIMIT} |
| 436 |
@end itemize |
| 437 |
@end defvr |
| 438 |
|
| 439 |
@cppindex ecl_char_code |
| 440 |
@cppindex ecl_base_char_code |
| 441 |
@cppindex ECL_CHAR_CODE |
| 442 |
@cppindex ECL_CODE_CHAR |
| 443 |
@cppindex CODE_CHAR |
| 444 |
@cppindex CHAR_CODE |
| 445 |
@deftypefun cl_object ECL_CODE_CHAR (ecl_character o) |
| 446 |
@deftypefunx ecl_character ECL_CHAR_CODE (cl_object o) |
| 447 |
@deftypefunx ecl_character ecl_char_code (cl_object o) |
| 448 |
@deftypefunx ecl_base_char ecl_base_char_code (cl_object o) |
| 449 |
@code{ECL_CHAR_CODE}, @code{ecl_char_code} and |
| 450 |
@code{ecl_base_char_code} return the integer code associated to a |
| 451 |
lisp character. @code{ecl_char_code} and @code{ecl_base_char_code} |
| 452 |
perform a safe conversion, while @code{ECL_CHAR_CODE} doesn't check |
| 453 |
its argument. |
| 454 |
|
| 455 |
@code{ECL_CODE_CHAR} returns the lisp character associated to an integer |
| 456 |
code. It does not check its arguments. |
| 457 |
|
| 458 |
@itemize @bullet |
| 459 |
@item @strong{DEPRECATED} @code{CHAR_CODE} – equivalent to @code{ECL_CHAR_CODE} |
| 460 |
@item @strong{DEPRECATED} @code{CODE_CHAR} – equivalent to @code{ECL_CODE_CHAR} |
| 461 |
@end itemize |
| 462 |
@end deftypefun |
| 463 |
|
| 464 |
@cppdef ecl_char_eq |
| 465 |
@cppdef ecl_char_equal |
| 466 |
@deftypefun bool ecl_char_eq (cl_object x, cl_object y) |
| 467 |
@deftypefunx bool ecl_char_equal (cl_object x, cl_object y) |
| 468 |
Compare two characters for equality. char_eq take case into account and |
| 469 |
char_equal ignores it. |
| 470 |
@end deftypefun |
| 471 |
|
| 472 |
@cppdef ecl_char_cmp |
| 473 |
@cppdef ecl_char_compare |
| 474 |
@deftypefun int ecl_char_cmp (cl_object x, cl_object y) |
| 475 |
@deftypefunx int ecl_char_compare (cl_object x, cl_object y) |
| 476 |
Compare the relative order of two characters. @coderef{ecl_char_cmp} |
| 477 |
takes care of case and @coderef{ecl_char_compare} converts all |
| 478 |
characters to uppercase before comparing them. |
| 479 |
@end deftypefun |
| 480 |
|
| 481 |
@subheading Arrays |
| 482 |
|
| 483 |
An array is an aggregate of data of a common type, which can be accessed |
| 484 |
with one or more non-negative indices. ECL stores arrays as a C structure |
| 485 |
with a pointer to the region of memory which contains the actual |
| 486 |
data. The cell of an array datatype varies depending on whether it is a |
| 487 |
vector, a bit-vector, a multidimensional array or a string. |
| 488 |
|
| 489 |
@cppdef ECL_ADJUSTABLE_ARRAY_P |
| 490 |
@cppdef ECL_ARRAY_HAS_FILL_POINTER_P |
| 491 |
@deftypefun bool ECL_ADJUSTABLE_ARRAY_P (cl_object x) |
| 492 |
@deftypefunx bool ECL_ARRAY_HAS_FILL_POINTER_P (cl_object x) |
| 493 |
All arrays (arrays, strings and bit-vectors) may be tested for being |
| 494 |
adjustable and whenever they have a fill pointer with this two |
| 495 |
macros. They don't check the type of their arguments. |
| 496 |
@end deftypefun |
| 497 |
|
| 498 |
@cppdef ecl_vector |
| 499 |
@deftp @cind{} ecl_vector |
| 500 |
If @code{x} contains a vector, you can access the following fields: |
| 501 |
|
| 502 |
@table @code |
| 503 |
@item x->vector.elttype |
| 504 |
The type of the elements of the vector. |
| 505 |
@item x->vector.displaced |
| 506 |
List storing the vectors that @code{x} is displaced from and that x |
| 507 |
displaces to. |
| 508 |
@item x->vector.dim |
| 509 |
The maximum number of elements. |
| 510 |
@item x->vector.fillp |
| 511 |
Actual number of elements in the vector or @code{fill pointer}. |
| 512 |
@item x->vector.self |
| 513 |
Union of pointers of different types. You should choose the right |
| 514 |
pointer depending on @var{x->vector.elttype}. |
| 515 |
@end table |
| 516 |
@end deftp |
| 517 |
|
| 518 |
@cppdef ecl_array |
| 519 |
@deftp @cind{} ecl_array |
| 520 |
If @code{x} contains a multidimensional array, you can access the |
| 521 |
following fields: |
| 522 |
|
| 523 |
@table @code |
| 524 |
@item x->array.elttype |
| 525 |
The type of the elements of the array. |
| 526 |
@item x->array.rank |
| 527 |
The number of array dimensions. |
| 528 |
@item x->array.displaced |
| 529 |
List storing the arrays that @code{x} is displaced from and that x |
| 530 |
displaces to. |
| 531 |
@item x->array.dim |
| 532 |
The maximum number of elements. |
| 533 |
@item x->array.dims[] |
| 534 |
Array with the dimensions of the array. The elements range from |
| 535 |
@code{x->array.dim[0]} to @code{x->array.dim[x->array.rank-1]}. |
| 536 |
@item x->array.fillp |
| 537 |
Actual number of elements in the array or @code{fill pointer}. |
| 538 |
@item x->array.self |
| 539 |
Union of pointers of different types. You should choose the right |
| 540 |
pointer depending on @var{x->array.elttype}. |
| 541 |
@end table |
| 542 |
@end deftp |
| 543 |
|
| 544 |
@cppindex cl_elttype |
| 545 |
@deftp @cind{} cl_elttype ecl_aet_object ecl_aet_sf ecl_aet_df ecl_aet_lf ecl_aet_csf ecl_aet_cdf ecl_aet_clf ecl_aet_bit ecl_aet_fix ecl_aet_index ecl_aet_b8 ecl_aet_i8 ecl_aet_b16 ecl_aet_i16 ecl_aet_b32 ecl_aet_i32 ecl_aet_b64 ecl_aet_i64 ecl_aet_ch ecl_aet_bc |
| 546 |
|
| 547 |
Each array is of an specialized type which is the type of the elements |
| 548 |
of the array. ECL has arrays only a few following specialized types, and |
| 549 |
for each of these types there is a C integer which is the corresponding |
| 550 |
value of @code{x->array.elttype} or @code{x->vector.elttype}. We list |
| 551 |
some of those types together with the C constant that denotes that type: |
| 552 |
|
| 553 |
@table @var |
| 554 |
@item t |
| 555 |
@code{ecl_aet_object} |
| 556 |
@item single-float |
| 557 |
@code{ecl_aet_sf} |
| 558 |
@item double-float |
| 559 |
@code{ecl_aet_df} |
| 560 |
@item long-float |
| 561 |
@code{ecl_aet_lf} |
| 562 |
@item (COMPLEX SINGLE-FLOAT) |
| 563 |
@code{ecl_aet_csf} |
| 564 |
@item (COMPLEX DOUBLE-FLOAT) |
| 565 |
@code{ecl_aet_cdf} |
| 566 |
@item (COMPLEX LONG-FLOAT) |
| 567 |
@code{ecl_aet_clf} |
| 568 |
@item BIT |
| 569 |
@code{ecl_aet_bit} |
| 570 |
@item FIXNUM |
| 571 |
@code{ecl_aet_fix} |
| 572 |
@item INDEX |
| 573 |
@code{ecl_aet_index} |
| 574 |
@item CHARACTER |
| 575 |
@code{ecl_aet_ch} |
| 576 |
@item BASE-CHAR |
| 577 |
@code{ecl_aet_bc} |
| 578 |
@end table |
| 579 |
@end deftp |
| 580 |
|
| 581 |
@cppindex ecl_array_elttype |
| 582 |
@deftypefun cl_elttype ecl_array_elttype (cl_object array) |
| 583 |
Returns the element type of the array @code{o}, which can be a string, a |
| 584 |
bit-vector, vector, or a multidimensional array. |
| 585 |
|
| 586 |
@subsubheading Example |
| 587 |
@exindex @code{ecl_array_elttype} different types of objects |
| 588 |
For example, the code |
| 589 |
|
| 590 |
@example |
| 591 |
ecl_array_elttype(ecl_read_from_cstring("\"AAA\"")); /* returns ecl_aet_ch */ |
| 592 |
ecl_array_elttype(ecl_read_from_cstring("#(A B C)")); /* returns ecl_aet_object */ |
| 593 |
@end example |
| 594 |
@end deftypefun |
| 595 |
|
| 596 |
@cppindex ecl_aref |
| 597 |
@cppindex ecl_aset |
| 598 |
@deftypefun cl_object ecl_aref (cl_object x, cl_index index) |
| 599 |
@deftypefunx cl_object ecl_aset (cl_object x, cl_index index, cl_object value) |
| 600 |
These functions are used to retrieve and set the elements of an |
| 601 |
array. The elements are accessed with one index, index, as in the lisp |
| 602 |
function ROW-MAJOR-AREF. |
| 603 |
|
| 604 |
@subsubheading Example |
| 605 |
@exindex @code{ecl_aref} and @code{ecl_aset} accessing arrays |
| 606 |
@example |
| 607 |
cl_object array = ecl_read_from_cstring("#2A((1 2) (3 4))"); |
| 608 |
cl_object x = ecl_aref(array, 3); |
| 609 |
cl_print(1, x); /* Outputs 4 */ |
| 610 |
ecl_aset(array, 3, ecl_make_fixnum(5)); |
| 611 |
cl_print(1, array); /* Outputs #2A((1 2) (3 5)) */ |
| 612 |
@end example |
| 613 |
@end deftypefun |
| 614 |
|
| 615 |
@cppindex ecl_aref1 |
| 616 |
@cppindex ecl_aset1 |
| 617 |
@deftypefun cl_object ecl_aref1 (cl_object x, cl_index index) |
| 618 |
@deftypefunx cl_object ecl_aset1 (cl_object x, cl_index index, cl_object value) |
| 619 |
These functions are similar to @code{aref} and @code{aset}, but they operate on vectors. |
| 620 |
|
| 621 |
@subsubheading Example |
| 622 |
@exindex @code{ecl_aref1} and @code{ecl_aset1} accessing vectors |
| 623 |
@example |
| 624 |
cl_object array = ecl_read_from_cstring("#(1 2 3 4)"); |
| 625 |
cl_object x = ecl_aref1(array, 3); |
| 626 |
cl_print(1, x); /* Outputs 4 */ |
| 627 |
ecl_aset1(array, 3, ecl_make_fixnum(5)); |
| 628 |
cl_print(1, array); /* Outputs #(1 2 3 5) */ |
| 629 |
@end example |
| 630 |
@end deftypefun |
| 631 |
|
| 632 |
@subheading Strings |
| 633 |
|
| 634 |
A string, both in Common-Lisp and in ECL is nothing but a vector of |
| 635 |
characters. Therefore, almost everything mentioned in the section of |
| 636 |
arrays remains valid here. |
| 637 |
|
| 638 |
The only important difference is that ECL stores the base-strings |
| 639 |
(non-Unicode version of a string) as a lisp object with a pointer to a |
| 640 |
zero terminated C string. Thus, if a string has n characters, ECL will |
| 641 |
reserve n+1 bytes for the base-string. This allows us to pass the |
| 642 |
base-string self pointer to any C routine. |
| 643 |
|
| 644 |
@cppdef ecl_string |
| 645 |
@cppdef ecl_base_string |
| 646 |
@deftp @cind{} ecl_string |
| 647 |
@deftpx @cind{} ecl_base_string |
| 648 |
If @code{x} is a lisp object of type string or a base-string, we can |
| 649 |
access the following fields: |
| 650 |
@table @code |
| 651 |
@item x->string.dim x->base_string.dim |
| 652 |
Actual number of characters in the string. |
| 653 |
@item x->string.fillp x->base_string.fillp |
| 654 |
Actual number of characters in the string. |
| 655 |
@item x->string.self x->base_string.self |
| 656 |
Pointer to the characters (appropriately @code{ecl_character}'s and @code{ecl_base_char}'s). |
| 657 |
@end table |
| 658 |
@end deftp |
| 659 |
|
| 660 |
@cppdef ECL_EXTENDED_STRING_P |
| 661 |
@cppdef ECL_BASE_STRING_P |
| 662 |
@deftypefun bool ECL_EXTENDED_STRING_P (cl_object object) |
| 663 |
@deftypefunx bool ECL_BASE_STRING_P (cl_object object) |
| 664 |
|
| 665 |
Verifies if an objects is an extended or base string. If Unicode isn't |
| 666 |
supported, then @code{ECL_EXTENDED_STRING_P} always returns 0. |
| 667 |
@end deftypefun |
| 668 |
|
| 669 |
@subheading Bit-vectors |
| 670 |
|
| 671 |
Bit-vector operations are implemented in file |
| 672 |
@code{src/c/array.d}. Bit-vector shares the structure with a vector, |
| 673 |
therefore, almost everything mentioned in the section of arrays remains |
| 674 |
valid here. |
| 675 |
|
| 676 |
@subheading Streams |
| 677 |
|
| 678 |
Streams implementation is a broad topic. Most of the implementation is |
| 679 |
done in the file @code{src/c/file.d}. Stream handling may have different |
| 680 |
implementations referred by a member pointer @code{ops}. |
| 681 |
|
| 682 |
Additionally on top of that we have implemented @emph{Gray Streams} (in |
| 683 |
portable Common Lisp) in file @code{src/clos/streams.lsp}, which may be |
| 684 |
somewhat slower (we need to benchmark it!). This implementation is in a |
| 685 |
separate package @var{GRAY}. We may redefine functions in the |
| 686 |
@var{COMMON-LISP} package with a function @code{redefine-cl-functions} |
| 687 |
at run-time. |
| 688 |
|
| 689 |
@cppdef ecl_file_pos |
| 690 |
@deftp @cind{} ecl_file_ops write_* read_* unread_* peek_* listen clear_input clear_output finish_output force_output input_p output_p interactive_p element_type length get_position set_position column close |
| 691 |
@end deftp |
| 692 |
|
| 693 |
@cppdef ecl_stream |
| 694 |
@deftp @cind{} ecl_stream |
| 695 |
@table @code |
| 696 |
@item ecl_smmode mode |
| 697 |
Stream mode (in example @code{ecl_smm_string_input}). |
| 698 |
@item int closed |
| 699 |
Whenever stream is closed or not. |
| 700 |
@item ecl_file_ops *ops |
| 701 |
Pointer to the structure containing operation implementations (dispatch |
| 702 |
table). |
| 703 |
@item union file |
| 704 |
Union of ANSI C streams (FILE *stream) and POSIX files interface |
| 705 |
(cl_fixnum descriptor). |
| 706 |
@item cl_object object0, object1 |
| 707 |
Some objects (may be used for a specific implementation purposes). |
| 708 |
@item cl_object byte_stack |
| 709 |
Buffer for unread bytes. |
| 710 |
@item cl_index column |
| 711 |
File column. |
| 712 |
@item cl_fixnum last_char |
| 713 |
Last character read. |
| 714 |
@item cl_fixnum int0 int1 |
| 715 |
Some integers (may be used for a specific implementation purposes). |
| 716 |
@item cl_index byte_size |
| 717 |
Size of byte in binary streams. |
| 718 |
@item cl_fixnum last_op |
| 719 |
0: unknown, 1: reading, -1: writing |
| 720 |
@item char *buffer |
| 721 |
Buffer for FILE |
| 722 |
@item cl_object format |
| 723 |
external format |
| 724 |
@item cl_eformat_encoder encoder |
| 725 |
@item cl_eformat_encoder decoder |
| 726 |
@item cl_object format_table |
| 727 |
@item in flags |
| 728 |
Character table, flags, etc |
| 729 |
@item ecl_character eof_character |
| 730 |
@end table |
| 731 |
@end deftp |
| 732 |
|
| 733 |
@cppdef ECL_ANSI_STREAM_P |
| 734 |
@deftypefun bool ECL_ANSI_STREAM_P (cl_object o) |
| 735 |
Predicate determining if @code{o} is a first-class stream object. |
| 736 |
@end deftypefun |
| 737 |
|
| 738 |
@cppdef ECL_ANSI_STREAM_TYPE_P |
| 739 |
@deftypefun bool ECL_ANSI_STREAM_TYPE_P (cl_object o, ecl_smmode m) |
| 740 |
Predicate determining if @code{o} is a first-class stream object of |
| 741 |
type @code{m}. |
| 742 |
@end deftypefun |
| 743 |
|
| 744 |
@subheading Structures |
| 745 |
|
| 746 |
Structures and instances share the same datatype @code{t_instance} ( |
| 747 |
with a few exceptions. Structure implementation details are the file |
| 748 |
@code{src/c/structure.d}. |
| 749 |
|
| 750 |
@cppdef ECL_STRUCT_TYPE |
| 751 |
@cppdef ECL_STRUCT_SLOTS |
| 752 |
@cppdef ECL_STRUCT_LENGTH |
| 753 |
@cppdef ECL_STRUCT_SLOT |
| 754 |
@cppdef ECL_STRUCT_NAME |
| 755 |
@deftypefun cl_object ECL_STRUCT_TYPE (cl_object x) |
| 756 |
@deftypefunx cl_object ECL_STRUCT_SLOTS (cl_object x) |
| 757 |
@deftypefunx cl_object ECL_STRUCT_LENGTH (cl_object x) |
| 758 |
@deftypefunx cl_object ECL_STRUCT_SLOT (cl_object x, cl_index i) |
| 759 |
@deftypefunx cl_object ECL_STRUCT_NAME (cl_object x) |
| 760 |
Convenience functions for the structures. |
| 761 |
@end deftypefun |
| 762 |
|
| 763 |
@subheading Instances |
| 764 |
|
| 765 |
@cppdef ECL_CLASS_OF |
| 766 |
@cppdef ECL_SPEC_FLAG |
| 767 |
@cppdef ECL_SPEC_OBJECT |
| 768 |
@cppdef ECL_CLASS_NAME |
| 769 |
@cppdef ECL_CLASS_SUPERIORS |
| 770 |
@cppdef ECL_CLASS_INFERIORS |
| 771 |
@cppdef ECL_CLASS_SLOTS |
| 772 |
@cppdef ECL_CLASS_CPL |
| 773 |
@cppdef ECL_INSTANCEP |
| 774 |
|
| 775 |
@deftypefun cl_object ECL_CLASS_OF (cl_object x) |
| 776 |
@deftypefunx cl_object ECL_SPEC_FLAG (cl_object x) |
| 777 |
@deftypefunx cl_object ECL_SPEC_OBJECT (cl_object x) |
| 778 |
@deftypefunx cl_object ECL_CLASS_NAME (cl_object x) |
| 779 |
@deftypefunx cl_object ECL_CLASS_SUPERIORS (cl_object x) |
| 780 |
@deftypefunx cl_object ECL_CLASS_INFERIORS (cl_object x) |
| 781 |
@deftypefunx cl_object ECL_CLASS_SLOTS (cl_object x) |
| 782 |
@deftypefunx cl_object ECL_CLASS_CPL (cl_object x) |
| 783 |
@deftypefunx bool ECL_INSTANCEP (cl_object x) |
| 784 |
Convenience functions for the structures. |
| 785 |
@end deftypefun |
| 786 |
|
| 787 |
@subheading Bytecodes |
| 788 |
|
| 789 |
A bytecodes object is a lisp object with a piece of code that can be |
| 790 |
interpreted. The objects of type @code{t_bytecodes} are implicitly |
| 791 |
constructed by a call to eval, but can also be explicitly constructed |
| 792 |
with the @code{si_make_lambda} function. |
| 793 |
|
| 794 |
@cppdef si_safe_eval |
| 795 |
@cppdef cl_safe_eval |
| 796 |
@cppdef cl_eval |
| 797 |
@defun si:safe-eval form env &optional err-value |
| 798 |
@end defun |
| 799 |
@deftypefun cl_object si_safe_eval (cl_narg narg, cl_object form, cl_object env, ...) |
| 800 |
|
| 801 |
@code{si_safe_eval} evaluates @code{form} in the lexical |
| 802 |
environment@footnote{Note that @code{env} must be a lexical |
| 803 |
environment as used in the interpreter, @xref{The lexical |
| 804 |
environment}} @code{env}, which can be @code{ECL_NIL}. Before |
| 805 |
evaluating it, the expression form is bytecompiled. If the form |
| 806 |
signals an error, or tries to jump to an outer point, the function has |
| 807 |
two choices: by default, it will invoke a debugger, but if a third |
| 808 |
value is supplied, then @code{si_safe_eval} will not use a debugger |
| 809 |
but rather return that value. |
| 810 |
|
| 811 |
@itemize @bullet |
| 812 |
@item @strong{DEPRECATED} @code{cl_object cl_eval (cl_object form)} - |
| 813 |
@code{cl_eval} is the equivalent of @code{si_safe_eval} but without |
| 814 |
environment and with no @code{err-value} supplied. It exists only for |
| 815 |
compatibility with previous versions. |
| 816 |
@item @strong{DEPRECATED} @code{cl_object cl_safe_eval (cl_object form, cl_object env, cl_object err_value)} - |
| 817 |
Equivalent of @code{si_safe_eval}. |
| 818 |
@end itemize |
| 819 |
|
| 820 |
@subheading Example |
| 821 |
@exindex @code{si_safe_eval} |
| 822 |
@example |
| 823 |
cl_object form = ecl_read_from_cstring("(print 1)"); |
| 824 |
si_safe_eval(2, form, ECL_NIL); |
| 825 |
si_safe_eval(3, form, ECL_NIL, ecl_make_fixnum(3)); /* on error function will return 3 */ |
| 826 |
@end example |
| 827 |
@end deftypefun |
| 828 |
|
| 829 |
@cppdef si_make_lambda |
| 830 |
@deftypefun cl_object si_make_lambda (cl_object name, cl_object def) |
| 831 |
Builds an interpreted lisp function with name given by the symbol name |
| 832 |
and body given by @code{def}. |
| 833 |
|
| 834 |
@subheading Example |
| 835 |
@exindex @code{si_make_lambda} building functions |
| 836 |
|
| 837 |
For instance, we would achieve the equivalent of |
| 838 |
|
| 839 |
@lisp |
| 840 |
(funcall #'(lambda (x y) |
| 841 |
(block foo (+ x y))) |
| 842 |
1 2) |
| 843 |
@end lisp |
| 844 |
|
| 845 |
with the following code |
| 846 |
|
| 847 |
@example |
| 848 |
cl_object def = ecl_read_from_cstring("((x y) (+ x y))"); |
| 849 |
cl_object name = ecl_make_symbol("FOO", "COMMON-LISP-USER"); |
| 850 |
cl_object fun = si_make_lambda(name, def); |
| 851 |
return cl_funcall(3, fun, ecl_make_fixnum(1), ecl_make_fixnum(2)); |
| 852 |
@end example |
| 853 |
|
| 854 |
Notice that @code{si_make_lambda} performs a bytecodes compilation of |
| 855 |
the definition and thus it may signal some errors. Such errors are not |
| 856 |
handled by the routine itself so you might consider using |
| 857 |
@code{si_safe_eval} instead. |
| 858 |
@end deftypefun |