import java.util.ArrayList;

/**
 * Derived class that represents an output statement in the SILLY language.
 *   @author Dave Reed
 *   @version 2/3/18
 */
public class Output extends Statement {
    private Expression expr;

    /**
     * Reads in an output statement from the specified TokenStream.
     *   @param input the stream to be read from
     */
    public Output(TokenStream input) throws Exception {
        if (!input.next().toString().equals("output")) {
            throw new Exception("SYNTAX ERROR: Malformed output statement");
        }
        
        this.expr = new Expression(input);
    }

    /**
     * Executes the current output statement.
     */
    public void execute() throws Exception {
        Object result = this.expr.evaluate().getValue();
        if (result instanceof Double && 
            ((Double)result).doubleValue() == ((Double)result).intValue()) {
            System.out.println(((Double)result).intValue() + " ");
        }
        else if (result instanceof String) {
            String str = (String)result;
            System.out.println(str.substring(1, str.length()-1) + " ");
        }
        else {
            System.out.println(this.expr.evaluate().getValue() + " ");
        }
    }

    /**
     * Converts the current output statement into a String.
     *   @return the String representation of this statement
     */
    public String toString() {
        return "output " + this.expr;
    }
}
