Embedded scripting engine for DSL - c++

I'm working on a project which needs an embedded DSL to fullfill its expected requirements.
The DSL would be user defined event based. Here goes a mockup of the desired syntax:
user-defined-event-1 {
// event body
}
user-defined-event-2 {
// event body
}
Probably, most similar language I know based on events is LSL (from Second Life).
So, after reading other similar questions on SO, I would like to ask for the best embeddable scripting engine (Ruby, Lua, Python, etc) on C++ (I work in Qt) which allows me to create this DSL.
In my project, I would test that the script properly uses the DSL syntax (at least one event defined) and give the user all the power of the underlying scripting engine and, if possible, Qt.
It is not a requirement for the embedded language to work with Qt. It can be isolated, but it would be nice to have some integration too.

There's at least a few Qt-Lua bindings out there. Lua can somewhat do the syntax you've shown above; specifically, {} indicates a table (associative array) in Lua, and if you are only passing an anonymous table to a function, you don't need parentheses:
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> function LengthOfTable(t) print(#t) end
> LengthOfTable ({"a","b","c"})
3
> LengthOfTable {"a","b","c"}
3
Whether Lua is actually the best for your application, depends on your application, of course. Either way, Lua is very easy (IMO) to embed in C or C++.

You could look at embeddable javascript, through Google's V8 project, which is written in C++.
http://code.google.com/apis/v8/intro.html

Qt comes with the QtScript scripting module. It uses an ECMAScript based langauge (like javascript).

Tcl comes fairly close to your proposed syntax:
proc user-defined-event-1 {} {
# event body
puts "Hello World"
}
proc defines a procedure, and the extra {} braces are used for arguments. In a tcl shell, procedures can be dynamically typed in line-by-line, copied and pasted, or loaded from a file. They can also be redefined by simply reloading them.

I've never tried it but there is PyQt.

I believe boost::python is pretty easy to implement. I hear there are some python-Qt solutions too.

You seem to have very specific requirements for picking a generic DSL. You may want to try a generic DSL library (e.g. Boost.Proto) rather than a prexisting-embedded language.

For embedding a DSL within your app, I recommend ANTLR. I have used ANTLR over the years, the latest being within a JDBC driver for Cassandra. You might want to try version 4 which has a C++ runtime. Version 3 was problematic with Qt over a collision with the keyword emit.

Related

Can I script a C++ application with Ruby like with Lua?

I've just started to read about Ruby, and I was wondering if it can be embedded in a C++ application like Lua which provides a library to parse a given script file: https://stackoverflow.com/a/5127294/399107
Yes, you can. You just need to embed a Ruby engine in your application.
Note that, unlike the main Lua engine, some Ruby engines aren't really that well suited to being embedded into other programs. But, for example, Rubinius, IronRuby and JRuby have been specifically designed with embedding in mind, and even though it isn't pretty, you can embed YARV or MRI as well, even though they are not designed for it.
There's also MRuby, but unlike the others, it doesn't implement the full Ruby Language Specification, it only implements a subset of the ISO Ruby Specification which itself is only a small subset of the intersection of Ruby 1.8 and Ruby 1.9. Plus, it hasn't been released yet, as is evidenced by the fact that not even its homepage exists yet. It is, however, specifically designed for embedding, in both senses of the word: being embedded into other programs, and being useful on an embedded device with very little RAM.
As you may have noticed, it is much easier to embed Ruby into your app if the app is running on the Java platform or the CLI. There are C++ compilers for both the Java platform and the CLI, so that option is not entirely out of the question. Otherwise, I'd say that Rubinius is easier to embed, but more people have tried embedding YARV, so there are more blog posts about how to do that. (Or maybe, embedding Rubinius is so trivial nobody needs to write blog posts about it.)
A long time ago, someone was working on an implementation of Ruby for the Lua VM, but that implementation never went anywhere. Would solve all your problems, though :-)
Sure you can. It's possible with with SWIG, or you can make your own bindings for it (or google to see if someone has already done the work). The big question is do you really want to? The ruby interpreter is pretty heavy, and the interface isn't very nice.
Matz is working on an embeddable version of Ruby called mruby, which strives to be as easy to embed and as light as Lua. But its still alpha quality.
Yes, it's possible. Most of the standard libraries types are written in C. And when you can use C, you can use C++ too. Use extern "C" declared functions to get the right binding. I had a lot of trouble, when using a C++ compiler that was different (different version) from the compiler that was used to compile the ruby interpreter.
Here is the part of the pick axt book, that covers the ruby extension library: http://media.pragprog.com/titles/ruby3/ext_ruby.pdf
In an open source C++ web server project, I wrote a ruby / rack adapter, to use the server with rails: https://github.com/TorstenRobitzki/Sioux/tree/master/source/rack

