Embedded scripting engine for DSL

寵の児 提交于 2019-12-14 01:16:14

问题


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:


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




回答2:


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

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




回答3:


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




回答4:


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.




回答5:


I've never tried it but there is PyQt.




回答6:


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




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/1591114/embedded-scripting-engine-for-dsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!