Search in sources :

Example 1 with Directive

use of net.jangaroo.jooc.ast.Directive in project jangaroo-tools by CoreMedia.

the class JsCodeGenerator method visitClassBody.

@Override
public void visitClassBody(ClassBody classBody) throws IOException {
    out.writeSymbolWhitespace(classBody.getLBrace());
    boolean inStaticInitializerBlock = false;
    for (Directive directive : classBody.getDirectives()) {
        final boolean isStaticInitializer = directive instanceof Statement && !(directive instanceof Declaration);
        if (isStaticInitializer) {
            inStaticInitializerBlock = beginStaticInitializer(out, inStaticInitializerBlock);
        } else {
            inStaticInitializerBlock = endStaticInitializer(out, inStaticInitializerBlock);
        }
        directive.visit(this);
    }
    endStaticInitializer(out, inStaticInitializerBlock);
    out.writeSymbolWhitespace(classBody.getRBrace());
}
Also used : ForInStatement(net.jangaroo.jooc.ast.ForInStatement) CaseStatement(net.jangaroo.jooc.ast.CaseStatement) WhileStatement(net.jangaroo.jooc.ast.WhileStatement) DoStatement(net.jangaroo.jooc.ast.DoStatement) SemicolonTerminatedStatement(net.jangaroo.jooc.ast.SemicolonTerminatedStatement) EmptyStatement(net.jangaroo.jooc.ast.EmptyStatement) SuperConstructorCallStatement(net.jangaroo.jooc.ast.SuperConstructorCallStatement) ReturnStatement(net.jangaroo.jooc.ast.ReturnStatement) ContinueStatement(net.jangaroo.jooc.ast.ContinueStatement) LabeledStatement(net.jangaroo.jooc.ast.LabeledStatement) SwitchStatement(net.jangaroo.jooc.ast.SwitchStatement) DefaultStatement(net.jangaroo.jooc.ast.DefaultStatement) Statement(net.jangaroo.jooc.ast.Statement) TryStatement(net.jangaroo.jooc.ast.TryStatement) BreakStatement(net.jangaroo.jooc.ast.BreakStatement) BlockStatement(net.jangaroo.jooc.ast.BlockStatement) IfStatement(net.jangaroo.jooc.ast.IfStatement) ForStatement(net.jangaroo.jooc.ast.ForStatement) ThrowStatement(net.jangaroo.jooc.ast.ThrowStatement) NamespaceDeclaration(net.jangaroo.jooc.ast.NamespaceDeclaration) ClassDeclaration(net.jangaroo.jooc.ast.ClassDeclaration) FunctionDeclaration(net.jangaroo.jooc.ast.FunctionDeclaration) IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration) PackageDeclaration(net.jangaroo.jooc.ast.PackageDeclaration) Declaration(net.jangaroo.jooc.ast.Declaration) VariableDeclaration(net.jangaroo.jooc.ast.VariableDeclaration) EmptyDeclaration(net.jangaroo.jooc.ast.EmptyDeclaration) TypedIdeDeclaration(net.jangaroo.jooc.ast.TypedIdeDeclaration) UseNamespaceDirective(net.jangaroo.jooc.ast.UseNamespaceDirective) Directive(net.jangaroo.jooc.ast.Directive) ImportDirective(net.jangaroo.jooc.ast.ImportDirective)

Example 2 with Directive

use of net.jangaroo.jooc.ast.Directive in project jangaroo-tools by CoreMedia.

the class JsCodeGenerator method visitForInStatement.

