[Compiler-sig] More Jython progress

Finn Bock bckfnn@worldonline.dk
Sun, 21 Apr 2002 12:49:02 GMT


Hi,

Based on the current AST tree and a modified CodeCompiler, I can now
generate javabytecode. I'm sure there are still a few bugs in the
generated code but it passes our initial (rather small) test suite.

The patch to current jython CVS is available here:

: http://sourceforge.net/tracker/?func=detail&atid=312867&aid=546737&group_id=12867

The next phase for Jython is to port jythonc (the java sourcecode
generator) to use the new AST tree.



Some observations about the AST:


I wonder if it would make sense to map the 'identifier' type to a Name
node. In some cases I create a Name node based on an identifier just so
it can play part of the visitor. From the end of visitFunctionDef():

   set(new Name(node.name, Name.Store));

Since the function name was initially parsed as a Name node anyway I
think it would be better to maintain the original Name node in the
FunctionDef node.


The ListComp and the way I uses it bugs me a little. I'll admit it is a
clever way of representing a listcomp but I have been reusing the
visitFor() and visitIf() methods to generate the loop and branching
code. Since I wanted to continue to do that I builds a series of For()
and If() statements from the listcomp:

    set(new Name(tmp_append, Name.Store));

    stmtType n = new Expr(new Call(new Name(tmp_append, Name.Load),
                                   new exprType[] { node.target },
                                   new keywordType[0], null, null));

    for (int i = node.generators.length - 1; i >= 0; i--) {
        listcompType lc = node.generators[i];
        for (int j = lc.ifs.length - 1; j >= 0; j--) {
            n = new If(lc.ifs[j], new stmtType[] { n }, null);
        }
        n = new For(lc.target, lc.iter, new stmtType[] { n }, null);
    }
    visit(n);
    visit(new Delete(new exprType[] {
	                 new Name(tmp_append, Name.Del) }));

I think it is quite clever to reuse the For() and If() codegen but I
don't like to create new nodes on the fly. These new nodes will not have
the right linenumbers and whatever other additional information that we
attach to the nodes. I would rather prefer that the For() and If() nodes
was stored in the ListComp node.


regards,
finn