Doxygen for a multi-language API

My company develops a API for engineering programs. It is developed in C++ but we create wrappers to it for the following languages:
a proprietary language that resembles VB
MATLAB
and Python.
Currently the documentation is generated by a bunch of scripts and it is starting to get time consuming to keep it 100%. I was wondering if there is a way to get doxygen/sphinx or another program to generate the documentation for C/C++, VB and MATLAB in one go. The Python part is done by SWIG. Currently the output is something like:
NameOfFunction
VB:
Function NameOfFunction(ByVal a As Long , ByRef b() As Long, ByVal c As Long) As Long
MATLAB:
value = NameOfFunction(a,b(),c)
C/C++:
value *NameOfFunction(objtype1 *a, objecttype2 *b[], int c)
+Description
+Examples
doxygen supports multiple languages,
to use this on multiple projects or folders and languages you just point doxygen to every folder you want checked in the config.
My favorite programming language is X. Can I still use doxygen?
No, not as such; doxygen needs to understand the structure of what it reads. If you don't mind spending some time on it, there are several options:
Is language X supported?
If the grammar of X is close to C or C++, then it is probably not too
hard to tweak src/scanner.l a bit so the language is supported. This
is done for all other languages directly supported by doxygen (i.e.
Java, IDL, C#, PHP). If the grammar of X is somewhat different than
you can write an input filter that translates X into something similar
enough to C/C++ for doxygen to understand (this approach is taken for
VB, Object Pascal, and Javascript, see
http://www.doxygen.nl/download.html#helpers). If the
grammar is completely different one could write a parser for X and
write a backend that produces a similar syntax tree as is done by
src/scanner.l (and also by src/tagreader.cpp while reading tag files).
however since your using a VB like proprietary , it might pick this language up if you simply just change the file extensions to that of .vb
here is how to use it with matlab
python and c++ are already supported
have a look at the FAQ page
I was wondering if there is a way to get doxygen/sphinx or another program to generate the documentation for c/c++, VB and matlab on one go.
The Zeus editor has a doxygen feature that allows you to create doxygen documentation for all items in the Zeus workspace.
The Doxygen site has a page with links to 3rd-party helpers for various languages, that you may be able to adapt:
http://www.doxygen.nl/helpers.html
VB and MatLab filters are mentioned there.

Wanted: Compiler Tool for Users of Software System

I am not sure if the title of this question gets to the point. I have written a large software system in C C++ for Windows, and want to give the users of this system the option to add compiled code to it. The user should be able to do basic stuff, and exchange data with my program.
Currently the implemented way is via DLLs. But for this, a grown up compiler is needed, and it is not as easy as I wished. Is there a tiny C compiler that can create Windows DLLs?
Another idea is the Java native interface. But this requires a complete Java system to run in the background, and it is not easy to run code in it.
Do you have any other ideas?
Any interpreted language? (TCL and Lua were designed as extension languages, but you can nearly as easily interface with any other).
How about python integration?
You could create an python interface that interfaces with your application. Python is rather easy to learn and should integrate easily with c/c++. The python documentation has an own chapter on that.
You could also use a tool like swig to generate the interface.
The advantage of this is that they wouldn't have to compile anything. They could just supply python files that could be loaded into your application and run within the software. This is a well known use for python, and something its simple syntax promotes.
As the other says you will be best of by providing an embedded language.
I would chip in for javascript and use the google v8 engine
By using javascript you get a language nearly everbody can use and program in.
There is other javascript engines you can embed like SpiderMonkey.
See this answer for what to choose.
An interpreted language is not good enough. I need speed. The software itself is an interpreted language. So I added support for the tiny C compiler. It is only C, and I do check mingw, which probably would not be as tiny as this. Thanks for all your hints.
Added after several months:
I have now two tools, actually: TinyC and Python. The speed difference between those is noticable (factor 5-10), but that usually does not matter too much. Python is much easier for the user, though I managed to integrate both into the Euler GUI quite nicely.
One of the ways is to add scripting. You application can host scripting environment and expose its services there. Users would be able to execute JScript/VBScript scripts and interact with your application. The advantage is that with reasonable effort you can get the power of well known and well documented scripting languages into your application (I suppose there is even debugger for scripting there). You will be required to shape your app services as COM interfaces and scripts will be able to access them automatically using method names you assigned on C++ side.
C++, Win32 and Scripting: Quick way to add Scripting support to your applications
MSDN Entry Point - IActiveScript interface

Extending a C++ application with embedded scripting

I am developing a C++ application that needs to be multiple platform compatible (Windows/Linux) and want to grant users the ability to extend the software to fit their needs exactly without allowing them to change critical parts of the application (so I do not want them in the C++ code).
What I am looking for is to embed a scripting language (I would prefer Python because I am familiar with it already, but it's not mandatory), so scripts put in some plugin folder can manipulate objects of the application if I want these objects to be modifyable.
The most simple example: if someone wants to build their own UI for my application, they should be able to do so with such a script.
The problem however is, that I've never put C++ and any kind of external scripts together, so I really do not know how to start. After looking for material to get started, I found that Lua claims to be a good language to do that, but I could not find good beginner tutorials.
I would really appreciate if someone knew a good place to start, be it online resources, or a good book. I wouldn't mind spending a few bucks on a good book.
As a learner, I tend to learn best from a mix of example code and a few lines explaining those.
I would suggest you to read Programming in Lua, this book has an entire section on how to embed Lua in C (and C++).
It is very highly rated by Amazon users.
The language has also pretty good online documentation and a active mailing list.
If you want to use Python I would definitely suggest using Boost.Python. It is an incredibly well designed library. Just an example: all you have to do to expose a C++ class to Python is this:
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
It handles almost everything automatically: conversions between types, exceptions, it even allows you to use reference counted objects between the two languages with boost::shared_ptr.
The article here at linux journal is a good place to start on how to embed a python interpreter in your c/c++ code. This is only half the battle however because when the interpreter is embedded, you then need to publish some part of your software to the scripting environment. The basic API to do so is in C and if most of your code is C++, it might be best to use boost::python since writing C wrappers around your C++ classes might be cumbersome. You can also use Py++ to generate the boost::python binding.
If you only want to use scripting as a door to customization and you can live with the memory footprint of python, it might be a better choice than Lua. Lua is usually good for small environment like game development. There is also a lot more python developers than lua developers as well as more built-ins and third party libraries available.
for Python, I guess the boost library is meant to do it. As for Lua, I haven't used it myself, but a quick Google search first led me to debian admin and then to Lua's C interface. Have you looked into those?

Lisp Interpreter in a C++ Program

I'm not sure I'm phrasing this right, but I'm pretty sure I'm looking for a LISP interpreter I can put in my C++ program.
The ideal situation I'm imagining is a function or something to which I can pass either a string, file, or filename containing the LISP code and then use the output from the LISP code in other parts.
To put it in terms of (print (eval (read))), I want read to be something I input to be what's read, and a string or something I can parse from print.
There is also ECL ("Embeddable Common Lisp"). It has the advantage, that it provides the full Common Lisp standard. Unfortunately, the documentation with respect to embedding is... well... a little bit scarce.
I never used it myself, so I cannot really tell, whether this would actually be an easy thing to embed into your application. IMHO, the Guile interpreter would be a reasonable choice.
Another embeddable Lisp is Rep, which is, for example, the extension language used by the Sawfish window manager. It started as Emacs lisp clone, but became something different over time, and is nowadays closer to Scheme.
If you're interested in Common Lisp, there's ECL.
From the FAQ:
2.1 What does this "embedding" stuff mean?
ECL is a full fledge
implementation of the Common-Lisp
language. However, due to the way it
is implemented, the implementation can
be used as an extensibility language
for your own application, much like
Guile works for the Scheme language.
By a rather simple set of functions,
you can parse, compile and execute
Common-Lisp forms, and using the
Foreign Function Interface (FFI), you
can add new functions to Common-Lisp
which suit your Domain-Specific
Language.
And:
2.2 How do I embed ECL into my application?
You have to use the ECL library, which
is called libecl.so, libecl.dyld or
ecl.dll, depending on your operating
system (Unix, Mac OSX or Windows). The
program ecl-config will provide you
with the flags you have to pass to the
C/C++ compiler and to the linker,
using either ecl-config --cflags or
ecl-config --ldflags, respectively.
Regarding your program, apart from
linking against the ECL library, you
have to call the proper initialization
routine, cl_boot(), so that ECL sets
up the appropiate internal structures.
After calling this routine, you will
be able to run lisp code and create or
manipulate lisp data
.
It's not Lisp, but Guile is the GNU cross platform scripting language, and is an interpreter/compiler for Scheme, which is pretty close to Lisp.
You can use GNU Guile, which is a Scheme interpreter specifically designed to be easily embeddable in C/C++ programs.
Try anything that comes up under this search: http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=embedded+lisp
It turns out that a lot of people have wanted to do this.
For something even more embeddable than Guile, there's also Chibi.
(I <3 Guile BTW, so this answer isn't trying to take away from that---it just offers another option.)