views:

256

answers:

2

I have no experience with llvm or clang, yet. From what I read clang is said to be easily embeddable Wikipedia-Clang, however, I did not find any tutorials about how to achieve this. So is it possible to provide the user of a c++ application with scripting-powers by JIT compiling and executing user-defined code at runtime? Would it be possible to call the applications own classes and methods and share objects?

edit: I'd prefer a C-like syntax for the script-languge (or even C++ itself)

+1  A: 

I don't know of any tutorial, but there is an example C interpreter in the Clang source that might be helpful. You can find it here: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/clang-interpreter/

You probably won't have much of a choice of syntax for your scripting language if you go this route. Clang only parses C, C++, and Objective C. If you want any variations, you may have your work cut out for you.

Evan Shaw
Thank you. It took me quite a while to get it up and running - basically because I didn't find the executable :) Now after looking into it I'd say it is half-way there. Now I need to figure out how to call functions that are defined in the non-jitted part. ... any ideas about that?
FFox
I'm not sure what you mean. You should be able to get a pointer to any function that you've compiled. Maybe this general LLVM tutorial would be helpful: http://llvm.org/docs/tutorial/ (chapter 4, in particular). I don't expect that you'll be able to use this interpreter as it is, but it should give you an idea of how to embed a C/C++ interpreter in your application. (I haven't even used it myself; I just remembered seeing it.)
Evan Shaw
Ok here is what I did: http://lists.cs.uiuc.edu/pipermail/cfe-dev/2010-July/009836.html ... and it actually worked out with the help of the nice people from the cfe-dev mailing list.Why do you think this interpreter cannot be used in an app?
FFox
I just figured you'd need to make some customizations or just general changes. If you don't have to, more power to you.
Evan Shaw
A: 

I don't know about Clang but you might want to look at Ch:

http://www.softintegration.com/

This is described as an embeddable or stand-alone c/c++ interpreter. There is a Dr. Dobbs article with examples of embedding it here:

http://www.drdobbs.com/architecture-and-design/212201774

I haven't done more than play with it but it seems to be a stable and mature product. It's commercial, closed-source, but the "standard" version is described as free for both personal and commercial use. However, looking at the license it seems that "commercial" may only include internal company use, not embedding in a product that is then sold or distributed. (I'm not a lawyer, so clearly one should check with SoftIntegration to be certain of the license terms.)

c-urchin