@Override
public void visitForInStatement(final ForInStatement forInStatement) throws IOException {
    final Ide exprAuxIde = forInStatement.getExprAuxIde();
    IdeDeclaration exprType = forInStatement.getExpr().getType();
    String exprTypeName = exprType != null ? exprType.getQualifiedNameStr() : "";
    boolean iterateArrayMode = "Array".equals(exprTypeName) || "Vector$object".equals(exprTypeName);
    if (exprAuxIde != null && !iterateArrayMode) {
        new SemicolonTerminatedStatement(new VariableDeclaration(SYM_VAR, exprAuxIde, null, null), SYM_SEMICOLON).visit(this);
    }
    out.writeSymbol(forInStatement.getSymKeyword());
    final boolean isForEach = forInStatement.getSymEach() != null;
    if (isForEach) {
        out.beginComment();
        out.writeSymbol(forInStatement.getSymEach());
        out.endComment();
    }
    out.writeSymbol(forInStatement.getLParen());
    if (isForEach || iterateArrayMode) {
        new VariableDeclaration(SYM_VAR, forInStatement.getAuxIde(), null, null).visit(this);
    } else {
        if (forInStatement.getDecl() != null) {
            forInStatement.getDecl().visit(this);
        } else {
            forInStatement.getLValue().visit(this);
        }
    }
    if (iterateArrayMode) {
        String indexVarName = forInStatement.getAuxIde().getName();
        out.write("=0");
        if (exprAuxIde != null) {
            out.write(",");
            out.writeToken(exprAuxIde.getName());
            out.writeToken("=");
            out.beginComment();
            out.writeSymbol(forInStatement.getSymIn());
            out.endComment();
            forInStatement.getExpr().visit(this);
        }
        out.write(";");
        out.write(indexVarName);
        out.write("<");
        if (exprAuxIde != null) {
            out.writeToken(exprAuxIde.getName());
        } else {
            out.beginComment();
            out.writeSymbol(forInStatement.getSymIn());
            out.endComment();
            forInStatement.getExpr().visit(this);
        }
        out.write(".length;");
        out.write("++" + indexVarName);
    } else {
        out.writeSymbol(forInStatement.getSymIn());
        if (exprAuxIde != null) {
            // assign the expression value to the auxiliary expression value variable once:
            out.writeToken(exprAuxIde.getName());
            out.writeToken("=");
        }
        forInStatement.getExpr().visit(this);
    }
    out.writeSymbol(forInStatement.getRParen());
    if (isForEach || iterateArrayMode) {
        // inject synthesized statement into loop body:
        if (!(forInStatement.getBody() instanceof BlockStatement)) {
            forInStatement.setBody(new BlockStatement(SYM_LBRACE, Arrays.<Directive>asList(forInStatement.getBody()), SYM_RBRACE));
        }
        ((BlockStatement) forInStatement.getBody()).addBlockStartCodeGenerator(new CodeGenerator() {

            @Override
            public void generate(JsWriter out, boolean first) throws IOException {
                // synthesize assigning the correct index to the variable given in the original for each statement:
                if (forInStatement.getDecl() != null) {
                    forInStatement.getDecl().visit(JsCodeGenerator.this);
                } else {
                    forInStatement.getLValue().visit(JsCodeGenerator.this);
                }
                out.writeToken("=");
                if (!isForEach) {
                    out.write("String(" + forInStatement.getAuxIde().getName() + ")");
                } else {
                    if (exprAuxIde == null) {
                        forInStatement.getExpr().visit(JsCodeGenerator.this);
                    } else {
                        out.write(exprAuxIde.getName());
                    }
                    out.write("[" + forInStatement.getAuxIde().getName() + "]");
                }
                out.write(";");
            }
        });
    }
    forInStatement.getBody().visit(this);
}
Also used : IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration) TypedIdeDeclaration(net.jangaroo.jooc.ast.TypedIdeDeclaration) VariableDeclaration(net.jangaroo.jooc.ast.VariableDeclaration) BlockStatement(net.jangaroo.jooc.ast.BlockStatement) Ide(net.jangaroo.jooc.ast.Ide) NamespacedIde(net.jangaroo.jooc.ast.NamespacedIde) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde) CodeGenerator(net.jangaroo.jooc.CodeGenerator) IOException(java.io.IOException) SemicolonTerminatedStatement(net.jangaroo.jooc.ast.SemicolonTerminatedStatement) UseNamespaceDirective(net.jangaroo.jooc.ast.UseNamespaceDirective) Directive(net.jangaroo.jooc.ast.Directive) ImportDirective(net.jangaroo.jooc.ast.ImportDirective) JsWriter(net.jangaroo.jooc.JsWriter)

Aggregations

BlockStatement (net.jangaroo.jooc.ast.BlockStatement)2 Directive (net.jangaroo.jooc.ast.Directive)2 IdeDeclaration (net.jangaroo.jooc.ast.IdeDeclaration)2 ImportDirective (net.jangaroo.jooc.ast.ImportDirective)2 SemicolonTerminatedStatement (net.jangaroo.jooc.ast.SemicolonTerminatedStatement)2 TypedIdeDeclaration (net.jangaroo.jooc.ast.TypedIdeDeclaration)2 UseNamespaceDirective (net.jangaroo.jooc.ast.UseNamespaceDirective)2 VariableDeclaration (net.jangaroo.jooc.ast.VariableDeclaration)2 IOException (java.io.IOException)1 CodeGenerator (net.jangaroo.jooc.CodeGenerator)1 JsWriter (net.jangaroo.jooc.JsWriter)1 BreakStatement (net.jangaroo.jooc.ast.BreakStatement)1 CaseStatement (net.jangaroo.jooc.ast.CaseStatement)1 ClassDeclaration (net.jangaroo.jooc.ast.ClassDeclaration)1 ContinueStatement (net.jangaroo.jooc.ast.ContinueStatement)1 Declaration (net.jangaroo.jooc.ast.Declaration)1 DefaultStatement (net.jangaroo.jooc.ast.DefaultStatement)1 DoStatement (net.jangaroo.jooc.ast.DoStatement)1 EmptyDeclaration (net.jangaroo.jooc.ast.EmptyDeclaration)1 EmptyStatement (net.jangaroo.jooc.ast.EmptyStatement)1