views:

475

answers:

7

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.

+1  A: 

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++.

Mark Rushakoff
Now I understand why Lua is a popular choice in game scripting. It looks very powerful and, as you said, very easy to embed (this is a wondeful example: http://cc.byexamples.com/20080607/how-to-embed-lua-51-in-c/). Also it works crossplatform and have a familiar syntax (at first glance, similar to Ruby, but I know that it is not) so I am not losing any feature adopting it in my project. Thanks!
I'm Dario
A: 

I've never tried it but there is PyQt.

Troubadour
Thanks but PyQt is just a binding upon Qt libraries. I need to embed a scripting engine in a C++ project which allows to build a DSL on it and, if possible, have bindings with Qt. Maybe I can embed python and use PyQt inside.
I'm Dario
A: 

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

Maciek
A: 

You could look at embeddable javascript, through Google's V8 project, which is written in C++.

http://code.google.com/apis/v8/intro.html

James Black
That's a good point too but I'm in the same way as QtScript. I guess I can build a DSL on Java/ECMAscript and provide it as a mandatory library.
I'm Dario
A: 

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.

coppro
Very interesting option but I guess that the created DSL is going to miss any basic feature (conditional flow, for example) provided for a preexisting embedded language unless I implement it on purpose. Am I right?
I'm Dario
+1  A: 

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

Karl Voigtland
Before asking I took a look to QtScript but I did not find a clue to build a DSL upon it.
I'm Dario
If you google 'javascript dsl' you may be able to find some useful information.
Karl Voigtland
A: 

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.

Joe Internet
I found out some info about DSL building on TCL, but Lua's looks better. Thanks anyway!
I'm Dario