简体   繁体   English

如果编译Python的exec和eval?

[英]If Python's exec and eval were compiled?

Can the equivalent of eval and exec exist in a compiled language? eval和exec的等价物是否可以用编译语言存在? If so, how would they be compiled (roughly speaking)? 如果是这样,他们将如何编译(粗略地说)?

Certainly, various Lisp environments have had this capability for decades. 当然,各种Lisp环境已有数十年的能力。 A Lisp compiler typically works on a per-function basis, and the compiler and runtime system work hand in hand. Lisp编译器通常基于每个函数工作,编译器和运行时系统协同工作。

When asked to eval something, the Lisp runtime environment will pass the list (a data structure) to the compiler for compiling. 当被要求eval某些内容时,Lisp运行时环境会将列表(数据结构)传递给编译器进行编译。 The compiler may generate machine code (or maybe bytecode, depending on the system), and then the function will be callable at the machine level just like every other function in the program. 编译器可以生成机器代码(或者可能是字节码,具体取决于系统),然后该函数将在机器级别调用,就像程序中的每个其他函数一样。

For starters, python is a compiled language, it just does the compilation at runtime. 对于初学者来说,python 一种编译语言,它只是在运行时进行编译。 That being said, all that you need to do to implement eval in any other compiled language is to be able to run the compiler (and dynamically load object code) - you can do this in Python (and a litany of other languages) easily because the compiler is an integral part of the runtime. 话虽这么说,在任何其他编译语言中实现eval所需要做的就是能够运行编译器(并动态加载目标代码) - 你可以很容易地用Python(和一连串其他语言)来做这件事,因为编译器是运行时的组成部分。 There's technically nothing that stops a program written in C from invoking the compiler and loading the result at runtime (using dlopen ), it's just not a common occurrence because the C runtime doesn't require a compiler, so most users don't have one. 从技术上讲,没有什么可以阻止用C编写的程序调用编译器并在运行时加载结果(使用dlopen ),这不常见,因为C运行时不需要编译器,因此大多数用户没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM