Lua 作为嵌入式语言的替代品?
我正在开发一个在 DSP 上运行 Linux 的嵌入式系统。现在我们想让它的某些部分可以编写脚本,并且我们正在寻找一种很好的嵌入式脚本语言。这些脚本应该与我们现有的 C++ 代码库很好地集成,并且小而快。
我知道 Lua 是解决此类问题的行业选择。我们可能会选择 Lua,因为它经过验证并且被证明是稳定的等等。然而,作为一种编程语言,它有一些相当古怪的地方。
那么,嵌入式语言有哪些替代方案呢?
编辑:
这是大约一年后的事。
我们实际上在嵌入式系统上使用了 Lua,并且它的性能非常好。随着时间的推移,我们为项目的越来越多部分添加了越来越多的脚本支持,这确实有助于项目的顺利进行。
性能确实非常出色。即使涉及搜索长数组或花哨的字符串操作的相当复杂的操作也表现得令人惊讶。我们基本上从来没有遇到过 Lua 相关的性能问题。
与 C 函数的接口非常简单并且工作得非常好。这使我们能够轻松地扩展脚本系统。
最后,我们对 Lua 的灵活性感到惊讶。我们的 Lua 解释器必须在具有非标准内存分配器且不支持双精度数据类型的系统上运行。为了让 Lua 在该系统上工作,我们必须修改一个头文件中两个有详细记录的地方。它真的非常适合嵌入!
I am working on an embedded system running Linux on a DSP. Now we want to make some parts of it scriptable and we are looking for a nice embeddable scripting language. These scripts should integrate nicely with our existing C++ code base, be small and fast.
I understand that Lua is the industry choice for problems like this. We will probably go with Lua because it is tried-and-true and proven to be stable and so on. However, as a programming language it has some rather quirky corners.
So, what alternatives are out there for embeddable languages?
EDIT:
This is about a year later.
We actually used Lua on our embedded system and it performs marvelously well. Over time, we added more and more scripting support to more and more parts of the project and that really helped to bring it along.
Performance is outstanding, really. Even rather complex operations that involve searching through long arrays or fancy string operations perform surprisingly well. We basically never ran into Lua related performance problems at all.
Interfacing with C functions is very straightforward and works really well. This allowed us to grow the scripting system painlessly.
Finally, we were astounded at how flexible Lua proved to be. Our Lua interpreter has to run on a system with a nonstandard memory allocator and without support for the double data type. There are two well-documented places in one header file we had to modify to make Lua work on that system. It is really well suited for embedding!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
既然你说“嵌入式系统”、“小而快”和“集成得很好”,我想说你是对的,Lua 即使不是唯一的选择,也是第一选择。但我不再同意编程语言有“古怪的角落”。首先,《Programming in Lua》这本书非常精彩,是我读过的最好的书之一。其次,一些“古怪的角落”来自于语言非常正交和干净的事实,从长远来看,这是一种优点,而不是缺点。我发现 JavaScript 的例子要糟糕得多。如果您阅读“Javascript the good parts”,作者会详细解释为什么该语言中的某些构造是设计错误以及为什么应该避免使用 new 运算符。 Lua 中则不然,不好的部分已被删除,例如,在 5.x 版本中,古怪的 upvalue 内容被标准语法作用域所取代。
我的观点实际上是,Lua 是一种比大多数其他语言更少古怪的语言!我们在商业项目中使用它,我们对此非常满意。
Since you say "embedded system" and "small and fast" and "integrate nicely" I would say you are correct that Lua is the number one if not the only choice. But I no longer agree that the programming language has "quirky corners". Firstly, the book Programming in Lua is simply splendid, one of the best books I have ever read. Secondly, some of the "quirky corners" come from the fact that the language is very orthogonal and clean, which in the long run is an asset, not a drawback. I find for example JavaScript much worse. If you read "Javascript the good parts" the author explains in length why some constructs in the language are design mistakes and why one should avoid the new operator. Not so in Lua, the bad parts have been removed, for example the quirky upvalue stuff was replaced with standard syntactic scoping in version 5.x.
My view is actually that Lua is a language with far less quirky corners than most other languages! We use it in a commercial project and we are more than happy with it.
我全心全意地推荐 Lua 用于您的用例。然而,Forth 是一种替代方案,特别是对于资源受限的嵌入式设备尚未提及。
I wholeheartedly recommend Lua for your use case. However, Forth is an alternative--especially for resource constrained embedded devices--that has not yet been mentioned.
Lisp 总是存在的。 :) 但这强调了这样一个事实:Lua 实际上不像大多数语言那么“古怪”。它是为非程序员设计的,读起来就像伪代码一样。它具有干净、统一的语义(具有词法作用域的一流嵌套函数;多重赋值;多个返回值;具有干净构造函数语法的单一、灵活的数据结构机制;等等),这使得它非常容易学习、阅读、编写等它也恰好出乎意料地强大且富有表现力(适当的尾部调用、延续、元编程等)。Lua
唯一真正“古怪”的方面是数组索引从 1 开始,而且它没有借用 C 的约定,例如。其他人(
~=
而不是!=
、--
而不是//
等),但是对于习惯了类 C 语言的程序员来说,这些大多都是古怪的。另一种选择可能是 Squirrel,它受到 Lua 的启发,具有类似的目标,但语法类似于 C。但我没有使用过它,所以我不太知道它是否达到了它的目标。
There's always Lisp. :) But that underscores the fact that Lua is in fact less "quirky" than most languages. It was designed for non-programmers and reads like pseudocode. It has clean, uniform semantics (first class nested functions with lexical scoping; multiple assignment; multiple return values; a single, flexible data structuring mechanism with clean constructor syntax; etc.) which make it very easy to learn, read, write, etc. It also happens to be unexpectedly powerful and expressive (proper tail calls, continuations, metaprogramming, etc.)
The only really "quirky" aspect of Lua is that arrays index from 1, and that fact that it doesn't borrow C's conventions like everybody else (
~=
rather than!=
,--
rather than//
, etc.), but these are mostly quirky through the eyes of programmers habituated to C-like languages.An alternative might be Squirrel, which is inspired by Lua, has similar goals, but C-like syntax. I've not used it though, so I don't know well it meets it's goals.
最近的替代方案是 wren:
A more recent alternative is wren:
Tcl 从一开始就被设计为一种嵌入式语言,并且已经存在了几十年。此外,由于其高度可扩展的性质,它是开发特定领域语言的完美选择。
我对 DSP 世界了解不多,但是当你用 google 搜索“dsp lua”和“dsp tcl”时,你会得到 Tcl 两倍的点击率。
Tcl was designed from the ground up to be an embedded language, and has been around for decades. Plus, it is a perfect choice for developing a domain-specific language because of its highly extensible nature.
I don't know a lot about the DSP world, but when you google "dsp lua" and "dsp tcl" you get twice as many hits for Tcl.
根据您的要求(占用空间小、有点怪癖以及与 C++ 集成),我能想到的唯一选择是 Common口齿不清。
这个其他问题中的一些人推荐CFFI 将其与 C 集成。
但如果我在你那里,我会坚持使用 Lua。
With your requirements (small footprint, little quirks and integration with C++), the only option I can think about is Common Lisp.
Some people in this other SO question are recommending CFFI for integrating it with C.
But I'd stick with Lua if I where you.
您是否考虑过Python?有一个很好的扩展和嵌入指南。如果您使用 Boost,Boost Python 是一个用于 C++ 和 Python 之间无缝集成的库。
Have you considered Python? There's a nice extending and embedding guide available. If you're using Boost, Boost Python is a library for seemless integration between C++ and Python.
这是一个大胆的主张!我建议,如果您已经在运行 Linux,那么选择是很广泛的。 Linux 本身绝不是嵌入式系统的“行业选择”; “行业”远比这更加分散。
Forth 是一种几乎可以在任何系统上实现的语言,无论性能、大小和操作系统(甚至没有操作系统)如何。也许不是最流行的语言,但很容易实现和扩展。
另一个候选者可能是 ch,它是一个可嵌入的 C/C++ 解释器,因此您可以使用相同的语言进行编译和脚本代码。
A bold claim! I would suggest that if you are already running Linux, the choice is wide open. Linux itself is by no means the "industry choice" for embedded systems; the 'industry' is far more fragmented than that.
A language that is implementable on virtually any system regardless of performance, size, and OS (or even no OS), is Forth. Not the most fashionable language perhaps, but easily implementable and extensible.
Another candidate might be ch, which is an embedable C/C++ interpreter, so you can use the same language for compiled and scripted code.
尝试 pocketpy 及其 现场演示。
Try pocketpy and its Live Demo.