Python元循环评估器

发布于 2024-11-10 03:58:14 字数 125 浏览 2 评论 0原文

在编程入门课程中编写 Lisp 元循环求值器的情况并不少见。有没有尝试为 Python 做这件事?

是的,我知道 Lisp 的结构和语法非常适合元循环求值器等。Python 很可能会更困难。我只是好奇是否有过这样的尝试。

It's not uncommon for an intro programming class to write a Lisp metacircular evaluator. Has there been any attempt at doing this for Python?

Yes, I know that Lisp's structure and syntax lends itself nicely to a metacircular evaluator, etc etc. Python will most likely be more difficult. I am just curious as to whether such an attempt has been made.

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

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

发布评论

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

评论(2

中二柚 2024-11-17 03:58:14

对于那些不知道什么是元循环评估器的人来说,它是一个用要解释的语言编写的解释器。例如:用 Lisp 编写的 Lisp 解释器,或者在我们的例子中,用 Python 编写的 Python 解释器。如需了解更多信息,阅读 SICP 的本章

正如 JBernardo 所说PyPy 就是其中之一。然而,PyPy 的 Python 解释器(即元循环求值器)是在名为 RPython.

您会很高兴地知道,从 1.5 版本开始,PyPy 完全符合官方 Python 2.7 规范。更重要的是:PyPy 在性能基准测试中几乎总是击败 Python

有关详细信息,请参阅 PyPy 文档PyPy 额外文档

For those who don't know what a meta-circular evaluator is, it is an interpreter which is written in the language to be interpreted. For example: a Lisp interpreter written in Lisp, or in our case, a Python interpreter written in Python. For more information, read this chapter from SICP.

As JBernardo said, PyPy is one. However, PyPy's Python interpreter, the meta-circular evaluator that is, is implemented in a statically typed subset of Python called RPython.

You'll be pleased to know that, as of the 1.5 release, PyPy is fully compliant with the official Python 2.7 specification. Even more so: PyPy nearly always beats Python in performance benchmarks.

For more information see PyPy docs and PyPy extra docs.

拥有 2024-11-17 03:58:14

我想我在这里写了一个

"""
Metacircular Python interpreter with macro feature.
By Cees Timmerman, 14aug13.
"""

import re
re_macros = re.compile("^#define (\S+) ([^\r\n]+)", re.MULTILINE)

def meta_python_exec(code):
    # Optional meta feature.
    macros = re_macros.findall(code)
    code = re_macros.sub("", code)
    for m in macros:
        code = code.replace(m[0], m[1])

    # Run the code.
    exec(code)

if __name__ == "__main__":
    #code = open("metacircular_overflow.py", "r").read()  # Causes a stack overflow in Python 3.2.3, but simply raises "RuntimeError: maximum recursion depth exceeded while calling a Python object" in Python 2.7.3.
    code = "#define 1 2\r\nprint(1 + 1)"
    meta_python_exec(code)

I think i wrote one here:

"""
Metacircular Python interpreter with macro feature.
By Cees Timmerman, 14aug13.
"""

import re
re_macros = re.compile("^#define (\S+) ([^\r\n]+)", re.MULTILINE)

def meta_python_exec(code):
    # Optional meta feature.
    macros = re_macros.findall(code)
    code = re_macros.sub("", code)
    for m in macros:
        code = code.replace(m[0], m[1])

    # Run the code.
    exec(code)

if __name__ == "__main__":
    #code = open("metacircular_overflow.py", "r").read()  # Causes a stack overflow in Python 3.2.3, but simply raises "RuntimeError: maximum recursion depth exceeded while calling a Python object" in Python 2.7.3.
    code = "#define 1 2\r\nprint(1 + 1)"
    meta_python_exec(code)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文