了解运行时代码解释和执行
我正在 XNA 中创建一个游戏,并正在考虑创建我自己的脚本语言(请注意,非常简单)。我知道有更好的方法来解决这个问题(而且我正在重新发明轮子),但我想要的不仅仅是高效和快速的学习体验。
当在运行时遇到代码时,据我了解,通常的方法是解析为机器代码或字节代码或其他实际上可执行的代码,然后执行它,对吗?但是,例如,当 Chrome 第一次出现时,他们说他们的 JavaScript 引擎很快,因为它将 JavaScript 编译成机器代码。这意味着其他引擎没有编译成机器代码。
我不想编译为较低的语言,那么是否有任何已知的现代技术可以在不编译为低级语言的情况下解析和执行代码?也许像将代码解析成某种树,在树中进行分支,然后比较每个符号并调用一些处理该符号的函数? (暗中狂猜、暗刺)
I'm creating a game in XNA and was thinking of creating my own scripting language (extremely simple mind you). I know there's better ways to go about this (and that I'm reinventing the wheel), but I want the learning experience more than to be productive and fast.
When confronted with code at run time, from what I understand, the usual approach is to parse into a machine code or byte code or something else that is actually executable and then execute that, right? But, for instance, when Chrome first came out they said their JavaScript engine was fast because it compiles the JavaScript into machine code. This implies other engines weren't compiling into machine code.
I'd prefer not compiling to a lower language, so are there any known modern techniques for parsing and executing code without compiling to low level? Perhaps something like parsing the code into some sort of tree, branching through the tree, and comparing each symbol and calling some function that handles that symbol? (Wild guessing and stabbing in the dark)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人不会推出您自己的解析器(将输入转换为标记)或词法分析器(检查输入标记的语言语法)。看看 ANTLR 进行解析/词法分析 - 如果您愿意,它是一个很棒的框架,并且有完整的源代码深入挖掘它的本质。
为了执行您已解析的代码,我会考虑运行一个简单的虚拟机,甚至更好地查看 llvm这是一个开源尝试,旨在标准化虚拟机字节代码格式并提供 JITing(将脚本编译的字节代码转换为程序集)等不错的功能。
我不会阻止您使用更高级的选项,例如本机机器代码执行,但请记住,这是一个非常专业的领域,并且变得非常复杂、非常快!
厄尔兹指出,我的回答似乎暗示“不用费心自己做这件事”。重读我的帖子,听起来确实有点这样。我之所以提到 ANTLR 和 LLVM 是因为它们都有大量的源代码和教程,所以我觉得这是一个很好的参考源。以此为基础来玩
I personally wouldn't roll your own parser ( turning the input into tokens ) or lexer ( checking the input tokens for your language grammar ). Take a look at ANTLR for parsing/lexing - it's a great framework and has full source code if you want to dig into the guts of it.
For executing code that you've parsed, I'd look at running a simple virtual machine or even better look at llvm which is an open-source(ish) attempt to standardise the virtual machine byte code format and provide nice features like JITing ( turning your script compiled byte code into assembly ).
I wouldn't discourage you from the more advanced options that you machine such as native machine code execution but bear in mind that this is a very specialist area and gets real complex, real fast!
Earlz pointed out that my reply might seem to imply 'don't bother doing this yourself. Re-reading my post it does sound a bit that way. The reason I mentioned ANTLR and LLVM is they both have heaps of source code and tutorials so I feel this is a good reference source. Take it as a base and play
您可以尝试使用这个框架来构建语言(它与 XNA 配合良好):
http://www.meta-alternative.net/mbase.html
有一些教程:
http://www.meta-alternative.net/calc.pdf
http://www.meta-alternative.net/pfront.pdf
You can try this framework for building languages (it works well with XNA):
http://www.meta-alternative.net/mbase.html
There are some tutorials:
http://www.meta-alternative.net/calc.pdf
http://www.meta-alternative.net/pfront.pdf
Python 作为一种很棒的脚本语言。我建议您为其 C API 创建一个 C# 绑定并使用它。嵌入 Python 很容易。您的应用程序可以在Python解释器可以访问的模块内定义函数、类型/类和变量。应用程序还可以调用 Python 脚本中的函数并返回结果。这两个功能相结合为您提供了双向通信方案。
基本上,您可以免费获得 Python 语法和语义。您需要实现的是您的应用程序向 Python 公开的 API。一个例子是访问游戏逻辑函数和渲染函数。然后,Python 脚本将定义调用这些函数的函数,并且主机应用程序将调用 Python 函数(带参数)来完成工作。
编辑:似乎 IronPython 可以为您节省更多工作。它是 CPython 的 C# 实现,并且有自己的嵌入 API:http://www.ironpython.net/
Python is great as a scripting language. I would recommend you make a C# binding for its C API and use that. Embedding Python is easy. Your application can define functions, types/classes and variables inside modules which the Python interpreter can access. The application can also call functions in Python scripts and get a result back. These two features combined gives you a two-way communication scheme.
Basically, you get the Python syntax and semantics for free. What you would need to implement is the API your application exposes to Python. An example could be access to game logic functions and render functions. Python scripts would then define functions which calls these, and the host application would invoke the Python functions (with parameters) to get work done.
EDIT: Seems like IronPython can save you even more work. It's a C# implementation of CPython, and has its own embedding API: http://www.ironpython.net/