5

I have some code available in some form of AST and I would like to execute it.

I can think of several ways to do this, e.g.:

  • Just straight-forwardly interpret it.
  • Translate it into a Python AST (the ast module) and
    • Python-compile that or
    • Python-eval that.
  • Translate it into Python source code (e.g. a pure string) and
    • Python-compile that or
    • Python-eval that.
  • Translate it in some form of low level code and write a simple VM in Python which runs that.

I guess I would get the fasted execution by translating it into a Python AST, compile that and run that. Esp. when using PyPy, I might even get improvements by PyPys JIT compiling optimizations (I hope I do, do I?).

Can you think of other possibilities? Can you give suggestions on what might be the best way?

11
  • 1
    @S.Lott: The code is not Python code. Commented Jul 8, 2011 at 15:48
  • 1
    @S.Lott: the question just says "some form as AST", so it's not necessarily Python. Commented Jul 8, 2011 at 16:05
  • 1
    @S.Lott It's pretty clear from the question... Commented Jul 8, 2011 at 17:17
  • 1
    @S.Lott: I've seen my share of bad questions too, but I'm not disillusioned enough (yet) to assume OP misses such an obvious solution. The question states he's thinking about translating his AST into Python code or AST, so I too think it's pretty obvious that the code at hand it not Python. Commented Jul 8, 2011 at 17:26
  • 2
    @S.Lott: No, you are not asked to make a clever deduction. You are asked to make one very simple deduction. You could also ask for clarification, but I don't see how the question could be taken with certainty as "I have Python code, how do I interpret it?". If you're demanding clarity, great, then demand clarity instead of guessing (that's what you did by assuming the AST in question is a Python AST). Commented Jul 8, 2011 at 17:31

2 Answers 2

1

Another possibility: translate to Cython code, write out to a file, compile with Cython then a C compiler with optimization turned on, load the resulting module and execute it.

If the code has type annotations that can be translated to Cython/C types, this can be blazing fast. Watch out, though, as Cython is in beta and still a bit rough around the edges. Also, this solution only works for CPython.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, also a nice idea. The only problem which will probably keep me from considering this is that this is not a pure Python solution anymore. I should have written that in my question that I am searching for such. Anyway, if that wouldn't be an issue, this solution might be quite fast. Although I could maybe also just output C code and use GCC/LLVM. Or I could generate LLVM bytecode.
0

Another possibility: translate the AST directly to Python byte code and execute that. This is like your last idea except using the existing Python VM.

It is not a great possibility because it could be a lot of work and Python compile would probably do a better job except in rather peculiar cases, but I'm just throwing it out there.

4 Comments

No, this is a rather bad possibility, as the bytecode format is propably not set in stone across versions and/or implementation. Plus, translating to something as low-level as bytecode may be (if the source language is somewhat close to Python) harder than translating to a high-level language like Python, and also less supported.
Good caveats. The bytecode does change in some Python versions, but fortunately there's dis.opmap[] for insulating yourself from that. The main issue is that it's probably more work than translating to Python source or AST.
Also, when running in PyPy and using PyPys compile on a Python AST, I probably get much better performance when running that as when running (i.e. interpreting) the Python bytecode with PyPy, don't I?
I'm pretty sure PyPy's JIT compiler operates on Python bytecode, so it shouldn't make any difference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.