import java.util.ArrayList;

/**
 * Class that declares (and assigns) a global variable.
 *   @author Dave Reed
 *   @version 1/26/20
 */
public class VarDeclaration extends Statement {
    private Token variable;
    private Assignment assign;

    /**
     * Constructs am object representing a global variable declaration. 
     *   @param input the stream to be read from
     *   @throws Exception if malformed statement
     */
    public VarDeclaration(TokenStream input) throws Exception {
        Token keyword = input.next();
        if (!keyword.toString().equals("var")) {
            throw new Exception("SYNTAX ERROR: Invalid variable declaration");
        }
        
        this.variable = input.lookAhead();
        this.assign = new Assignment(input);
    }
    
    /**
     * Executes the current assignment statement.
     */
    public void execute() throws Exception {
        Interpreter.MEMORY.declareVariable(this.variable);
        this.assign.execute();
    }
    
    /**
     * Converts the current assignment statement into a String.
     *   @return the String representation of this statement
     */
    public String toString() {
        return "var " + this.assign;
    }
}

