脚本语言可以翻译成其他语言吗?

发布于 2024-10-16 05:37:13 字数 53 浏览 6 评论 0原文

脚本语言是否可以翻译为 C、C++ 或 Java,以便可以在 IDE 上运行而无需重写代码?

Can a scripting language be translated into C, C++, or Java so it can be run on an IDE without rewriting the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

故事还在继续 2024-10-23 05:37:13

理论上,是的,可以将任何脚本语言翻译成 C、C++ 或 Java 代码。理论上有效的方法是获取解释器的源代码,然后在它将要执行的脚本中进行硬编码。生成的代码将“在指定的源代码上运行用 C/C++/Java 编写的解释器”。

在实践中,通常没有一种好的方法可以在保留原始编码风格的情况下从脚本语言转换为其他目标语言。每种语言都有自己的结构、习语和特性,在从源脚本语言翻译成目标语言的过程中,许多原始结构都会丢失。也就是说,有许多项目出于性能原因进行这种转换。例如,Facebook 的 HipHop 编译器出于效率原因将 PHP 翻译为 C++。不过,生成的代码并不适合人类阅读。

简而言之,是的,这是可以完成的,但不能以产生漂亮代码的方式完成。

In theory, yes, it is possible to translate any scripting language into C, C++, or Java code. A theoretically valid way of doing this would be to take the source code for the interpreter and then to hardcode in the script that it's going to be executing. The resulting code would then be "run the interpreter written in C/C++/Java on the specified source code."

In practice, there usually isn't a good way of translating from a scripting language to some other target language in a way that preserves the original coding style. Each language has its own constructs, idioms, and idiosyncrasies and in translating from the source scripting language to a target language much of the original structure is lost. That said, there are many projects that do this sort of conversion for performance reasons. For example, Facebook's HipHop compiler translates PHP into C++ for efficiency reasons. The resulting code is not intended to be read by humans, though.

So in short, yes, it can be done, but not in a way that's going to result in pretty code.

风吹雪碎 2024-10-23 05:37:13

请查看 shedskin,了解 Python 到 C++ 转换器的示例。它并不完美。它对可以翻译的代码有一些限制。但总的来说它是有效的。

在这种情况下,这样做的主要原因是与其他现有 C++ 软件集成的速度和方便性。

Take a look at shedskin for an example of a Python to C++ translator. It isn't perfect. It has some limitations on what code can be translated. But in general it works.

The main reason to do so, in this case, is speed and ease of integration with other existing C++ software.

逐鹿 2024-10-23 05:37:13

从理论上讲,是的,这是可能的。根据脚本语言及其支持“虚拟机”,有一些工具可以(半)自动执行此操作。语言的解释越深入,您翻译代码的可能性就越小(例如,将 HTML 网页翻译成原生 C 有点可笑,而不是将 matlab 代码翻译成 C 或 C++)。一般来说,用于翻译代码的通用工具很少足够好,以至于您可以编译和运行生成的代码,通常它们会完成大部分语法翻译(基本上是查找和替换操作),也许还有一些更高级的东西。但是,大多数时候,您仍然有大量工作要做(例如使用谷歌翻译器将网页从一种语言翻译成另一种语言,它永远不会完美,这取决于两种语言的接近程度)。

然而,在我看来,代码翻译是一项非常危险的事情。当您手动重写您知之甚少的代码时,很容易犯错别字或其他错误。自动翻译工具在这方面也不会表现得更好。然后,翻译完代码后,如果出现错误怎么办?你该如何找到他们?并修复它们?当您对实际代码知之甚少时。这很快就会变成一场噩梦般的经历。我以前这样做过,以后不要再这样做了!

顺便说一句:如果您希望在用另一种语言编写的项目中使用以脚本语言编写的代码,那么您可能会考虑连接这些语言,而不是将一种代码翻译为另一种语言。大多数编程语言和脚本语言都具有与其他语言(例如DLL 或COM/ActiveX 组件)交互的功能。如果可能的话,最好以最初编写的语言保存代码。

In theory, yes it's possible. Depending on the scripting language and it supporting "virtual machine", there are tools to do this (semi-)automatically. The more heavily interpreted the language is the less likely you will be able to translate the code (for example, translating an HTML webpage into native C is kinda ridiculous, as opposed to translating matlab code into C or C++). In general, generic tools for translating code are rarely good enough that you can compile and run the code that is produced, very often they will do most of the syntax translation (basically find & replace operations) and maybe some more advanced stuff. But, most of the time, you will still have significant work to do (like using google translator to translate a webpage from one language to another, it is never perfect and it depends on how close the two languages are).

In my opinion, however, I would say that code translation is a very dangerous business. It is a lot easier to make typos or other mistakes when you are manually rewriting code that you know very little about. An automatic translation tool won't perform much better on that front either. And then, once you have translated the code, what if there are bugs? How are you supposed to find them? and fix them? when you know very little about the actual code. This can very rapidly become a nightmarish experience. I have done it in the past, and don't do it anymore!

BTW: if you are looking to use code that is written in a script language inside a project written in another language, then you might consider interfacing the languages instead of translating one code to the other language. Most programming languages and scripting languages have facilities to interface with other languages (e.g. DLLs or COM/ActiveX components). It is always better to preserve the code in the language it was originally written if at all possible.

甜心 2024-10-23 05:37:13

有一种名为 Haxe 的编程语言,可以翻译成 C++、Java、Javascript、C# 和其他几种语言。这种语言出现的时间相对较晚,旨在翻译成尽可能多的目标语言。

There is a programming language called Haxe that can be translated into C++, Java, Javascript, C# and several other languages. This language appeared relatively recently, and is designed to be translated into as many target languages as possible.

七堇年 2024-10-23 05:37:13

对于 C,有一些脚本语言看起来有点像 C。也许这是一个起点。我想到了 Lua。

对于java,有一种脚本语言beanshell/bsh,它以脚本的形式运行简化的java代码。但是你必须重写它才能用它生成 Java 代码,而且我不知道这个过程有多容易,才能自动发生。

另一种方法是:编写一个 C 解释器,这样您就可以使用 C 代码进行脚本编写,并在需要时编译它。

For C, there are some scripting languages which look a little like C. Maybe that's a starting point. Lua comes to my mind.

For java, there is a scripting language, beanshell/bsh which runs a simplified java code as script. But you would have to rewrite it to make Javacode out of it, and I don't know how easy the process would be, to make this automatically happen.

A different approach would be: Write an C interpreter, so you can use C-Code for scriping, and just compile it when you need to.

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