为什么我们需要像Lua这样的可嵌入编程语言? [英] Why do we need an embeddable programming language like Lua?
问题描述
使用嵌入式编程语言的典型用例是什么?我是否正确理解这样的语言应该嵌入到某些程序环境中并应该可以从那里执行?
What are the typical use cases of using an embeddable programming language? Do I understand it correctly that such language should be embedded into some program environment and should be able to be executed from there?
推荐答案
由于您将问题标记为"Lua",因此我将在使用该语言的情况下为您提供答案.
Since you tagged the question as "Lua", I'll give you an answer in the context of this language.
Lua用C编写(几乎完全与C89标准兼容;如果需要,可以使用编译时开关轻松禁用不兼容的功能),并已设计为易于与C代码集成.在Lua的上下文中,集成"是指两个不同但相关的事物:
Lua is written in C (almost completely compatible with C89 standard; the incompatible features can be easily disabled, if needed, using compile-time switches) and has been designed to be easily integrated with C code. In the the context of Lua, "integrated" means two different, but related, things:
-
您可以轻松编写可被Lua代码用作库的C代码.可以通过将C代码静态或动态链接到Lua引擎的代码来实现集成.然后可以使用Lua
require
函数在您的Lua代码中引用链接的库.
You can easily write C code that can be used as a library by Lua code. The integration is achieved either by static or dynamic linking your C code to Lua engine's code. The linked library can then be referred to in your Lua code using the Lua
require
function.
Lua引擎可以轻松地嵌入到C应用程序中,即(再次静态或动态地)链接到C应用程序代码.然后,C应用程序可以使用Lua的 C应用程序编程接口(Lua C -API).
Lua engine can be easily embedded in a C application, i.e. linked (again either statically or dynamically) to the C application code. Then the C application can interact with the Lua code using Lua's C application programming interface (Lua C-API).
注意:即使使用C ++应用程序,也可以花费更多的精力来做到这一点.
Note: this can be done, with a little more effort, also with a C++ application.
如果您的C应用程序将Lua嵌入很多(如果不是大多数),则可以将操作委派给Lua引擎,即,可以委派给使用C-API函数编写的代码,或者更好的是,可以将Lua代码委派给Lua引擎. Lua代码可以作为C字符串嵌入到您的C代码中,也可以存储为外部Lua脚本.
If your C application embeds Lua many, if not most, operations can be delegated to the Lua engine, i.e. either to code written using the C-API functions or, better yet, Lua code. Lua code could be embedded as C strings inside your C code or be stored as external Lua scripts.
使用Lua代码实现部分代码逻辑具有以下优点:
Having part of your code logic implemented using Lua code has several advantages:
-
与C相比,Lua学习和使用起来更简单(不那么棘手),并且它的层次更高.它支持强大的抽象,例如函数闭包和面向对象(使用Lua表和 Lua是一种动态语言:它不需要离线"编译.您可以修改Lua脚本的文本,这就是修改应用程序行为的全部(无需其他编译+链接步骤).这简化了应用程序的开发和调试.
Lua is a dynamic language: it requires no "off-line" compilation. You can modify the text of your Lua script and that's all you need to modify your application behavior (no additional compilation+linking steps needed). This simplifies application development and debugging.
Lua是比C更安全的语言:按照C/C ++的意图,编写具有未定义行为的Lua代码确实很困难.如果Lua脚本失败,则大声"失败.而且,Lua支持一种异常机制(尽管其语法与C ++不同),与C相比,该机制可用于以更简单的方式实现错误管理.
Lua is a safer language than C: it is really difficult to write Lua code that exhibits undefined behavior, as intended in the context of C/C++. If a Lua script fails, it fails "loudly". Moreover Lua supports an exception mechanism (although with a different syntax than C++) which can be employed to implement error management in a much easier way compared to C.
Lua被垃圾回收.这意味着程序员可以免去手动管理动态内存的痛苦,这是缺乏垃圾收集的语言中的错误,泄漏,不稳定和安全漏洞的主要原因.
Lua, as most dynamic languages, is garbage collected. This means that the programmer is spared the pain of manually managing dynamic memory, which is a major cause of bugs, leaks, instability and security loopholes in languages that lack garbage collection.
Lua可以吃掉自己的狗食",即,您可以在运行时(甚至在Lua本身中)构建一个字符串,并且如果它是有效的Lua代码,则您的程序可以即时执行它.即使在其他动态语言中,这种情况也很少见(仍然不是LISP,但它越来越近了,并且语法更加可读).这使Lua脚本能够:
Lua can "eat its own dog food", i.e. you can build a string at runtime (even in Lua itself) and if it is valid Lua code, your program can execute it on the fly. This is something not frequently seen even in other dynamic languages (still it is not LISP, but it gets closer, and with much more readable syntax). This enables Lua scripts to:
-
采用强大的基于文本的元编程技术,其中Lua代码可以生成其他Lua代码并即时执行;
employ powerful text-based metaprogramming techniques, where Lua code can generate other Lua code and execute it on the fly;
以一种简单的方式实现领域特定语言(DSL); Lua代码可以在运行时加载其他经过精心设计的Lua代码,以反映使用它的特定问题域(Lua语法很简单,但足够灵活以允许此类事情);
implement domain specific languages (DSLs) in an easy way; Lua code can load at runtime other Lua code that is crafted so as to reflect the specific problem domain in which it is used (Lua syntax is simple, yet flexible enough to allow such things);
轻松用作配置语言:您的应用程序(使用C和Lua混合编写)可以使用一些lua文件作为配置文件,而无需为特定的配置文件设计临时解析器格式.因此,如果您没有为此目的使用Lua文件的选项,则无需解析
*.properties
,*.csv
,*.ini
或任何其他格式,就可以了.be used as a configuration language with ease: your application (written in a mix of C and Lua) can use some lua files as configuration files without the need to craft an ad-hoc parser for a specific configuration file format. Therefore you don't need to parse
*.properties
,*.csv
,*.ini
, or whichever other format you would choose if you hadn't the option of using Lua files for that purpose.Lua引擎的内存占用非常小(约数百kB),具有强大的功能.只需很少的C代码行和一堆Lua文件,您就可以创建一个完整的应用程序,否则它将需要成千上万的C代码行. 标准Lua独立解释器可以看作是嵌入Lua的一个示例.在C应用程序中!
Lua engine has a very small memory footprint (some hundreds kBs), packing powerful capabilities. With very few C code lines and a bunch of Lua files you could create a complete application that would require thousands of C code lines otherwise. The standard Lua standalone interpreter can be seen as just an example of embedding Lua in a C application!
Lua具有非常自由的开源许可证,甚至可以使用在商业应用中没有太多麻烦.这也允许修改其源代码以使其适应特殊需要.
Lua has a very liberal open-source license, which enables its use even in commercial applications without much hassle. This also allows the modification of its source code to adapt it to special needs.
小内存占用空间和易于调整的C源代码使Lua成为将其移植到嵌入式系统或小型微计算机系统(微控制器等)上的理想选择.可以剥离标准Lua发行版的许多部分,从而将核心Lua引擎减少到约100kB范围内.例如,以 eLua项目(一种为嵌入式设备设计的Lua的改进发行版)
Small memory footprint and easily tweakable C sources make Lua a perfect candidate for porting it on embedded systems or small microcomputer systems (microcontrollers, etc.). Many parts of the standard Lua distributions can be stripped off, reducing the core Lua engine in the ~100kB range. As an example, take the eLua project, a modified distribution of Lua designed for embedded devices.
这篇关于为什么我们需要像Lua这样的可嵌入编程语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-