PyPy Translation error

554 Views Asked by At

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....

2

There are 2 best solutions below

0
On BEST ANSWER

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.

1
On

This is a very specific pypy-related question and you're more likely to get answers on #pypy on irc.freenode.net or asking on pypy-dev.

That said, you should not use interactive translation for that (among other things it has insane defaults). Instead you should write a target (look into translator/goal/targetnopstandalone.py for an example) and use translate.py to compile it.