我希望这是这个问题的正确位置。我正在尝试编写一个简单的支持 JIT 的解释器。但是,在翻译方面我遇到了一个奇怪的错误。我几乎逐字复制了 JavaScript 示例解析器,但这是我的问题:
首先,这是解析器(几乎 100% 像 JS 示例):
from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
from pypy.rlib.parsing.parsing import ParseError, Rule
import py
import sys
GFILE = py.magic.autopath().dirpath().join("grammar.txt")
try:
t = GFILE.read(mode="U")
regexs, rules, ToAST = parse_ebnf(t)
except ParseError, e:
print e.nice_error_message(filename=str(GFILE), source=t)
raise
parsef = make_parse_function(regexs, rules, eof=True)
def parse(code):
t = parsef(code)
return ToAST().transform(t)
和我的语法:
STRING: "\\"[^\\\\"]*\\"";
SYMBOL: "[A-Za-z+-_*<>]+";
KEYWORD: ":[A-Za-z+-_*<>]+";
INTEGER: "\-?([0-9]+)";
DECIMAL: "\-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][\+\-]?[0-9]+)?";
IGNORE: " |\n|\t|,";
value: <KEYWORD> | <SYMBOL> | <STRING> | <DECIMAL> | <INTEGER> |
<hash> | <vector> | <sexps>;
hash: ["{"] (entry [","])* entry ["}"];
vector: ["["] value* ["]"];
entry: STRING [":"] value;
sexps: ["("] value+ [")"];
我正在执行以下操作以将代码编译为 c:
import parse
t = Translation(parse.parse)
t.annotate([str])
t.rtype()
t.compile_c()
>>>
<---snip---->
File "/home/tbaldridge/pypy/pypy/translator/c/genc.py", line 339, in
getentrypointptr
self._wrapper = new_wrapper(self.entrypoint, self.translator)
File "/home/tbaldridge/pypy/pypy/translator/llsupport/wrapper.py",
line 57, in new_wrapper
r_to = pyobj_repr)
File "/home/tbaldridge/pypy/pypy/rpython/rtyper.py", line 931, in convertvar
(r_from, r_to))
pypy.rpython.error.TyperError: don't know how to convert from
<InstanceRepr for pypy.rlib.parsing.tree.Node> to <PyObjRepr *
PyObject>
我错过了什么?这看起来很直接......