tags:

views:

154

answers:

7

I'd like to add some degree of "scriptability" to a application I'm writing. Roughly speaking, I'd like a way to embed a programming language API into my program. I've looked at TinyPy, but I'm worried that - given it's small size - it might be deceptively limiting (i.e. it looks and feels like Python, but is lacking many of the features of full-fledged Python).

I'm not looking to write my own programming language; I'd like to simply take a relatively well-known language that users can write functions in, and then be able to call those functions/pass those functions parameters from my application, which will be written in C.

I'd also like to be access any data types those functions return back to me, also from within C.

Does anyone have any suggestions as to an API/Library to use, which would be both lightweight, relatively full-featured, and with a relatively easy to use or easy to understand C library?

Thanks!

+10  A: 

Lua is a popular choice.

Ignacio Vazquez-Abrams
+4  A: 

You might want to look at SpiderMonkey, which will allow you to embed a Javascript engine in your C program.

JacobM
This is awesome, thanks! Started glancing thru the JSAPI documentation, and it's quite good!
FreeMemory
+2  A: 

Most scripting languages have good interfaces with C. The ones I've actually glanced at are Perl, Python, and Lua. If your users aren't serious programmers, I'd advise giving Perl a miss. If they're serious World of Warcraft players, they might already be familiar with Lua, and writing Lua code to interface with a compiled program, so that may be a good choice.

David Thornley
+1  A: 

YOu can always use 'c' with the ch toolkit.
As you said python is good especially if you can use the boost::python wrapper.
Another popular choice is Javascript, it's used by Qt's scripting system

Martin Beckett
C, not C++. No boost.
Nathon
A: 

The most common well known language is javascript. There are a few good and simple (well not really as simple as possible if you compare it with Lua for example) to embedd Javascript interpreters out there.

You haven't said anything about external libraries. Like Lua these javascript systems are naked. There is not much provided for communication with the outside word. That is in contrast to ruby and python where you get a batteries included system with the zillions of functions from the runtime library

Start with reading this:

Why embed JavaScript?

How to embed the JavaScript engine

Lothar
Heavy run-time library makes a language less usable for embedding, because during deployment one has to count not only dependencies of the application itself, but also dependencies of the embedded scripting language. And also all the compatibility problems when target system already has the language installed.
Dummy00001
+1  A: 

Lua and slang were both invented just for this. Lua has some neat features to sandbox code, so if users can provide code you can attempt to limit their ability to shoot themselves in the foot.

Flavors of lisp have been used for this (famously in gnu emacs).

You really need to think about what types of things the embedded language will be used for and then think about the type of languages that you might like to write that functionality in, read code that others had written in, and easily satisfy yourself that the code was correct, or at least easily debug it.

nategoose
+1  A: 

GUILE is GNU's Ubiquitous Intelligent Language for Extension. It is a Scheme dialect (LISP dialect). Before you scream about LISP, consider that AutoCAD used LISP as the extension language, and at least one major secretarial word-processing system used it, and the secretaries had no trouble at all writing extensions for their particular tasks. (It seems that people don't realize programming is supposed to be hard if you don't tell them they're doing programming.)

John R. Strohm