Compile AST to bytecode?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob De Almeida

    #1

    Compile AST to bytecode?

    Hi,

    I would like to compile an AST to bytecode, so I can eval it later. I
    tried using parse.compileas t, but it fails:
    >>import compiler, parser
    >>ast = compiler.parse( "42")
    >>parser.compil east(ast)
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: compilest() argument 1 must be parser.st, not instance

    Any hints?

    TIA,
    --Rob

  • Duncan Booth

    #2
    Re: Compile AST to bytecode?

    "Rob De Almeida" <ralmeida@gmail .comwrote:
    I would like to compile an AST to bytecode, so I can eval it later. I
    tried using parse.compileas t, but it fails:
    >
    >>>import compiler, parser
    >>>ast = compiler.parse( "42")
    >>>parser.compi least(ast)
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: compilest() argument 1 must be parser.st, not instance
    >
    Any hints?
    There are two distinct varieties of AST in the Python standard library.
    compiler and parser ast objects are not compatible.

    I'm not sure there are any properly documented functions for converting an
    AST to a code object, so your best bet may be to examine what a
    pycodegen class like Expression or Module actually does.
    >>import compiler
    >>def ast_to_evalcode (ast):
    return compiler.pycode gen.ExpressionC odeGenerator(as t).getCode()
    >>def parse_to_ast(sr c):
    return compiler.pycode gen.Expression( src, "<string>")._ge t_tree()
    >>ast = parse_to_ast("4 0+2")
    >>ast
    Expression(Add( (Const(40), Const(2))))
    >>ast_to_evalco de(ast)
    <code object <expressionat 0122D848, file "<string>", line -1>
    >>eval(_)
    42

    Comment

    • Rob De Almeida

      #3
      Re: Compile AST to bytecode?

      Duncan Booth wrote:
      I would like to compile an AST to bytecode, so I can eval it later.
      I'm not sure there are any properly documented functions for converting an
      AST to a code object, so your best bet may be to examine what a
      pycodegen class like Expression or Module actually does.
      Thanks, Duncan. It worked perfectly. :-)

      For arbitrary nodes I just had to wrap them inside an Expression node:
      >>ast = compiler.ast.Ex pression(node)
      >>ast.filenam e = 'dummy'
      >>c = compiler.pycode gen.ExpressionC odeGenerator(as t)
      >>obj = eval(c.getCode( ), scope)
      --Rob

      Comment

      • fumanchu

        #4
        Re: Compile AST to bytecode?

        Rob De Almeida wrote:
        Duncan Booth wrote:
        I would like to compile an AST to bytecode, so I can eval it later.
        I'm not sure there are any properly documented functions for converting an
        AST to a code object, so your best bet may be to examine what a
        pycodegen class like Expression or Module actually does.
        >
        Thanks, Duncan. It worked perfectly. :-)
        >
        For arbitrary nodes I just had to wrap them inside an Expression node:
        >
        >ast = compiler.ast.Ex pression(node)
        >ast.filename = 'dummy'
        >c = compiler.pycode gen.ExpressionC odeGenerator(as t)
        >obj = eval(c.getCode( ), scope)
        If you're only worried about expressions, then you can have CPython do
        the compilation for you much faster by wrapping the expression in a
        lambda:
        >>f = lambda: 42
        >>f.func_code.c o_code
        'd\x01\x00S'
        >>map(ord, _)
        [100, 1, 0, 83]
        >>g = lambda x, y: (x * 3) + len(y)
        >>map(ord, g.func_code.co_ code)
        [124, 0, 0, 100, 1, 0, 20, 116, 1, 0, 124, 1, 0, 131, 1, 0, 23, 83]

        For a complete round-trip Expression library, see



        Robert Brewer
        System Architect
        Amor Ministries
        fumanchu@amor.o rg

        Comment

        Working...