编译动态语言
我寻找一种存在编译器并且支持自修改代码的编程语言。我听说 Lisp 支持这些功能,但我想知道是否有一种更像 C/C++/D 的语言具有这些功能。
为了澄清我的意思:
我希望能够以某种方式在运行时访问程序代码并对其应用任何类型的更改,即删除命令、添加命令、更改它们。 就好像我有我的程序的 AstTree 一样。当然,我不能用编译语言来生成该树,所以它必须以不同的方式完成。编译需要将自修改命令转换为其二进制等效修改,以便它们可以在运行时与编译的代码一起工作。
我不想依赖虚拟机,这就是我编译的意思:)
I search for a programming language for which a compiler exists and that supports self modifying code. I’ve heared that Lisp supports these features, but I was wondering if there is a more C/C++/D-Like language with these features.
To clarify what I mean:
I want to be able to have in some way access to the programms code at runtime and apply any kind of changes to it, that is, removing commands, adding commands, changing them.
As if i had the AstTree of my programm. Of course i can’t have that tree in a compiled language, so it must be done different. The compile would need to translate the self-modifying commands into their binary equivalent modifications so they would work in runtime with the compiled code.
I don’t want to be dependent on an VM, thats what i meant with compiled :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Lisp 之所以如此,或许是有原因的? Lisp 旨在对其他语言进行编程并使用代码和数据的符号表示进行计算。代码和数据之间的界限不再存在。这会影响编程语言的设计和实现。
Lisp 具有生成新代码、翻译该代码并执行它的语法功能。因此,预解析的代码也使用与其他程序相同的数据结构(符号、列表、数字、字符……)。
Lisp 在运行时知道它的数据——你可以查询所有内容的类型或类。类本身就是对象,函数也是对象。因此,编程语言和程序的这些元素也是一流的对象,它们可以被这样操作。 动态语言与“动态类型”无关。
“动态语言”意味着可以查看编程语言的元素(例如通过元类和元对象协议)和程序(其类、函数、方法、槽、继承等)可能在运行时运行并且可以在运行时修改。
可能你添加到一种语言中的这些特性越多,它就越像 Lisp。因为 Lisp 几乎是一种简单、动态、可编程编程语言的局部最大值。如果您想要其中一些功能,那么您可能需要考虑您必须放弃或愿意放弃其他程序语言的哪些功能。例如,对于简单的代码即数据语言,整个 C 语法模型可能不实用。
因此,类 C 语言和“动态语言”可能并不真正适合——语法只是整个图景的一部分。但即使是 C 语法模型也限制了我们使用动态语言的容易程度。
Probably there is a reason Lisp is like it is? Lisp was designed to program other languages and to compute with symbolic representations of code and data. The boundary between code and data is no longer there. This influences the design AND the implementation of a programming language.
Lisp has got its syntactical features to generate new code, translate that code and execute it. Thus pre-parsed code is also using the same data structures (symbols, lists, numbers, characters, ...) that are used for other programs, too.
Lisp knows its data at runtime - you can query everything for its type or class. Classes are objects themselves, as are functions. So these elements of the programming language and the programs also are first-class objects, they can be manipulated as such. Dynamic language has nothing to do with 'dynamic typing'.
'Dynamic language' means that the elements of the programming language (for example via meta classes and the meta-object protocol) and the program (its classes, functions, methods, slots, inheritance, ...) can be looked at runtime and can be modified at runtime.
Probably the more of these features you add to a language, the more it will look like Lisp. Since Lisp is pretty much the local maximum of a simple, dynamic, programmable programming language. If you want some of these features, then you might want to think which features of your other program language you have to give up or are willing to give up. For example for a simple code-as-data language, the whole C syntax model might not be practical.
So C-like and 'dynamic language' might not really be a good fit - the syntax is one part of the whole picture. But even the C syntax model limits us how easy we can work with a dynamic language.
C# 始终允许自修改代码。
C# has always allowed for self-modifying code.
您可能需要考虑使用 C++ 和 LLVM 来生成(大部分)可移植代码。您甚至可以引入 clang 来在 C 解析树中工作(请注意,clang 对 C++ 的支持不完整) 例如,您可以用 C ++
编写一个自修改核心来与 clang 和 LLVM 交互,并用 C 编写程序的其余部分。将主程序的解析树与自修改树一起存储修改代码,然后在运行时使用 clang 对其进行操作。 Clang 可以让您以任何方式直接操作 AST 树,然后将其一直编译为机器代码。
请记住,使用编译语言操作 AST 始终意味着在程序中包含编译器(或解释器)。 LLVM 只是一个简单的选择。
You might want to consider using C++ with LLVM for (mostly) portable code generation. You can even pull in clang as well to work in C parse trees (note that clang has incomplete support for C++ currently, but is written in C++ itself)
For example, you could write a self-modification core in C++ to interface with clang and LLVM, and the rest of the program in C. Store the parse tree for the main program alongside the self-modification code, then manipulate it with clang at runtime. Clang will let you directly manipulate the AST tree in any way, then compile it all the way down to machine code.
Keep in mind that manipulating your AST in a compiled language will always mean including a compiler (or interpreter) with your program. LLVM is just an easy option for this.
JavaScirpt + V8(Chrome JavaScript 编译器)
JavaScript 是
现在你可以用 V8 编译它: http://code .google.com/p/v8/
JavaScirpt + V8 (the Chrome JavaScript compiler)
JavaScript is
And you now can compile it with V8: http://code.google.com/p/v8/
“动态语言”是一个广泛的术语,涵盖了各种各样的概念。 C# 4.0 支持动态类型,C# 4.0 是一种编译语言。 Objective-C 还支持动态语言的一些功能。然而,在支持自修改代码方面,它们都无法接近 Lisp。
为了支持这种程度的动态性和自修改代码,您应该有一个功能齐全的编译器在运行时调用;这几乎就是真正的口译员。
"Dynamic language" is a broad term that covers a wide variety of concepts. Dynamic typing is supported by C# 4.0 which is a compiled language. Objective-C also supports some features of dynamic languages. However, none of them are even close to Lisp in terms of supporting self modifying code.
To support such a degree of dynamism and self-modifying code, you should have a full-featured compiler to call at run time; this is pretty much what an interpreter really is.
尝试一下吧。它是一种基于 Java-JVM 的动态语言,在运行时编译。它应该能够执行自己的代码。
http://groovy.codehaus.org/
否则,你总是有 Perl、PHP 等。 ..但正如您所建议的,这些并不是类似 C/C++/D 的语言。
Try groovy. It's a dynamic Java-JVM based language that is compiled at runtime. It should be able to execute its own code.
http://groovy.codehaus.org/
Otherwise, you've always got Perl, PHP, etc... but those are not, as you suggest, C/C++/D- like languages.
如果这就是您所寻找的,我推荐Python或Ruby。它们都可以在自己的虚拟机、JVM 和 .Net CLR 上运行。因此,您可以选择任何您想要的运行时。两者中,Ruby 似乎拥有更多的元编程设施,但 Python 似乎在其他平台上有更成熟的实现。
If that's all you're looking for, I'd recommend Python or Ruby. They can both run on their own virtual machines and the JVM and the .Net CLR. Thus, you can choose any runtime you want. Of the two, Ruby seems to have more meta-programming facilities, but Python seems to have more mature implementations on other platforms.