3

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.

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
Dario Castañé
  • 1,813
  • 3
  • 15
  • 20
  • Thanks for your comments, I am going to check unanswered ones as soon as possible, because they need a more deeper look than others. – Dario Castañé Oct 20 '09 at 22:03
  • Thanks again to everybody. I choose Lua due its truly embeddable way to work and Qt bindings, although QtScript/V8 were good options too. Python looks too hard to embed for me now. – Dario Castañé Oct 21 '09 at 17:32

8 Answers8

3

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
  • 249,864
  • 45
  • 407
  • 398
  • 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! – Dario Castañé Oct 21 '09 at 17:30
1

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
  • 41,583
  • 10
  • 86
  • 166
  • 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. – Dario Castañé Oct 20 '09 at 22:00
1

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

Karl Voigtland
  • 7,637
  • 34
  • 29
1

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
  • 571
  • 1
  • 3
  • 4
0

I've never tried it but there is PyQt.

Troubadour
  • 13,334
  • 2
  • 38
  • 57
  • 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. – Dario Castañé Oct 20 '09 at 21:57
0

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

Maciek
  • 19,435
  • 18
  • 63
  • 87
0

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
  • 14,338
  • 5
  • 58
  • 73
  • 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? – Dario Castañé Oct 20 '09 at 21:54
0

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.

Glenn
  • 7,874
  • 3
  • 29
  • 38