Search in sources :

Example 1 with IteratorStatus

use of org.beetl.core.IteratorStatus in project beetl2.0 by javamonkey.

the class ForStatement method execute.

public final void execute(Context ctx) {
    // idNode 是其后设置的
    int varIndex = ((IVarIndex) idNode).getVarIndex();
    Object collection = exp.evaluate(ctx);
    IteratorStatus it = null;
    if (collection == null) {
        if (!this.hasSafe) {
            BeetlException ex = new BeetlException(BeetlException.NULL);
            ex.pushToken(exp.token);
            throw ex;
        } else {
            it = new IteratorStatus(Collections.EMPTY_LIST);
        }
    } else {
        it = IteratorStatus.getIteratorStatusByType(collection, itType);
        if (it == null) {
            BeetlParserException ex = new BeetlParserException(BeetlParserException.COLLECTION_EXPECTED_ERROR, "实际类型是:" + collection.getClass());
            ex.pushToken(exp.token);
            throw ex;
        }
    }
    ctx.vars[varIndex + 1] = it;
    // 
    if (this.hasGoto) {
        while (it.hasNext()) {
            ctx.vars[varIndex] = it.next();
            forPart.execute(ctx);
            switch(ctx.gotoFlag) {
                case IGoto.NORMAL:
                    break;
                case IGoto.CONTINUE:
                    ctx.gotoFlag = IGoto.NORMAL;
                    continue;
                case IGoto.RETURN:
                    return;
                case IGoto.BREAK:
                    ctx.gotoFlag = IGoto.NORMAL;
                    return;
            }
        }
        if (!it.hasData()) {
            if (elseforPart != null)
                elseforPart.execute(ctx);
        }
        return;
    } else {
        while (it.hasNext()) {
            ctx.vars[varIndex] = it.next();
            forPart.execute(ctx);
        }
        if (!it.hasData()) {
            if (elseforPart != null)
                elseforPart.execute(ctx);
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) IteratorStatus(org.beetl.core.IteratorStatus)

Aggregations

IteratorStatus (org.beetl.core.IteratorStatus)1 BeetlException (org.beetl.core.exception.BeetlException)1 BeetlParserException (org.beetl.core.exception.BeetlParserException)1