轻松将 Lua 代码移植到 C#

发布于 2024-08-20 03:51:03 字数 116 浏览 3 评论 0原文

有没有简单的方法可以将Lua代码移植到C#?

最大的问题可能是在某些字典中整齐地移植表格。

为了防止任何误解:不,我不能在我的程序中使用嵌入式 Lua

is there any easy way to port Lua code to C#?

The biggest problem would probably be to port tables neatly in some dictionaries.

And to prevent any misunderstanding: no I cannot use embedded Lua in my program.

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

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

发布评论

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

评论(4

沙与沫 2024-08-27 03:51:03

使用 Lua 这样的高度动态语言设计的代码需要进行大量重构才能在 C# 这样的静态语言中有意义 - 两者服务于根本不同的目的。实际上,您必须从头开始重新编写此类代码,除非它仅使用任何语言的最基本功能,例如基本的数字/字符串操作。

Code designed in such a highly dynamic language like Lua would need substantial refactoring to make sense in a static language like C#- the two serve fundamentally different purposes. You would have to re-write such code again from scratch, realistically, unless it used only the most basic features of any language, like basic numerical/string ops.

旧伤还要旧人安 2024-08-27 03:51:03

没有简单的方法可以做到这一点。

There's no easy way to do that.

若水般的淡然安静女子 2024-08-27 03:51:03

Universal-transpiler 可以将一小部分 Lua 翻译成其他几种语言,包括 C#。这是为 SWI-Prolog 编写的示例:

:- use_module(library(transpiler)).
:- set_prolog_flag(double_quotes,chars).
:- initialization(main).

main :- 
    translate("function add(a,b) return a + b end function squared(a) return a*a end function add_exclamation_point(parameter) return parameter .. \"!\" end",'lua','c#',X),
    atom_chars(Y,X),
    writeln(Y).

这是它生成的 C# 源代码:

public static int add(int a,int b){
        return a+b;
}
public static int squared(int a){
        return a*a;
}
public static string add_exclamation_point(string parameter){
        return parameter+"!";
}

Universal-transpiler can translate a small subset of Lua into several other languages, including C#. This is an example, written for SWI-Prolog:

:- use_module(library(transpiler)).
:- set_prolog_flag(double_quotes,chars).
:- initialization(main).

main :- 
    translate("function add(a,b) return a + b end function squared(a) return a*a end function add_exclamation_point(parameter) return parameter .. \"!\" end",'lua','c#',X),
    atom_chars(Y,X),
    writeln(Y).

This is the C# source code that it generates:

public static int add(int a,int b){
        return a+b;
}
public static int squared(int a){
        return a*a;
}
public static string add_exclamation_point(string parameter){
        return parameter+"!";
}
淡水深流 2024-08-27 03:51:03

你想实现什么目标?将 lua 文件转换为 C# 代码,您希望在其中广泛使用它们,或者您只需要一些与原始代码执行类似操作的代码。

对于第一种类型的转换,答案是相当困难,但并非不可能。您必须解析代码并在 C# 中重新创建相同的(动态)功能。像 LinFu.Reflection 这样的框架可以在这方面提供帮助,因为它们将为 CLR 添加一些动态功能。

对于第二种,我的想法是将lua字节码转换为C#,而不是原来的代码。这应该不会太难,主要是因为 lua 没有太多操作码(如果我没记错的话大约 30 个)。从这些操作码中,最难转换的是逻辑和跳转运算符(因为 C# 中没有 goto),但如果保持流运算符完整(并将它们转换为 C# - 这或多或少是可以实现的),并且只有编译之间的代码,并将结果字节码转换为 C# 应该可以完成这项工作。当然,这样你会失去很多原始代码的可读性,并且维护它会更加困难。

您也可以尝试在我在这里写的这两个边缘情况之间找到解决方案。一些结构可以很容易地移植(主要是循环和简单的算术运算符),但可以回退到操作码表示来进行表处理。

What do you want to achieve? Convert the lua files to C# code, where you want to work with them extensively, or you just want some code that does similar things than the original code.

For the first type of conversion the answer is that it is quite hard, but not impossible. You have to parse the code and re-create the same (dynamic) functionality in C#. Frameworks, like LinFu.Reflection can help here, because they will add some dynamic functionality to CLR.

For the second type, my idea is to convert the lua bytecode to C# instead of the original code. This shouldn't be too hard, mainly because lua doesn't have much opcodes (around 30 if I remember it correctly). From these opcodes the hardest to convert are the logic and jump operators (because you don't have goto in C#), but if you keep the flow operators intact (and convert them to C# - that is more or less accomplishable), and only compile the code between, and convert the result bytecode to C# should do the job. Of course this way you'll lose a lot from the readibility of the original code, and maintaining it will be much harder.

You might also try to find a solution between these two edge cases I've written here. Some constructs can be easily ported (mainly the loops, and simple arithmetic operators), but fall back to the opcode representation for table handling.

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