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.

2
  • Thanks for your comments, I am going to check unanswered ones as soon as possible, because they need a more deeper look than others. Commented Oct 20, 2009 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. Commented Oct 21, 2009 at 17:32

8 Answers 8

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

Sign up to request clarification or add additional context in comments.

1 Comment

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: 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!
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

1 Comment

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

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

2 Comments

Before asking I took a look to QtScript but I did not find a clue to build a DSL upon it.
If you google 'javascript dsl' you may be able to find some useful information.
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.

1 Comment

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

I've never tried it but there is PyQt.

1 Comment

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

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

Comments

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.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.