2011-05-29 91 views
5

編程Lisp metacircular評估者的介紹編程類並不少見。有沒有嘗試過爲Python做這件事?Python meta-circular評估者

是的,我知道Lisp的結構和語法非常適合於一個元環評估者,等等.Python很可能會更困難。我只是好奇這樣的嘗試是否已經做出。

+1

[PyPy](http://pypy.org/) – JBernardo 2011-05-29 00:23:12

回答

7

對於那些不知道元圓評估者是什麼的人來說,它是一個用解釋語言編寫的解釋器。例如:用Lisp編寫的Lisp解釋器,或者在我們的例子中,用Python編寫的Python解釋器。欲瞭解更多信息,請致電read this chapter from SICP

由於JBernardo said,PyPy是1。然而,PyPy的Python解釋器(meta-circular評估器)是在一個名爲RPython的Python的子集中實現的。

從1.5版本開始,您將會很高興知道,PyPy完全符合官方的Python 2.7規範。更重要的是:PyPy nearly always beats Python在性能基準測試中。

欲瞭解更多信息,請參閱PyPy docsPyPy extra docs

0

我覺得我寫一個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) 
+0

Downvoted,因爲它不是一個?爲什麼不? – 2014-07-10 15:13:05