Lua 作为嵌入式语言的替代品?

发布于 2024-10-07 21:00:59 字数 600 浏览 0 评论 0原文

我正在开发一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

太阳男子 2024-10-14 21:00:59

既然你说“嵌入式系统”、“小而快”和“集成得很好”,我想说你是对的,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.

岛歌少女 2024-10-14 21:00:59

我全心全意地推荐 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.

迷鸟归林 2024-10-14 21:00:59

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.

一紙繁鸢 2024-10-14 21:00:59

最近的替代方案是 wren

Wren 是一种小型、快速、基于类的并发脚本语言

Wren 是一种脚本语言。 Wren 旨在嵌入
应用程序。它没有依赖项、一个小型标准库和一个
易于使用的 C API。它可以干净地编译为 C99、C++98 或任何其他类型
稍后。

A more recent alternative is wren:

Wren is a small, fast, class-based concurrent scripting language

Wren is a scripting language. Wren is intended for embedding in
applications. It has no dependencies, a small standard library, and an
easy-to-use C API. It compiles cleanly as C99, C++98 or anything
later.

怪异←思 2024-10-14 21:00:59

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.

那些过往 2024-10-14 21:00:59

根据您的要求(占用空间小、有点怪癖以及与 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.

戴着白色围巾的女孩 2024-10-14 21:00:59

您是否考虑过Python?有一个很好的扩展和嵌入指南。如果您使用 BoostBoost 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.

腻橙味 2024-10-14 21:00:59

我知道 Lua 是解决此类问题的行业选择。

这是一个大胆的主张!我建议,如果您已经在运行 Linux,那么选择是很广泛的。 Linux 本身绝不是嵌入式系统的“行业选择”; “行业”远比这更加分散。

Forth 是一种几乎可以在任何系统上实现的语言,无论性能、大小和操作系统(甚至没有操作系统)如何。也许不是最流行的语言,但很容易实现和扩展。

另一个候选者可能是 ch,它是一个可嵌入的 C/C++ 解释器,因此您可以使用相同的语言进行编译和脚本代码。

I understand that Lua is the industry choice for problems like this.

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.

一枫情书 2024-10-14 21:00:59

尝试 pocketpy 及其 现场演示

pocketpy 是一个轻量级(~10000 LOC)的游戏 Python 解释器
脚本,使用 STL 基于 C++17 构建。

它的目标是成为游戏脚本中lua的替代品,具有优雅的
语法、强大的功能和有竞争力的性能。 pkpy 是
通过单个头文件 pocketpy.h 非常容易嵌入,无需
外部依赖项。

Try pocketpy and its Live Demo.

pocketpy is a lightweight(~10000 LOC) Python interpreter for game
scripting, built on C++17 with STL.

It aims to be an alternative to lua for game scripting, with elegant
syntax, powerful features and competitive performance. pkpy is
extremely easy to embed via a single header file pocketpy.h, without
external dependencies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文