将编程语言嵌入到程序中

时间:2021-04-08 15:04:49

I'd like to add some degree of "scriptability" to a application I'm writing. Roughly speaking, I'd like a way to embed a programming language API into my program. I've looked at TinyPy, but I'm worried that - given it's small size - it might be deceptively limiting (i.e. it looks and feels like Python, but is lacking many of the features of full-fledged Python).

我想为我正在编写的应用程序添加一定程度的“脚本性”。粗略地说,我想要一种在我的程序中嵌入编程语言API的方法。我看过TinyPy,但我很担心 - 鉴于它的体积很小 - 它可能具有欺骗性的限制(即它看起来和感觉像Python,但缺乏成熟的Python的许多功能)。

I'm not looking to write my own programming language; I'd like to simply take a relatively well-known language that users can write functions in, and then be able to call those functions/pass those functions parameters from my application, which will be written in C.

我不打算编写自己的编程语言;我想简单地使用一个相对众所周知的语言,用户可以编写函数,然后能够调用这些函数/从我的应用程序中传递这些函数参数,这些参数将用C语言编写。

I'd also like to be access any data types those functions return back to me, also from within C.

我也想访问那些函数返回给我的任何数据类型,也是从C内部。

Does anyone have any suggestions as to an API/Library to use, which would be both lightweight, relatively full-featured, and with a relatively easy to use or easy to understand C library?

有没有人对使用的API /库有任何建议,哪些都是轻量级,相对功能齐全,并且具有相对易于使用或易于理解的C库?

Thanks!

7 个解决方案

#1


5  

You might want to look at SpiderMonkey, which will allow you to embed a Javascript engine in your C program.

您可能希望查看SpiderMonkey,它允许您在C程序中嵌入Javascript引擎。

#2


16  

Lua is a popular choice.

Lua是一个受欢迎的选择。

#3


3  

Most scripting languages have good interfaces with C. The ones I've actually glanced at are Perl, Python, and Lua. If your users aren't serious programmers, I'd advise giving Perl a miss. If they're serious World of Warcraft players, they might already be familiar with Lua, and writing Lua code to interface with a compiled program, so that may be a good choice.

大多数脚本语言都与C有很好的接口。我实际上看过的是Perl,Python和Lua。如果您的用户不是认真的程序员,我建议给Perl一个小姐。如果他们是严肃的魔兽世界玩家,他们可能已经熟悉Lua,并编写Lua代码与编译程序接口,因此这可能是一个不错的选择。

#4


2  

YOu can always use 'c' with the ch toolkit.
As you said python is good especially if you can use the boost::python wrapper.
Another popular choice is Javascript, it's used by Qt's scripting system

你可以随时使用'c'和ch工具包。正如你所说python是好的,特别是如果你可以使用boost :: python包装器。另一个受欢迎的选择是Javascript,它被Qt的脚本系统使用

#5


2  

Lua and slang were both invented just for this. Lua has some neat features to sandbox code, so if users can provide code you can attempt to limit their ability to shoot themselves in the foot.

Lua和俚语都是为此而发明的。 Lua对沙箱代码有一些巧妙的功能,所以如果用户可以提供代码,你可以尝试限制他们自己射击自己的能力。

Flavors of lisp have been used for this (famously in gnu emacs).

lisp的味道已被用于此(着名的gnu emacs)。

You really need to think about what types of things the embedded language will be used for and then think about the type of languages that you might like to write that functionality in, read code that others had written in, and easily satisfy yourself that the code was correct, or at least easily debug it.

您真的需要考虑嵌入式语言将用于什么类型的事物,然后考虑您可能想要编写该功能的语言类型,阅读其他人编写的代码,并轻松满足自己的代码是正确的,或至少很容易调试它。

#6


2  

GUILE is GNU's Ubiquitous Intelligent Language for Extension. It is a Scheme dialect (LISP dialect). Before you scream about LISP, consider that AutoCAD used LISP as the extension language, and at least one major secretarial word-processing system used it, and the secretaries had no trouble at all writing extensions for their particular tasks. (It seems that people don't realize programming is supposed to be hard if you don't tell them they're doing programming.)

GUILE是GNU无处不在的扩展智能语言。它是Scheme方言(LISP方言)。在您对LISP尖叫之前,请考虑AutoCAD使用LISP作为扩展语言,并且至少有一个主要的秘书文字处理系统使用它,并且秘书可以毫无困难地为他们的特定任务编写扩展。 (如果你不告诉他们他们正在编程,人们似乎并没有意识到编程应该很难。)

#7


1  

The most common well known language is javascript. There are a few good and simple (well not really as simple as possible if you compare it with Lua for example) to embedd Javascript interpreters out there.

