<commit_before>/*
 * Copyright (c) 2014, Absolute Performance, Inc. http://www.absolute-performance.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ublu.command;

import ublu.util.ArgArray;
import ublu.util.Generics.StringArrayList;
import ublu.util.Parser;
import ublu.util.Tuple;
import com.ibm.as400.access.AS400SecurityException;
import com.ibm.as400.access.ErrorCompletingRequestException;
import com.ibm.as400.access.JobList;
import com.ibm.as400.access.ObjectDoesNotExistException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.logging.Level;

/**
 *
 * Iterate over an enumerable instancing an instance var
 *
 * @author jwoehr
 */
public class CmdFor extends Command {

    {
        setNameAndDescription("FOR", "/5 @iteratorvar ~@valuevar $[ cmd .. ]$ : FOR enumerable @valuevar execute block instancing @iteratorvar");
    }

    /* Parse and execute a FOR block
     *
     * @param argArray args to the interpreter
     * @return what's left of the args
     */
    /**
     * Do the work of a FOR cmd
     *
     * @param argArray passed-in arg array
     * @return remnant of the arg array
     */
    public ArgArray doCmdFor(ArgArray argArray) {
        getInterpreter().pushLocal(); // local context for iterator
        Tuple iteratorTuple;
        if (!argArray.isNextTupleName()) {
            getLogger().log(Level.SEVERE, "Iterator tuple name " + argArray.next() + " is invalid in {0}", getNameAndDescription());
            setCommandResult(COMMANDRESULT.FAILURE);
        } else {
            String iteratorTupleName = argArray.next();
            iteratorTuple = new Tuple(iteratorTupleName, null);
            getInterpreter().putTupleMostLocal(iteratorTuple);
            if (argArray.peekNext().equalsIgnoreCase("in")) {
                argArray.next(); // discard "in"
            }
            Tuple iteratedTuple = argArray.nextTupleOrPop();
            String block = argArray.nextUnlessNotBlock();
            if (block == null) {
                getLogger().log(Level.SEVERE, "FOR found without a $[ block ]$");
                setCommandResult(COMMANDRESULT.FAILURE);
            } else {
                if (iteratedTuple == null) {
                    getLogger().log(Level.SEVERE, "Iterated tuple does not exist in {0}", getNameAndDescription());
                    setCommandResult(COMMANDRESULT.FAILURE);
                } else {
                    Parser p = new Parser(getInterpreter(), block);
                    ArgArray aa = p.parseAnArgArray();
                    getInterpreter().pushFrame();
                    getInterpreter().setForBlock(true);
                    walkFor(iteratedTuple.getValue(), aa, iteratorTuple);
                    if (getInterpreter().isBreakIssued()) {
                        // If a BREAK then the frame was already popped
                        getInterpreter().setBreakIssued(false);
                    } else {
                        getInterpreter().popFrame();
                    }
                }
            }
        }
        getInterpreter().popLocal();
        return argArray;
    }

    private void walkFor(Object o, ArgArray argArray, Tuple iteratorTuple) {
        if (o != null) {
            if (o instanceof JobList) {
                walk(JobList.class.cast(o), argArray, iteratorTuple);
            }<commit_msg>Better error handling for non-tuple in FOR<commit_after>/*
 * Copyright (c) 2014, Absolute Performance, Inc. http://www.absolute-performance.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ublu.command;

import ublu.util.ArgArray;
import ublu.util.Generics.StringArrayList;
import ublu.util.Parser;
import ublu.util.Tuple;
import com.ibm.as400.access.AS400SecurityException;
import com.ibm.as400.access.ErrorCompletingRequestException;
import com.ibm.as400.access.JobList;
import com.ibm.as400.access.ObjectDoesNotExistException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.logging.Level;

/**
 *
 * Iterate over an enumerable instancing an instance var
 *
 * @author jwoehr
 */
public class CmdFor extends Command {

    {
        setNameAndDescription("FOR", "/5 @iteratorvar ~@valuevar $[ cmd .. ]$ : FOR enumerable @valuevar execute block instancing @iteratorvar");
    }

    /* Parse and execute a FOR block
     *
     * @param argArray args to the interpreter
     * @return what's left of the args
     */
    /**
     * Do the work of a FOR cmd
     *
     * @param argArray passed-in arg array
     * @return remnant of the arg array
     */
    public ArgArray doCmdFor(ArgArray argArray) {
        getInterpreter().pushLocal(); // local context for iterator
        Tuple iteratorTuple;
        if (!argArray.isNextTupleName()) {
            getLogger().log(Level.SEVERE, "Iterator tuple name " + argArray.next() + " is invalid in {0}", getNameAndDescription());
            setCommandResult(COMMANDRESULT.FAILURE);
        } else {
            String iteratorTupleName = argArray.next();
            iteratorTuple = new Tuple(iteratorTupleName, null);
            getInterpreter().putTupleMostLocal(iteratorTuple);
            if (argArray.peekNext().equalsIgnoreCase("in")) {
                argArray.next(); // discard "in"
            }
            Tuple iteratedTuple = argArray.nextTupleOrPop();
            if (iteratedTuple == null) {
                getLogger().log(Level.SEVERE, "Iterated tuple does not exist in {0}", getNameAndDescription());
                setCommandResult(COMMANDRESULT.FAILURE);
            } else {
                String block = argArray.nextUnlessNotBlock();
                if (block == null) {
                    getLogger().log(Level.SEVERE, "FOR found without a $[ block ]$");
                    setCommandResult(COMMANDRESULT.FAILURE);
                } else {
                    Parser p = new Parser(getInterpreter(), block);
                    ArgArray aa = p.parseAnArgArray();
                    getInterpreter().pushFrame();
                    getInterpreter().setForBlock(true);
                    walkFor(iteratedTuple.getValue(), aa, iteratorTuple);
                    if (getInterpreter().isBreakIssued()) {
                        // If a BREAK then the frame was already popped
                        getInterpreter().setBreakIssued(false);
                    } else {
                        getInterpreter().popFrame();
                    }
                }
            }
        }
        getInterpreter().popLocal();
        return argArray;
    }

    private void walkFor(Object o, ArgArray argArray, Tuple iteratorTuple) {
        if (o != null) {
            if (o instanceof JobList) {
                walk(JobList.class.cast(o), argArray, iteratorTuple);
            }
{'fraction_non_alphanumeric': 0.08902868109606568, 'fraction_numerical': 0.0046913316984753175, 'mean_word_length': 2.5316265060240966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1050222', 'n_tokens_mistral': 2521, 'n_tokens_neox': 2353, 'n_words': 806}