What do you think of the Lush C interface?
Edward C. Jones
edcjones at erols.com
Thu Jan 30 21:23:28 EST 2003
I have just stumbled across a language calles "lush". See
"http://lush.sourceforge.net/". What do you all think about the
internals of this language? About the CLush compiler? And the
relationship between Lush and C? Here is a section of the manual:
1.13. Calling C functions from Lush
The ability to freely mix Lisp and C code is one of the most
interesting features of Lush.
Let's say you have written a C file called titi.c with the
following content:
float sq(float x)
{
return x*x;
}
You have compiled the file and produced the object file titi.o.
Calling sq from the Lush interpreter is as simple as the
following. First, dynamically load the object file into Lush
? (mod-load "titi.o")
then, write a lisp function whose only purpose is to call sq.
? (de square (x) ((-float-) x)
(cpheader "extern float sq(float);")
(to-float #{ sq( $x ) #} ))
Here is what the above means: (de square (x) ...) means define a
new Lisp function called square with a single argument x.
((-float-) x) simply declares x as float. (to-float ....) converts
its argument to float (like a cast in C). The sequence #{ .... #}
allows to insert C code within Lisp code. Therefore #{ sq( $x ) #}
simply calls the C function sq and returns the result. Lisp
variables can be inserted in in-line C code by prepending a Dollar
sign, thus $x refers to the Lisp float variable x. The cheader
directive allows one to include a string in the "header" section
of the C files generated by the Lisp to C compiler. We use it here
to specify the type of the argument and the return value of sq.
We can now compile the above function using:
? (dhc-make () square)
Two file C/square.c and C/i686-pc-linux-gnu/square.o will be
generated with the C source and the object code for square. The
object code will be automatically loaded into Lush. Now square can
be called from the Lisp interpreter:
? (square 5)
= 25
In the above example, the to-float casting function was used to
tell the compiler what type the returned value has. The following
"cast" functions are available: to-float, to-int, to-double,
to-bool, to-gptr. These functions cast their argument to the
corresponding type.
More information about the Python-list
mailing list