Tools for AST, and moving towards a JsPy (or PyJs) AST

2 views
Skip to first unread message

Jonathan Fine

unread,
May 9, 2010, 8:52:16 AM5/9/10
to JavaScript for Python programmers
Hi

I've done some work on a framework for AST translation.  My starting point is that many Unix programs (such as grep) are an adaption of copying stdin to stdout.

Here we want Python source in and JavaScript source out.  And we're going via an AST.

So our starting point (similar to cat) is

1)  Python source to Python AST (already provided by ast.parse).

2)  Python AST to Python AST (but a deep copy, not simply returning the argument).

3)  Python AST to Python source.

I've written some code that takes us in that direction.  In particular it defines an AbstractSyntax base class and if you inherit from it you get a deep copy.  I've also defined and AbstractTranslator base class, which provides a framework for all the syntax class translation functions.

It's in the toys branch, at http://bitbucket.org/PeterRust/py2js/changeset/6de4c091c459

The next tasks are to complete (3) and also

4)  Python AST to JsPy AST

The JsPy AST is similar to the Python AST (and can share many of its classes if we wish) but it provides the appropriate starting point for generating JavaScript, and for further transformations.

Thus, a JsPy AST contains only items we can deal with in JavaScript.  It would contain Yield, for example.  Each function will have Locals and Globals fields.

The basic idea of a JsPy AST is that one can easily generated JavaScript from it, and one can easily transform it in various ways.

--
Jonathan

--
You received this message because you are subscribed to the Google Groups "JavaScript for Python programmers" group.
To post to this group, send an email to js...@googlegroups.com.
To unsubscribe from this group, send email to js4py+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/js4py?hl=en-GB.

Jonathan Fine

unread,
May 9, 2010, 1:48:48 PM5/9/10
to JavaScript for Python programmers
On Sun, May 9, 2010 at 1:52 PM, Jonathan Fine <jonatha...@googlemail.com> wrote:
Hi

I've done some work on a framework for AST translation.  My starting point is that many Unix programs (such as grep) are an adaption of copying stdin to stdout.

I've made progress and pushed a few more changes.  Please take a look, if you have time.  The latest version is at
    http://bitbucket.org/PeterRust/py2js/src/c8ca5bbaf03c/toys/py-to-py.py

What I like is about my approach is that it separates the generic code related to translating abstract syntax trees from the code that is specific to the particular abstract syntax.  As a result, the code required for a Python AST to source translator is quite simple (once one has improved the Python AST for this particular purpose).


Jonathan 

Reply all
Reply to author
Forward
0 new messages