import java.util.ArrayList;

/**
 * Derived class that represents a repeat statement in the SILLY language.
 *   @author Dave Reed
 *   @version 2/3/18
 */
public class Repeat extends Statement {
    private Expression expr;
    private ArrayList<Statement> stmts;  
    
    /**
     * Reads in a repeat statement from the specified stream
     *   @param input the stream to be read from
     */
    public Repeat(TokenStream input) throws Exception {
        if (!input.next().toString().equals("repeat")) {
            throw new Exception("SYNTAX ERROR: Malformed repeat statement");
        }

        this.expr = new Expression(input);
        
        if (!input.next().toString().equals("times")) {
            throw new Exception("SYNTAX ERROR: Malformed repeat statement");
        }    
        
        this.stmts = new ArrayList<Statement>();
        while (!input.lookAhead().toString().equals("end")) {
            this.stmts.add(Statement.getStatement(input));
        }
        input.next();
    }

    /**
     * Executes the current repeat statement.
     */
    public void execute() throws Exception {
        Object val = this.expr.evaluate().getValue();
        if (val instanceof Double) {
            int numReps = (int)((Double) val).doubleValue();
            for (int i = 0; i < numReps; i++) {
                for (Statement stmt : this.stmts) {
                    stmt.execute();
                }
            }
        } else {
            throw new Exception("RUNTIME ERROR: Repeat expression must be a number");
        }
    }

    /**
     * Converts the current repeat statement into a String.
     *   @return the String representation of this statement
     */
    public String toString() {
        String str = "repeat " + this.expr + " times \n";
        for (Statement stmt : this.stmts) {
            str += "    " + stmt + "\n";
        }
        return str + "end";
    }
}
