generating byte code from compiler.parse output

Discussion in 'Python' started by Chris Wright, Aug 8, 2003.

  1. Chris Wright

    Chris Wright Guest

    Is there a way to go from the AST output of compiler.paser to byte
    code

    i.e. in the example below, is there a way to compile m and get a code
    object that could then be called?

    cheers and thanks for the help

    chris wright

    >>> s = """

    .... def test(x):
    .... y = x + 1
    .... print x, y
    .... """
    >>> m = compiler.parse(s)
    >>> m

    Module(None, Stmt([Function('test', ['x'], [], 0, None,
    Stmt([Assign([AssName('y', 'OP_ASSIGN')], Add((Name('x'), Const(1)))),
    Printnl([Name('x'), Name('y')], None)]))]))
    >>>
     
    Chris Wright, Aug 8, 2003
    #1
    1. Advertising

  2. (Chris Wright) writes:

    > Is there a way to go from the AST output of compiler.paser to byte
    > code


    Yes. Use the source, Luke.

    > i.e. in the example below, is there a way to compile m and get a code
    > object that could then be called?


    You have to find out what compiler.compile is doing in addition to
    what you are doing. Then, you find, that you need misc.set_filename,
    syntax.check, ModuleCodeGenerator, and .getCode, in that order.

    HTH,
    Martin
     
    Martin v. =?iso-8859-15?q?L=F6wis?=, Aug 8, 2003
    #2
    1. Advertising

  3. Chris Wright

    Chris Wright Guest

    After Martin's gentle prodding, I came up with the code below
    (without error checking etc....)

    I am very grateful !

    cheers

    Chris

    ------------------

    from compiler import parse, syntax
    from misc import set_filename
    from pycodegen import ModuleCodeGenerator

    class Test:
    def __init__(self):
    pass

    def munge_code_string(s):
    m = parse(s)
    set_filename("<string>", m)
    syntax.check(m)
    print m
    gen = ModuleCodeGenerator(m)
    c = gen.getCode()
    return c


    code_string = """
    def f(x):
    print x
    """

    if __name__ == '__main__':
    t = Test()
    code = munge_code_string(code_string)
    exec code in globals(), t.__dict__
    print dir(t)
    t.f(4)

    (Martin v. Löwis) wrote in message news:<-berlin.de>...
    > (Chris Wright) writes:
    >
    > > Is there a way to go from the AST output of compiler.paser to byte
    > > code

    >
    > Yes. Use the source, Luke.
    >
    > > i.e. in the example below, is there a way to compile m and get a code
    > > object that could then be called?

    >
    > You have to find out what compiler.compile is doing in addition to
    > what you are doing. Then, you find, that you need misc.set_filename,
    > syntax.check, ModuleCodeGenerator, and .getCode, in that order.
    >
    > HTH,
    > Martin
     
    Chris Wright, Aug 8, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrey Batyuck

    Compiler compiler with C++ as output

    Andrey Batyuck, May 11, 2004, in forum: C++
    Replies:
    3
    Views:
    458
    Frederik Hertzum
    May 17, 2004
  2. chuck amadi
    Replies:
    1
    Views:
    509
    Larry Bates
    Jun 23, 2004
  3. RickMuller
    Replies:
    4
    Views:
    752
    Alexey Shamrin
    Mar 26, 2005
  4. napi
    Replies:
    1
    Views:
    367
    QuantumG
    Sep 25, 2004
  5. gelonida
    Replies:
    1
    Views:
    792
    Gabriel Genellina
    May 6, 2010
Loading...

Share This Page