import java.util.ArrayList;

/**
 * Derived class that represents an output statement in the SILLY language.
 *   @author Dave Reed
 *   @version 1/26/20
 */
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
     *   @throws Exception if malformed statement
     */
    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);
        
        if (!input.next().toString().equals(";")) {
            throw new Exception("SYNTAX ERROR: Malformed output statement");
        }
    }

    /**
     * Executes the current output statement.
     */
    public void execute() throws Exception {
        DataValue result = this.expr.evaluate();
        if (result.getType() == DataValue.Type.STRING_VALUE) {
            String str = result.toString();
            System.out.print(str.substring(1, str.length() - 1) + " ");
        } else {
            System.out.print(result.getValue() + " ");
        }
        System.out.println();
    }

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