Search in sources :

Example 1 with Ide

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

the class DeclarationScope method declareIde.

@Override
public IdeDeclaration declareIde(IdeDeclaration decl) {
    final Ide ide = decl.getIde();
    final String name = ide.getName();
    if (importsByName.containsKey(name)) {
        throw new CompilerError(ide.getSymbol(), "attempt to redefine an imported identifier " + name);
    }
    if (AUX_VAR_NAME_PATTERN.matcher(name).matches()) {
        DeclarationScope packageDeclarationScope = getPackageDeclarationScope();
        if (packageDeclarationScope != null && packageDeclarationScope != this) {
            // also declare local auxiliary vars in package scope to reserve them so they are not used for package names:
            new VariableDeclaration(null, new Ide(name), null).scope(packageDeclarationScope);
        }
    }
    return ides.put(name, decl);
}
Also used : VariableDeclaration(net.jangaroo.jooc.ast.VariableDeclaration) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde) Ide(net.jangaroo.jooc.ast.Ide)

Example 2 with Ide

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

the class JsCodeGenerator method generateSuperConstructorCallCode.

private void generateSuperConstructorCallCode(ClassDeclaration classDeclaration, ParenthesizedExpr<CommaSeparatedList<Expr>> args) throws IOException {
    String superClassQName = classDeclaration.getSuperTypeDeclaration().getQualifiedNameStr();
    if ("Error".equals(superClassQName)) {
        // built-in Error constructor called as function unfortunately always creates a new Error object, so we have to use emulation provided by Jangaroo Runtime:
        out.write("joo.Error");
    } else {
        Ide superClassIde = classDeclaration.getSuperType().getIde();
        out.writeSymbolWhitespace(superClassIde.getSymbol());
        IdeDeclaration superClassDeclaration = superClassIde.getDeclaration();
        String packageName = superClassDeclaration.getPackageDeclaration().getQualifiedNameStr();
        String qName = superClassDeclaration.getName();
        if (packageName.length() > 0) {
            String packageAuxVar = compilationUnit.getAuxVarForPackage(packageName);
            qName = CompilerUtils.qName(packageAuxVar, qName);
        }
        out.write(qName);
    }
    out.writeToken(".call");
    if (args == null) {
        out.writeToken("(this)");
    } else {
        out.writeSymbol(args.getLParen());
        out.writeToken("this");
        CommaSeparatedList<Expr> arguments = args.getExpr();
        if (arguments != null) {
            if (arguments.getHead() != null) {
                out.writeToken(",");
            }
            arguments.visit(this);
        }
        out.writeSymbol(args.getRParen());
    }
}
Also used : IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration) TypedIdeDeclaration(net.jangaroo.jooc.ast.TypedIdeDeclaration) IdeExpr(net.jangaroo.jooc.ast.IdeExpr) ParenthesizedExpr(net.jangaroo.jooc.ast.ParenthesizedExpr) DotExpr(net.jangaroo.jooc.ast.DotExpr) AssignmentOpExpr(net.jangaroo.jooc.ast.AssignmentOpExpr) Expr(net.jangaroo.jooc.ast.Expr) FunctionExpr(net.jangaroo.jooc.ast.FunctionExpr) ArrayIndexExpr(net.jangaroo.jooc.ast.ArrayIndexExpr) InfixOpExpr(net.jangaroo.jooc.ast.InfixOpExpr) NewExpr(net.jangaroo.jooc.ast.NewExpr) AsExpr(net.jangaroo.jooc.ast.AsExpr) ApplyExpr(net.jangaroo.jooc.ast.ApplyExpr) Ide(net.jangaroo.jooc.ast.Ide) NamespacedIde(net.jangaroo.jooc.ast.NamespacedIde) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde)

Example 3 with Ide

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

the class JsCodeGenerator method visitCatch.

@Override
public void visitCatch(Catch aCatch) throws IOException {
    List<Catch> catches = aCatch.getParentTryStatement().getCatches();
    Catch firstCatch = catches.get(0);
    boolean isFirst = aCatch.equals(firstCatch);
    boolean isLast = aCatch.equals(catches.get(catches.size() - 1));
    TypeRelation typeRelation = aCatch.getParam().getOptTypeRelation();
    boolean hasCondition = aCatch.hasCondition();
    if (!hasCondition && !isLast) {
        throw Jooc.error(aCatch.getRParen(), "Only last catch clause may be untyped.");
    }
    final JooSymbol errorVar = firstCatch.getParam().getIde().getIde();
    final JooSymbol localErrorVar = aCatch.getParam().getIde().getIde();
    // in the following, always take care to write whitespace only once!
    out.writeSymbolWhitespace(aCatch.getSymKeyword());
    if (isFirst) {
        // "catch"
        out.writeSymbolToken(aCatch.getSymKeyword());
        // "(localErrorVar)":
        out.writeSymbol(aCatch.getLParen(), !hasCondition);
        out.writeSymbol(errorVar, !hasCondition);
        if (!hasCondition && typeRelation != null) {
            // can only be ": *", add as comment:
            typeRelation.visit(this);
        }
        out.writeSymbol(aCatch.getRParen(), !hasCondition);
        if (hasCondition || !isLast) {
            // a catch block always needs a brace, so generate one for conditions:
            out.writeToken("{");
        }
    } else {
        // transform catch(ide:Type){...} into else if is(e,Type)){var ide=e;...}
        out.writeToken("else");
    }
    if (hasCondition) {
        out.writeToken("if(is");
        out.writeSymbol(aCatch.getLParen());
        out.writeSymbolWhitespace(localErrorVar);
        out.writeSymbolToken(errorVar);
        out.writeSymbolWhitespace(typeRelation.getSymRelation());
        out.writeToken(",");
        Ide typeIde = typeRelation.getType().getIde();
        out.writeSymbolWhitespace(typeIde.getIde());
        out.writeToken(typeIde.getDeclaration().getQualifiedNameStr());
        out.writeSymbol(aCatch.getRParen());
        out.writeToken(")");
    }
    if (!localErrorVar.getText().equals(errorVar.getText())) {
        aCatch.getBlock().addBlockStartCodeGenerator(new VarCodeGenerator(localErrorVar, errorVar));
    }
    aCatch.getBlock().visit(this);
    if (isLast) {
        if (hasCondition) {
            out.writeToken("else throw");
            out.writeSymbolToken(errorVar);
            out.writeToken(";");
        }
        if (!(isFirst && !hasCondition)) {
            // last catch clause closes the JS catch block:
            out.writeToken("}");
        }
    }
}
Also used : TypeRelation(net.jangaroo.jooc.ast.TypeRelation) Catch(net.jangaroo.jooc.ast.Catch) Ide(net.jangaroo.jooc.ast.Ide) NamespacedIde(net.jangaroo.jooc.ast.NamespacedIde) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde) JooSymbol(net.jangaroo.jooc.JooSymbol)

Example 4 with Ide

use of net.jangaroo.jooc.ast.Ide 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)

Example 5 with Ide

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

the class JangarooParser method declareValues.

protected static void declareValues(Scope scope, String[] identifiers) {
    for (String identifier : identifiers) {
        Ide ide = new Ide(new JooSymbol(identifier));
        IdeDeclaration decl = new VariableDeclaration(new JooSymbol("var"), ide, null, null);
        decl.scope(scope);
    }
}
Also used : IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration) VariableDeclaration(net.jangaroo.jooc.ast.VariableDeclaration) Ide(net.jangaroo.jooc.ast.Ide)

Aggregations

Ide (net.jangaroo.jooc.ast.Ide)8 QualifiedIde (net.jangaroo.jooc.ast.QualifiedIde)7 VariableDeclaration (net.jangaroo.jooc.ast.VariableDeclaration)4 IdeDeclaration (net.jangaroo.jooc.ast.IdeDeclaration)3 NamespacedIde (net.jangaroo.jooc.ast.NamespacedIde)3 ImportDirective (net.jangaroo.jooc.ast.ImportDirective)2 TypedIdeDeclaration (net.jangaroo.jooc.ast.TypedIdeDeclaration)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 CodeGenerator (net.jangaroo.jooc.CodeGenerator)1 JooSymbol (net.jangaroo.jooc.JooSymbol)1 JsWriter (net.jangaroo.jooc.JsWriter)1 ApplyExpr (net.jangaroo.jooc.ast.ApplyExpr)1 ArrayIndexExpr (net.jangaroo.jooc.ast.ArrayIndexExpr)1 AsExpr (net.jangaroo.jooc.ast.AsExpr)1 AssignmentOpExpr (net.jangaroo.jooc.ast.AssignmentOpExpr)1 BlockStatement (net.jangaroo.jooc.ast.BlockStatement)1 Catch (net.jangaroo.jooc.ast.Catch)1 CompilationUnit (net.jangaroo.jooc.ast.CompilationUnit)1 Directive (net.jangaroo.jooc.ast.Directive)1