Embedded scripting engine for DSL
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 que开发者_如何学Pythonstions 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.
精彩评论