I hope this is the right place for this question. I'm trying to write a simple JIT-enabled interpreter. However, I'm hitting a odd error when it comes to translation. I've copied the JavaScript example parser almost verbatim, but here's my issues:
First of all, here's parser (almost 100% like the JS example):
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)
and my grammar:
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+ [")"];
I'm doing the following to compile the code to 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>
What am I missing? This seemed so straight-forward....
Your function returns a RPython-level AST node, which is a C structure after translation. The translator does not know how to represent this in the Python interpreter, to return the result when the function is called.
in C, a main() function can only return an integer. In pypy, the interactive translator can pass and return strings, ints, floats and tuples. You should process the ast nodes and return basic types.