SWIG interface to lisp FFI's

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages.
CLISP is a feature-loaded implementation of a great language (lisp) !
If you are looking for the hacks and documentation for CLISP then they are available in the latest SWIG release.

The SWIG CFFI module can now be downloaded from SWIG CVS repository. It supports C++, but I have still not added support for function overloading, it may work but the interface may change in future.
These are some of the options which this module may support.

To run it:
swig -cffi interface-file-name

The interface file is not necessary but it gives more power, if you want you can directly feed the source file. To do that just do
swig -cffi -module module-name C-file-name

Sample interface file (test.i)

%module test

%typemap(cin) struct cfunr* ":my-struct";

%insert("lisphead") %{
;;SWIG example
 (in-package :test)
%}
%include "test.cpp"
You can create your own typemaps if you don't want to use the default ones. `%typemap(cin) struct cfunr* ":my-struct";' creates one such typemap. The word `cín' is for typemap which are input to C functions, while `cout' is for typemaps returned by C functions. The `%insert("lisphead") %{…' allows things to be included verbatim into the genreated lisp wrapper file.

Sample c - file (test.cpp)

typedef long int real;

#define max 1000

extern "C"
{
void  acbdsqr_ (int xyz[][]);

  enum {ALE, LAGER, PORTER, STOUT};

struct bar {
    short x, y;
    char a, b;
    int *z[1000];
    struct bar * n;
};
  
struct   bar * my_struct;

struct foo {
    int a;
    struct foo * b[100];
  
};



int pointer_func(void (*ClosureFun)( void* _fun, void* _data, void* _evt ), int y);

typedef struct {
  int quot;   /* Quotient */
  int rem;    /* Remainder */
} div_t;

int func123(div_t * x,int **z[100],int y[][1000][10]);

void func1234(float ***x);


struct cfunr { int x; 
  char *s; };

struct cfunr * cfun (int i,char *s,struct cfunr * r,int a[10]) {
  int j;
  struct cfunr * r2;
  printf("i = %d\n", i);
  printf("s = %s\n", s);
  printf("r->x = %d\n", r->x);
  printf("r->s = %s\n", r->s);
  for (j = 0; j < 10; j++) printf("a[%d] = %d.\n", j, a[j]);
  r2 = (struct cfunr *) malloc (sizeof (struct cfunr));
  r2->x = i+5;
  r2->s = "A C string";
  return r2;
}

void* lispsort_function(int);


void lispsort_double (int n, double * array) {
    double * sorted_array;
    int i;
    lispsort_begin(n); /* store #'sort2 in lispsort_function */
    sorted_array = ((double * (*) (double *)) lispsort_function) (array);
    for (i = 0; i < n; i++) array[i] = sorted_array[i];
    free(sorted_array);
}
void test(float x , double y);

}

The output lisp file


;;SWIG example
 (in-package :test)


(defconstant max 1000)

(defcfun ("acbdsqr_" acbdsqr_) :void
  (xyz :pointer))

(defcenum 
	:ALE
	:LAGER
	:PORTER
	:STOUT)

(defcstruct bar
	(x :short)
	(y :short)
	(a :char)
	(b :char)
	(z :int)
	(n :pointer))

(defcstruct foo
	(a :int)
	(b :pointer))

(defcfun ("pointer_func" pointer_func) :int
  (ClosureFun :pointer)
  (y :int))

(defcstruct div_t
	(quot :int)
	(rem :int))

(defcfun ("func123" func123) :int
  (x :pointer)
  (z :pointer)
  (y :pointer))

(defcfun ("func1234" func1234) :void
  (x :pointer))

(defcstruct cfunr
	(x :int)
	(s :char))

(defcfun ("cfun" cfun) :pointer
  (i :int)
  (s :string)
  (r :my-struct)
  (a :pointer))

(defcfun ("lispsort_function" lispsort_function) :pointer
  (arg0 :int))

(defcfun ("lispsort_double" lispsort_double) :void
  (n :int)
  (array :pointer))

(defcfun ("test" test) :void
  (x :float)
  (y :double))


ssinghi AT kreeti DOT com
Kreeti Technologies
Viewable With Any
Browser