对于入门编程类来说,编写Lisp元循环计算器并不少见。有没有人尝试过为Python做这件事?
是的,我知道Lisp的结构和语法很适合于元循环计算器,等等。Python很可能会更难。我只是好奇是否有人做过这样的尝试。
发布于 2011-05-30 08:04:17
对于那些不知道什么是元循环计算器的人来说,它是一个用要解释的语言编写的解释器。例如:用Lisp编写的Lisp解释器,或者在我们的例子中,用Python编写的Python解释器。有关更多信息,请访问read this chapter from SICP。
作为JBernardo said,PyPy就是其中之一。然而,PyPy的Python解释器,也就是元循环计算器,是在Python的静态类型子集RPython中实现的。
您将很高兴地了解到,从1.5版开始,PyPy完全兼容官方的Python2.7规范。更重要的是:性能基准测试中的PyPy nearly always beats Python。
有关更多信息,请参阅PyPy docs和PyPy 。
发布于 2013-08-15 21:30:08
我想我写了一个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)https://stackoverflow.com/questions/6165177
复制相似问题