Calling Lisp Functions from D Language

Written by on December 25, 2021 in D Language, LISP, Programming with 0 Comments

After exploring “newLisp” in the past few posts, I would like to start looking at the “D Programming Language” (DLang).

DLang has been around since 2001. It was originally created by Walter Bright and later Andrei Alexandrescu joined the team in 2007. The main inspiration for DLang was C++, although it uses ideas from other languages such as Eiffel, Java and Python.

Incidentally, Walter Bright was also the creator of the famous Zortech C++ Compiler, which I had used in early 1990s.

The first thing I look for when I work with a new language is its interoperability with C/C++ (C is considered by many as the “universal assembly language”). In that respect, D scores high.

In today’s post, I would like to show how a program written in D can call a Lisp function. My example uses LispWorks Lisp  64 bit edition for Windows. For programming in D and building the project, I am using “VisualD” installed in Visual Studio 2017, Professional Edition, on Windows.

First, here is the simple Lisp program:

Lisp Function Called from D

Lisp Function Called from D

The program takes an “int64” parameter and returns another “int64” value. It uses Lisp’s built-in function “random” to generate a random number.

LispWorks Lisp has extensive support for Foreign Language Interface (FLI). Here is the script to build the 64-bit DLL:

Building Lisp DLL

Building Lisp DLL

Once the DLL is available, there are essentially two ways to use it:

1) Create a LIB file from the DLL and link it statically with D code

2) Load the DLL functions dynamically and call from D

I have chosen to go with the second approach because it is easier in this case.

Here is the D program that makes a call to the Lisp function:

The D Program

The D Program

In VS 2017, when you create a “New Project”, you need to select “D/VC++ Based Windows Application”.

Visual Studio Project Type

Visual Studio Project Type

Once the project is open, here is how the “Solution Explorer” looks:

Solution Explorer

Solution Explorer

When you build and run the project from within the IDE, the program prints the value received from the Lisp function. Because the Lisp function uses “random” internally, the result will be different each time you run the D program.

I did not face any challenge in interfacing D with my Lisp function in 64-bit mode. This is likely to be the case when interfacing D with any other language (e.g. Elixir, Go, etc.) that has a well-documented C language interface. 

You can download the sources here.

That is it for this article. Have a Merry Christmas and a Wonderful New Year 2022!

Tags: ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *