Search in sources :

Example 1 with CaseStatement

use of org.codehaus.groovy.ast.stmt.CaseStatement in project groovy by apache.

the class ClassCodeExpressionTransformer method visitSwitch.

public void visitSwitch(SwitchStatement statement) {
    Expression exp = statement.getExpression();
    statement.setExpression(transform(exp));
    for (CaseStatement caseStatement : statement.getCaseStatements()) {
        caseStatement.visit(this);
    }
    statement.getDefaultStatement().visit(this);
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement)

Example 2 with CaseStatement

use of org.codehaus.groovy.ast.stmt.CaseStatement in project groovy-core by groovy.

the class StatementWriter method writeSwitch.

public void writeSwitch(SwitchStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitSwitch");
    writeStatementLabel(statement);
    statement.getExpression().visit(controller.getAcg());
    // switch does not have a continue label. use its parent's for continue
    Label breakLabel = controller.getCompileStack().pushSwitch();
    int switchVariableIndex = controller.getCompileStack().defineTemporaryVariable("switch", true);
    List caseStatements = statement.getCaseStatements();
    int caseCount = caseStatements.size();
    Label[] labels = new Label[caseCount + 1];
    for (int i = 0; i < caseCount; i++) {
        labels[i] = new Label();
    }
    int i = 0;
    for (Iterator iter = caseStatements.iterator(); iter.hasNext(); i++) {
        CaseStatement caseStatement = (CaseStatement) iter.next();
        writeCaseStatement(caseStatement, switchVariableIndex, labels[i], labels[i + 1]);
    }
    statement.getDefaultStatement().visit(controller.getAcg());
    controller.getMethodVisitor().visitLabel(breakLabel);
    controller.getCompileStack().pop();
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) Label(org.objectweb.asm.Label) Iterator(java.util.Iterator) List(java.util.List)

Example 3 with CaseStatement

use of org.codehaus.groovy.ast.stmt.CaseStatement in project groovy-core by groovy.

the class ClassCodeExpressionTransformer method visitSwitch.

public void visitSwitch(SwitchStatement statement) {
    Expression exp = statement.getExpression();
    statement.setExpression(transform(exp));
    for (CaseStatement caseStatement : statement.getCaseStatements()) {
        caseStatement.visit(this);
    }
    statement.getDefaultStatement().visit(this);
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement)

Example 4 with CaseStatement

use of org.codehaus.groovy.ast.stmt.CaseStatement in project groovy by apache.

the class AntlrParserPlugin method caseStatements.

protected Statement caseStatements(AST node, List cases) {
    List<Expression> expressions = new LinkedList<Expression>();
    Statement statement = EmptyStatement.INSTANCE;
    Statement defaultStatement = EmptyStatement.INSTANCE;
    AST nextSibling = node;
    do {
        Expression expression = expression(nextSibling.getFirstChild());
        expressions.add(expression);
        nextSibling = nextSibling.getNextSibling();
    } while (isType(LITERAL_case, nextSibling));
    if (nextSibling != null) {
        if (isType(LITERAL_default, nextSibling)) {
            defaultStatement = statement(nextSibling.getNextSibling());
            statement = EmptyStatement.INSTANCE;
        } else {
            statement = statement(nextSibling);
        }
    }
    Iterator iterator = expressions.iterator();
    while (iterator.hasNext()) {
        Expression expr = (Expression) iterator.next();
        Statement stmt;
        if (iterator.hasNext()) {
            stmt = new CaseStatement(expr, EmptyStatement.INSTANCE);
        } else {
            stmt = new CaseStatement(expr, statement);
        }
        configureAST(stmt, node);
        cases.add(stmt);
    }
    return defaultStatement;
}
Also used : AST(antlr.collections.AST) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) Iterator(java.util.Iterator) LinkedList(java.util.LinkedList)

Example 5 with CaseStatement

use of org.codehaus.groovy.ast.stmt.CaseStatement in project groovy by apache.

the class StatementWriter method writeSwitch.

public void writeSwitch(SwitchStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitSwitch");
    writeStatementLabel(statement);
    statement.getExpression().visit(controller.getAcg());
    // switch does not have a continue label. use its parent's for continue
    Label breakLabel = controller.getCompileStack().pushSwitch();
    final int switchVariableIndex = controller.getCompileStack().defineTemporaryVariable("switch", true);
    List caseStatements = statement.getCaseStatements();
    int caseCount = caseStatements.size();
    Label[] labels = new Label[caseCount + 1];
    for (int i = 0; i < caseCount; i++) {
        labels[i] = new Label();
    }
    int i = 0;
    for (Iterator iter = caseStatements.iterator(); iter.hasNext(); i++) {
        CaseStatement caseStatement = (CaseStatement) iter.next();
        writeCaseStatement(caseStatement, switchVariableIndex, labels[i], labels[i + 1]);
    }
    statement.getDefaultStatement().visit(controller.getAcg());
    controller.getMethodVisitor().visitLabel(breakLabel);
    controller.getCompileStack().removeVar(switchVariableIndex);
    controller.getCompileStack().pop();
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) Label(org.objectweb.asm.Label) Iterator(java.util.Iterator) List(java.util.List)

Aggregations

CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)5 Iterator (java.util.Iterator)3 List (java.util.List)2 Label (org.objectweb.asm.Label)2 AST (antlr.collections.AST)1 LinkedList (java.util.LinkedList)1 AssertStatement (org.codehaus.groovy.ast.stmt.AssertStatement)1 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)1 BreakStatement (org.codehaus.groovy.ast.stmt.BreakStatement)1 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)1 ContinueStatement (org.codehaus.groovy.ast.stmt.ContinueStatement)1 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)1 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)1 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)1 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)1 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)1 Statement (org.codehaus.groovy.ast.stmt.Statement)1 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)1 SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)1 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)1