Interfacing with Other Software

Writing Libraries/FFI's, calling external programs, and communicating with the outside world
The intention of PicoLisp is to avoid writing extensive libraries, and rather use existing libraries implemented in other languages. For that, accessing external programs and libraries is made especially easy and transparent in PicoLisp. There are many ways a PicoLisp program can be integrated with other software.

Call existing library functions in C

Call C functions directly from Lisp code, manipulate C data structures in memory, and even call Lisp functions from C, all this available interactively at the REPL. Native C Calls describes the powerful 'native' function in detail.

For a trivial example, see the Truncate a File task on Rosetta Code. Other simple examples include Regular Expressions, Encode a string (MD5), and connecting to an LDAP server.

To highlight a typical usecase, we call C library functions for computing Fast Fourier Transforms.

For extensive examples of 'native' libraries, have a look at Alex Williams PicoLisp Libraries. Each repo contains a great explanation of the code in the EXPLAIN.md file. These should be considered required reading for anyone interested in creating their own FFI libraries and API's.

in Java

The Java Interoperability article covers all aspects of this process in detail. Examples included!

in other languages

And there's no reason similar mechanisms couldn't be created for other languages; we just haven't had the need yet. But if you're really missing some (e.g. Python) functions, we can certainly point you in the right direction.

Write inline C (or Java) code

The 32-bit and 64-bit versions of PicoLisp behave a little different here. In the 32-bit version, these functions must contain glue code to convert between C and Lisp data. The file "misc/crc.l" contains an example for both versions.

For a simple example in C, see the Call a Foreign Language Function task on Rosetta Code. See the Call a Function in a Shared Library task for an example of the difference between the 32- and 64-bit version.

For an extensive example that highlights the difference between inline and external C, see Greg Lee's floating point math library.

As a proof of concept, Tomas Hlavaty wrote a simple FFI generator that makes it easier to wrap C libraries for 32-bit and miniPicoLisp.

For a simple example in Java, see the 'Inline Java Code' section of the Java Interoperability article.

Write a shared object file (DLL)

This is analog in the 32-bit and 64-bit versions, but due to the underlying implementation such shared object files are written in C for the 32-bit version, and in assembly for the 64-bit version.

Examples are the 'ext' and 'ht' libraries, built in the Makefile's ("src/ext.c", "src/ht.c", "src64/ext.l" and "src64/ht.l").

Start other programs as sub-processes, and communicate with their standard I/O via pipes.

This can be done with the 'in', 'out' and 'pipe' functions. On Rosetta Code you can find examples where external programs called from PicoLisp: The function 'call' can be used for this as well. See (doc 'call) for details/examples.

Use sockets or some other type of IPC

Nanomsg or ZeroMQ come to mind. We have Nanomsg bindings for PicoLisp already. And you could write a ZeroMQ wrapper without too much hassle, as it exports C language bindings that could be used with the 'native' function. You should then be able to converse freely with any other language that has bindings for your library of choice.

And finally, if you want access to PicoLisp from somewhere else, you might...

Write a PicoLisp program/script and call it from another language

Calling a PicoLisp function from another program requires a running interpreter. It's relatively efficient, as the interpreter's startup time is quite short. This Rosetta Code task shows how PicoLisp might be called from C.

Or maybe you could do something like this.

https://picolisp.com/wiki/?interfacing

24mar16   erik