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);
}
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();
}
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);
}
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;
}
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();
}
Aggregations