[Ecls-list] with-local-gmp
Juan Jose Garcia-Ripoll
worm at arrakis.es
Mon Feb 9 00:27:17 UTC 2004
On Saturday 07 February 2004 18:57, Erik Winkels wrote:
> Hi,
>
> It's been a while since I've done any real coding so please bear with
> my simple question: How do I use cl_write_object?
Why not use the lisp routines? cl_write_object is an internal routine,
which accidentally got the "cl_" prefix instead of the "ecl_" one (the
latter meaning: "warning! make sure you know what you're doing!")
The problem is that the lisp printer needs some auxiliary data structures
to be initialized: the pretty printter buffer, the hash of objects that
were printed in the case of *print-circle* = t, and many other values,
such as *print-base*, *print-pretty*, etc, are cached in the cl_env structure.
cl_write_object() assumes that you have properly initialized these values,
which basically amounts to calling cl_setup_printer (hey, this guy should
also have "ecl_" prefix!).
I do not like that way of working, and in a near future these auxiliary
data structures will disappear (probably a must if we want to import
CMUCL's pretty printer), so I strongly discourage you from using cl_write_object.
Instead, you have _all_ lisp functions available for C programming: cl_print,
cl_princ, cl_write, etc, and some even are in a more "compact" form which
does not check the arguments, princ, print, prin1, etc.
Finally, the lisp printer will always output to the *standard-output* stream
by default. Mixing calls to printf() and calls to the lisp printer is not
guaranteed to have the desired result. I would rather recommend using cl_format,
princ_str, princ_char, and the lisp printer, or write all lisp output to a string
and then use only the standard C writer.
A possible rewrite of your program (did not check whether it compiles)
#include "ecl.h"
#define MAX_STRING_LENGTH 80
int main (int argc, char *argv[])
{
cl_object form, result;
cl_boot(argc, argv);
while (1)
{
printc_str("? ", Cnil);
form = cl_read(0);
result = si_safe_eval(1, form);
if (type_of(result) == t_string) {
cl_format(3, Ct, make_constant_string("\"~A\"~%"), result);
} else {
princ_str("Output format not implemented. "
"Trying cl_write_object: ");
cl_princ(1, result);
}
}
return 0;
}
More information about the ecl-devel
mailing list