Search in sources :

Example 1 with ClosureListExpression

use of org.codehaus.groovy.ast.expr.ClosureListExpression in project groovy-core by groovy.

the class StatementWriter method writeForLoopWithClosureList.

protected void writeForLoopWithClosureList(ForStatement loop) {
    controller.getAcg().onLineNumber(loop, "visitForLoop");
    writeStatementLabel(loop);
    MethodVisitor mv = controller.getMethodVisitor();
    controller.getCompileStack().pushLoop(loop.getVariableScope(), loop.getStatementLabels());
    ClosureListExpression clExpr = (ClosureListExpression) loop.getCollectionExpression();
    controller.getCompileStack().pushVariableScope(clExpr.getVariableScope());
    List expressions = clExpr.getExpressions();
    int size = expressions.size();
    // middle element is condition, lower half is init, higher half is increment
    int condIndex = (size - 1) / 2;
    // visit init
    for (int i = 0; i < condIndex; i++) {
        visitExpressionOrStatement(expressions.get(i));
    }
    Label continueLabel = controller.getCompileStack().getContinueLabel();
    Label breakLabel = controller.getCompileStack().getBreakLabel();
    Label cond = new Label();
    mv.visitLabel(cond);
    // visit condition leave boolean on stack
    {
        Expression condExpr = (Expression) expressions.get(condIndex);
        int mark = controller.getOperandStack().getStackLength();
        condExpr.visit(controller.getAcg());
        controller.getOperandStack().castToBool(mark, true);
    }
    // jump if we don't want to continue
    // note: ifeq tests for ==0, a boolean is 0 if it is false
    controller.getOperandStack().jump(IFEQ, breakLabel);
    // Generate the loop body
    loop.getLoopBlock().visit(controller.getAcg());
    // visit increment
    mv.visitLabel(continueLabel);
    for (int i = condIndex + 1; i < size; i++) {
        visitExpressionOrStatement(expressions.get(i));
    }
    // jump to test the condition again
    mv.visitJumpInsn(GOTO, cond);
    // loop end
    mv.visitLabel(breakLabel);
    controller.getCompileStack().pop();
    controller.getCompileStack().pop();
}
Also used : ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Label(org.objectweb.asm.Label) List(java.util.List) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 2 with ClosureListExpression

use of org.codehaus.groovy.ast.expr.ClosureListExpression in project groovy by apache.

the class StatementWriter method writeForLoopWithClosureList.

protected void writeForLoopWithClosureList(ForStatement loop) {
    controller.getAcg().onLineNumber(loop, "visitForLoop");
    writeStatementLabel(loop);
    MethodVisitor mv = controller.getMethodVisitor();
    controller.getCompileStack().pushLoop(loop.getVariableScope(), loop.getStatementLabels());
    ClosureListExpression clExpr = (ClosureListExpression) loop.getCollectionExpression();
    controller.getCompileStack().pushVariableScope(clExpr.getVariableScope());
    List expressions = clExpr.getExpressions();
    int size = expressions.size();
    // middle element is condition, lower half is init, higher half is increment
    int condIndex = (size - 1) / 2;
    // visit init
    for (int i = 0; i < condIndex; i++) {
        visitExpressionOrStatement(expressions.get(i));
    }
    Label continueLabel = controller.getCompileStack().getContinueLabel();
    Label breakLabel = controller.getCompileStack().getBreakLabel();
    Label cond = new Label();
    mv.visitLabel(cond);
    // visit condition leave boolean on stack
    {
        Expression condExpr = (Expression) expressions.get(condIndex);
        int mark = controller.getOperandStack().getStackLength();
        condExpr.visit(controller.getAcg());
        controller.getOperandStack().castToBool(mark, true);
    }
    // jump if we don't want to continue
    // note: ifeq tests for ==0, a boolean is 0 if it is false
    controller.getOperandStack().jump(IFEQ, breakLabel);
    // Generate the loop body
    loop.getLoopBlock().visit(controller.getAcg());
    // visit increment
    mv.visitLabel(continueLabel);
    for (int i = condIndex + 1; i < size; i++) {
        visitExpressionOrStatement(expressions.get(i));
    }
    // jump to test the condition again
    mv.visitJumpInsn(GOTO, cond);
    // loop end
    mv.visitLabel(breakLabel);
    controller.getCompileStack().pop();
    controller.getCompileStack().pop();
}
Also used : ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Label(org.objectweb.asm.Label) List(java.util.List) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

List (java.util.List)2 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)2 ClosureListExpression (org.codehaus.groovy.ast.expr.ClosureListExpression)2 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)2 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)2 Expression (org.codehaus.groovy.ast.expr.Expression)2 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)2 Label (org.objectweb.asm.Label)2 MethodVisitor (org.objectweb.asm.MethodVisitor)2 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)1