最常见的语言是javascript。有一些好的和简单的(例如,如果你将它与Lua比较,那就不是那么简单)嵌入Javascript解释器。

You haven't said anything about external libraries. Like Lua these javascript systems are naked. There is not much provided for communication with the outside word. That is in contrast to ruby and python where you get a batteries included system with the zillions of functions from the runtime library

你还没有说过外部库。像Lua一样,这些javascript系统都是裸体的。提供与外部单词的通信并不多。这与ruby和python相反,在这里你可以获得一个包含电池的系统,其中包含来自运行时库的数十万个函数

Start with reading this:

从阅读本文开始:

Why embed JavaScript?

为什么要嵌入JavaScript?

How to embed the JavaScript engine

如何嵌入JavaScript引擎

#1


5  

You might want to look at SpiderMonkey, which will allow you to embed a Javascript engine in your C program.

您可能希望查看SpiderMonkey,它允许您在C程序中嵌入Javascript引擎。

#2


16  

Lua is a popular choice.

Lua是一个受欢迎的选择。

#3


3  

Most scripting languages have good interfaces with C. The ones I've actually glanced at are Perl, Python, and Lua. If your users aren't serious programmers, I'd advise giving Perl a miss. If they're serious World of Warcraft players, they might already be familiar with Lua, and writing Lua code to interface with a compiled program, so that may be a good choice.

大多数脚本语言都与C有很好的接口。我实际上看过的是Perl,Python和Lua。如果您的用户不是认真的程序员,我建议给Perl一个小姐。如果他们是严肃的魔兽世界玩家,他们可能已经熟悉Lua,并编写Lua代码与编译程序接口,因此这可能是一个不错的选择。

#4


2  

YOu can always use 'c' with the ch toolkit.
As you said python is good especially if you can use the boost::python wrapper.
Another popular choice is Javascript, it's used by Qt's scripting system

你可以随时使用'c'和ch工具包。正如你所说python是好的,特别是如果你可以使用boost :: python包装器。另一个受欢迎的选择是Javascript,它被Qt的脚本系统使用

#5


2  

Lua and slang were both invented just for this. Lua has some neat features to sandbox code, so if users can provide code you can attempt to limit their ability to shoot themselves in the foot.

Lua和俚语都是为此而发明的。 Lua对沙箱代码有一些巧妙的功能,所以如果用户可以提供代码,你可以尝试限制他们自己射击自己的能力。

Flavors of lisp have been used for this (famously in gnu emacs).

lisp的味道已被用于此(着名的gnu emacs)。

You really need to think about what types of things the embedded language will be used for and then think about the type of languages that you might like to write that functionality in, read code that others had written in, and easily satisfy yourself that the code was correct, or at least easily debug it.

您真的需要考虑嵌入式语言将用于什么类型的事物,然后考虑您可能想要编写该功能的语言类型,阅读其他人编写的代码,并轻松满足自己的代码是正确的,或至少很容易调试它。

#6


2  

GUILE is GNU's Ubiquitous Intelligent Language for Extension. It is a Scheme dialect (LISP dialect). Before you scream about LISP, consider that AutoCAD used LISP as the extension language, and at least one major secretarial word-processing system used it, and the secretaries had no trouble at all writing extensions for their particular tasks. (It seems that people don't realize programming is supposed to be hard if you don't tell them they're doing programming.)

GUILE是GNU无处不在的扩展智能语言。它是Scheme方言(LISP方言)。在您对LISP尖叫之前,请考虑AutoCAD使用LISP作为扩展语言,并且至少有一个主要的秘书文字处理系统使用它,并且秘书可以毫无困难地为他们的特定任务编写扩展。 (如果你不告诉他们他们正在编程,人们似乎并没有意识到编程应该很难。)

#7


1  

The most common well known language is javascript. There are a few good and simple (well not really as simple as possible if you compare it with Lua for example) to embedd Javascript interpreters out there.

最常见的语言是javascript。有一些好的和简单的(例如,如果你将它与Lua比较,那就不是那么简单)嵌入Javascript解释器。

You haven't said anything about external libraries. Like Lua these javascript systems are naked. There is not much provided for communication with the outside word. That is in contrast to ruby and python where you get a batteries included system with the zillions of functions from the runtime library

你还没有说过外部库。像Lua一样,这些javascript系统都是裸体的。提供与外部单词的通信并不多。这与ruby和python相反,在这里你可以获得一个包含电池的系统,其中包含来自运行时库的数十万个函数

Start with reading this:

从阅读本文开始:

Why embed JavaScript?

为什么要嵌入JavaScript?

How to embed the JavaScript engine

如何嵌入JavaScript引擎