Question

I would like to write a "simple" programming language which can call functions written in C. For example, I would like to integrate the language with the socket library.

What is the easiest way to call C functions from a custom programming language?


Note: This is similar to How do multiple-languages interact in one project?, but I hope that this question focuses more on the easiest way to design a language for interoperability.

Was it helpful?

Solution

1. The hard, universal and "write once, run everywhere" approach:

Your programming language enforces the user to somehow provide declarations of C functions to be used (as well as their implementation, most probably as a dynamically linkable/loadable library). Your programming language parser/compiler/interpreter interprets the function signature, and possibly uses a foreign function interface library, such as libFFI, to make calls that obey the ABI and C calling convention of the platform. This way one can call any C function from within your language without writing bindings.

This method is used in a few programming languages, for example PyObjC is a library that makes it possible for Python programmers to directly call Objective-C APIs from Python.

2. The easy, not-even-close-to-universal and "write as many times as you want to use it" way:

You require the user of your programming language to write the C extension functions with a signature specified beforehands, i. e. all programming language extensions should look like

GenericValueType *extension_function(int argc, GenericValueType **argv);

and then whoever wants to connect some other code to your programming language has to write bindings using a function and the dedicated API of your programming language. For example, if you wanted to use the strlen() function from the C standard library, you would write this (pseudoo-)code:

GenericValueType *my_ext_strlen(int argc, GenericValueType **argv)
{
    GenericValueType *arg1 = argv[0];
    const char *input = ValueToCString(arg1);
    size_t len = strlen(input);
    return NewValueFromInteger(len);
}

This is easier to implement for the creator of the language, but as I mentioned it before, it's not universal - every function has to be bound/ported to be compatible. This is the approach that most scripting languages take, for example Python, PHP and Lua require bindings to external libraries to be written in this way.

OTHER TIPS

This answer might seem too obvious, but it's the truth: the languages can call functions in C by generating (somehow) and executing the same machine instructions that a C program would use to call the same function. If your new language has a compiler that emits machine code, it could actually be designed to use the same calling conventions as C for everything, so that not only could it easily call C functions, but C code could easily call functions written in your language.

Here is a Wiki page containing information on the standard mechanisms used to call functions in C (and other languages) on x86 processors; a little research will turn up similar conventions for other processors and architectures of interest to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top