generating byte code from compiler.parse output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Wright

    generating byte code from compiler.parse output

    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
    [color=blue][color=green][color=darkred]
    >>> s = """[/color][/color][/color]
    .... def test(x):
    .... y = x + 1
    .... print x, y
    .... """[color=blue][color=green][color=darkred]
    >>> m = compiler.parse( s)
    >>> m[/color][/color][/color]
    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)]))]))[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
  • Martin v. Löwis

    #2
    Re: generating byte code from compiler.parse output

    [email protected] (Chris Wright) writes:
    [color=blue]
    > Is there a way to go from the AST output of compiler.paser to byte
    > code[/color]

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

    You have to find out what compiler.compil e is doing in addition to
    what you are doing. Then, you find, that you need misc.set_filena me,
    syntax.check, ModuleCodeGener ator, and .getCode, in that order.

    HTH,
    Martin

    Comment

    • Chris Wright

      #3
      Re: generating byte code from compiler.parse output

      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 ModuleCodeGener ator

      class Test:
      def __init__(self):
      pass

      def munge_code_stri ng(s):
      m = parse(s)
      set_filename("< string>", m)
      syntax.check(m)
      print m
      gen = ModuleCodeGener ator(m)
      c = gen.getCode()
      return c


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

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

      [email protected] .de (Martin v. Löwis) wrote in message news:<m3wudoadz [email protected] rmatik.hu-berlin.de>...[color=blue]
      > [email protected] (Chris Wright) writes:
      >[color=green]
      > > Is there a way to go from the AST output of compiler.paser to byte
      > > code[/color]
      >
      > Yes. Use the source, Luke.
      >[color=green]
      > > i.e. in the example below, is there a way to compile m and get a code
      > > object that could then be called?[/color]
      >
      > You have to find out what compiler.compil e is doing in addition to
      > what you are doing. Then, you find, that you need misc.set_filena me,
      > syntax.check, ModuleCodeGener ator, and .getCode, in that order.
      >
      > HTH,
      > Martin[/color]

      Comment

      Working...